I have another probably basic problem. happy if u can help.
there is a request parameter 'action'. if I write :
<label><s:property value="%{#parameters.action}"/></label>
the value appears (it is 1)
So itry to test now :
<s:if test="%{#parameters.action == '1'}">YES 1</s:if><s:else>NOT 1</s:else>
NOT 1 appears.
I have tries all the syntaxes I found on the net for the test. Nothing changes, NOT 1 still displays
Thank you
This is because:
the value of %{#parameters.action} is an array, not a single value, and
the value will be type-converted to a number (not sure why; need to look in to that)
The correct expression would be:
<s:if test="%{#parameters.action[0] == 1}">YES 1</s:if><s:else>NOT 1</s:else>
The correct expression would be:
<s:if test="#parameters.action[0] == 1">YES 1</s:if><s:else>NOT 1</s:else>
The request parameters is a map of [Strinf, String[]], So you have to access it like above
Related
In my Grails 2.4.4 app I am using messages.properties for internationalization, with the following value:
my.app.thing.allowance(s)={0} Allowance(s)
and it is being using in a gsp like so:
<g:message code="my.app.thing.allowance(s)" args="${item.allowances.size()}"/>
the message is correctly displayed for any values greater than 0, for example if item.allowances.size() == 4 then the message displayed is 4 Allowances(s)
the issue is that if item.allowances.size() == 0 then the message displayed is {0} Allowance(s)
I have tried to write the args in several different ways, for example:
<g:message code="my.app.thing.allowance(s)" args="${item.allowances.isEmpty() ? 0.intValue() : item.allowances.size()}"/>
I have debugged things and I am sure that item.allowances.size() == 0 but for some reason it can not handle a value of 0 properly. What is the correct way to pass an argument with an int value of 0 to messages.properties?
In g.message arguments are always passed as an List.
From: http://docs.grails.org/3.0.17/ref/Tags/message.html
args (optional) - A list of argument values to apply to the message when code is used.
Try this code instead:
<g:message code="my.app.thing.allowance(s)" args="[item.allowances.size()]"/>
The Bharat's answer is correct, but I want to add why it happened so:
You have passed args=0
And here it is code from message tag lib:
List args = []
if (attrs.args) {
args = attrs.encodeAs ? attrs.args as List : encodeArgsIfRequired(attrs.args)
}
In groovy 0 is false, that's why you didn't have filled in message in case of ZERO
I'm developing an app using Grails. I want to get length of array.
I got a wrong value. Here is my code,
def Medias = params.medias
println params.medias // I got [37, 40]
println params.medias.size() // I got 7 but it should be 2
What I did wrong ?
Thanks for help.
What is params.medias (where is it being set)?
If Grials is treating it as a string, then using size() will return the length of the string, rather than an array.
Does:
println params.medias.length
also return 7?
You can check what Grails thinks an object is by using the assert keyword.
If it is indeed a string, you can try the following code to convert it into an array:
def mediasArray = Eval.me(params.medias)
println mediasArray.size()
The downside of this is that Eval presents the possibility of unwanted code execution if the params.medias is provided by an end user, or can be maliciously modified outside of your compiled code.
A good snippet on the "evil (or lack thereof) of eval" is here if you're interested (not mine):
https://javascriptweblog.wordpress.com/2010/04/19/how-evil-is-eval/
I think 7 is result of length of the string : "[37,40]"
Seems your media variable is an array not a collection
Try : params.medias.length
Thanks to everyone. I've found my mistake
First of all, I sent an array from client and my params.medias returned null,so I converted it to string but it is a wrong way.
Finally, I sent and array from client as array and in the grails, I got a params by
params."medias[]"
List medias = params.list('medias')
Documentation: http://grails.github.io/grails-doc/latest/guide/single.html#typeConverters
I am trying to populate a text field on my GSP as such:
<label>Phone(aaa-bbb-cccc):</label> <g:textField name="phone" style ="border-radius: 5px"
value="${recordToEdit.telephones = [] ? null : recordToEdit.telephones.first()}"></g:textField><br>
but it still tell me I can't access first() on an empty list. telephones is a List of Strings each being a phone number.
as #gross-jonas pointed out, the recordToEdit.telephones = [] ? .. : .. is terribly wrong already, unless it's a typo
the check you are trying to make should look like:
value="${recordToEdit.telephones ? recordToEdit.telephones.first() : ''}"
or
value="${recordToEdit.telephones?.getAt( 0 ) ?: ''}"
You can just use the Null Safe Operator (?.) as
${recordToEdit.telephones?.first()}
for null checks, which is not sufficient.
UPDATE
for empty list checks and null checks,
${ recordToEdit.telephones ? recordToEdit.telephones[0] : '' }
will be good.
Dude, didn't you just mean == instead of = ?
It looks like you are overwriting your telephones which get issued successfully instead of comparing it.
This has been driving me crazy for hours and its probably very obvious to someone ...
Can anyone see why this is printing out even though its reporting as being false?
<g:if test="${className == 'SRep'}">
${className == 'SRep'}
</g:if>
If classname==SRep then its correct. However if classname <> SRep it still prints out false? I don't understand how this can be.
If I use ?showSource=true, the if statement looks like this:
if(true && ("false")) {
printHtmlPart(29)
}
else {
printHtmlPart(30)
}
Anyone see anything obvious?
Thanks
John
your code looks fine but im not sure what 'className' is, perhaps it's not being returned or returning wrong type etc:
i would display the output in the GSP of className just to see what it is
e.g. add this anywhere in your GSP:
${className} //displays the value
you may also want to check the object type, in your case i think it should be string
so check what you have:
${className?.class} //displays the type of object
If I don't provide the value in the URL for Codeigniter, it'll display a PHP Error was encountered saying it's missing argument for a function call. I do NOT want it displayed at all. If there's no argument in the URL, it should just reroute to its index function. How do I prevent that from happening?
For example, www.example.com/profile/id/45
anyone would guess and enter like this "www.example.com/profile/id"
that would generate the error. How do I prevent that? I tried, "if (!isset($id))" but it generated error before it reached to that condition.
just learned that the solution is simple:
function id($id=NULL)
{
if ($id===NULL)
{
redirect....
}
}
you can also use:
$this->uri->segment(2); // gets 'id'
$this->uri->segment(3); // gets '45'
then do a redirect, check out:
http://codeigniter.com/user_guide/helpers/url_helper.html
use default values for your function
function id($id=0){
if(there any record with $id being provided){
//do something
}else{
//id not provided,set to default values 0 and then
show_404();
}
}
no way there could be id = 0 so its automatically showerror without redirect, if user enter the url www.example.com/profile/id ,