URL Mapping - Replacing characters in a parameter pulled from a database - grails

I am currently trying to figure out, how to modify the parameter being integrated into the URL Mapping I am using.
static mappings =
{
"/$controller/$action?/$id?/(.$format)?"
{
constraints {
// apply constraints here
}
}
name test1: "/.../$title/..."{
controller = "study"
action = "st_show"
}
name test2: "/.../$title/..."{
controller = "search"
action = "se_show"
}
The parameter $title is pretty much a dataset, which is pulled from a database and which will get transmitted in the following format [ this is a title ]. So there are square brackets in front and behind the string and words are seperated through blanks.
If I am creating a link through g:link now with the params nested in, it gets put into the url as it is pulled from the database. What I am attempting is to create SEO-URLs, which will present a certain title of a publication devided by hyphens instead of url-encoded "%20".
Until now, I was able to generate dynamic urls looking like this:
http://localhost:8080/projectname/show/%5BAllgemeine%20Bevölkerungs[...]/782/...PARAMS...
Furthermore I already implemented it through JQuery, though it should be static and users should be able to copy the link to open up the page themselves - that wouldn't be possible when changing the url client-side while loading up the page.
Is there a way to define a function with something like replaceAll.(' ', '-'), which can be invoked onto the parameter in the mapping to replace blanks with hyphens and f.e. square brackets with an empty character?
That's pretty much, what I wasn't able to come by through the documentation.
Thank you already in advance for your help!

I managed to solve my problem by creating a service with a function containing a regex and executing this function onto the parameter title in my g:link, which I firstly converted to a string, which gets passed to the function.
<g:link controller="study" action="st_show" params="[data: data, ... title: ConversionService.convert(fieldValue(bean: path).toString(), ... data: data)]"></g:link>
And the function in the ConversionService
public static String convert(String title){
title = title.replaceAll("\\s", "-").replaceAll("[^0-9a-zA-Z\\-]", "");
return title;
}

Related

getarguments in typo3 from URL

I am working in TYPO3 .....
how can i get arguments from URL
i passed arguments in url like this ,
as a template in resources folder , file name : list.html
<f:form action="update" object="{hotel}" arguments="{myArgument: argumentname}" name="hotel">
and in controller in updateAction() , i want to fetch that agruments , so i write like this ,
$this->view->assign('hotel', array('test' => 'hello' . isset($this->arguments['myArgument']) .'##' . $this->getParametersSafely('myArgument')));
and i make the function in controller...
public function getParametersSafely ($parameterName) {
if ($this-> request-> hasArgument ($parameterName)) {
return $this-> request->getArgument($parameterName);
}
}
So please help me this is not working
I guess "this is not working" means the string in the fluid variable {hotel} is kind of not what you expect? Or what exactly is not working?
First of all isset() returns a boolean, taht you should not just add to your string.
Secondly, if you use arguments="{myArgument: argumentname}"fluid expects argumentname to be a variable passed to the template. If you want to pass a string, you need to specify it: arguments="{myArgument: 'argumentname'}".

How to force the my url not to change the value of '#'

I have an URL address that I change dynamically I it goes something like this:
The dynamic part is the -> edition = Model.Edition.Usually it as an integer value and the url ends up something like that: ....&edition=1232113 . Sometimes I need it to end up like that : &edition=1232113#10_11 and I managed to pass th right value to the edition placeholder but after the reload it doesn't show the same url that I expected it is similar but it substitutes the '#' with '%23'. And it looks something like that: 1232113%2310_11 and the effect is not what I expect.
From the other side, when I type it manually : 1232113#10_11 it works.
Can you help?
If you problem is concerning that the Url.Action is converting a part of your url, you may want to use the RAW method.
#Html.Raw(Url.Action("Method","Controller", new { Id = Model.DId, dbId = Model.DbId, iconId = Model.IconId, edition = Model.Edition })

Replacing html text with parameters in url?

I have the url parmeters http://www.nositeinparticular.coom/product1?test=brand&old=Superman&new=Batman.
I was wanting to know the proper way to use javascript to do the following;
Look to see if test=brand
If it does replace all the values of old with the value of new, ie replacing the word Superman with Batman.
I have this so far to see if test equals brand:
$(document).ready(function() {
var url = location.pathname;
if (url.indexOf("test=brand")) {
}
How would I go about parsing the values from the url and running the replacment?

querystring in URL of MVC app

I am having trouble with building a URL with a query string. I have this code that does what I want it to:
formatoptions: { baseLinkUrl: '#Url.Action("UserInformation", "UserList")', idName: 'Id' }
This makes the proper URL (/UserInformation?Id=4)
This is the section that I am having trouble with, basically trying to replicate what is above, but the syntax is different and I am not sure what's wrong.
results.Add(New SearchResult With {.Link = Url.Action("UserInformation", "UserList", New With {.id = use.Id}), .Text = use.ToString, .Type = "User"})
This make the URL a bit off (/UserInformation/4), it causes problems when redirecting from that page. I'd like to edit this to replicate the proper URL string.
This is a bit of a hack that we figured out to make it work..but I'd like to do it 'properly' if possible
results.Add(New SearchResult With {.Link = Url.Content("~/UserList/UserInformation?Id=" & use.Id), .Text = use.ToString, .Type = "User"})
It's because of your default route having the Id as optional. You can either change the default route which might make all your other Url's look not as clean, or just pick a different parameter name for the Id on the UserInformation action such as userId.

jQuery autocomplete not displaying my encoded values

I am working from this example: http://jqueryui.com/demos/autocomplete/#remote and I am encoding the output like this:
$rows = array();
while($r = mysql_fetch_assoc($category_result))
{
$rows[] = $r;
error_log ("rows: ".$rows[0]);
}
echo json_encode($rows);
But the dropdown on the other side shows nothing. Here is my test page: http://problemio.com/test.php - if you enter "ho" it matches 2 results in the database, but they are not getting displayed for some reason. Any idea why?
Thanks!!
The properties should be named label and value. From the JQuery UI demo page you linked to:
The local data can be a simple Array of Strings, or it contains
Objects for each item in the array, with either a label or value
property or both. The label property is displayed in the suggestion
menu.
So you would need to rename category_name to label either in PHP or later on in your JavaScript source handler function. The latter would require you to replace the PHP URL with a callback function like in the remote example. That way you could get the data any way you want (e.g. by jQuery.getJSON()) and work with it before it gets handed over to the suggestion box.
Hope this helps.
Regarding your comment, this should do it:
$rows = array();
while ($r = mysql_fetch_array($category_result)) {
$rows[] = array("label" => $r["category_name"]);
}
echo json_encode($rows);

Resources