messages bundle with dynamic key in struts2 - struts2

I have a problem with displaying a message from messages bundle with key passed as a tag argument. Since version 2.3, struts tags don't allow runtime expressions.
I have a custom tag with fieldName parameter and I want to retrieve a message with use of this parameter, like in previous versions of the framework: <s:text name="${fieldName}"/>
Neither <s:text> nor <s:property> work for me.
I tried every solution found in web, but nothing works.

Use OGNL or use <s:set> tag
<s:set name="fname">
${fieldName}
</s:set>
<s:text name="%{#fname}"/>

Related

How do you compare 2 struts param in a struts if

I'm am still pretty new to struts and am having trouble trying to compare two struts params in a struts if statement. I am trying find if the current year param is equal to the checkYear param
If they are equal I want to execute some code. If they are not equal then I would like to execute some other code..
Sample code:
<s:param name="currentYear">
<s:date name="%{new java.util.Date()}" format="yyyy" />
</s:param>
<s:param name="checkYear">
<s:date format="yyyy" name="#year"/>
</s:param>
<s:if test="${checkYear == legendYear}">
code executed
</s:if>
I don't really understand the purpose of the '$','%', or '{}' in the test part of the if statement if or how I need to apply it to have it check the params.
Thank you in advance!
Inside Struts2 tags, you use OGNL. Refer to the small language guide to see how it works.
In OGNL, %{} means you want to force the evaluation of an expression; it is needed somewhere, while it is superfluos somewhere else. In the <s:if/>, it is not needed, because test="" will evaluate its content by default.
${} does not exists in OGNL, it is JSP EL. Search on StackOverflow for some nice answer on that if you are curious.
{} alone in OGNL is List Projection... you probably don't need it now.

Stop Grails from HTML encoding messages?

Note: in the process of upgrading from Grails 1.3.6 to 2.2.2.
If I have a tag such as:
<g:message code="some.code.here" args="${[someHTML]}" />
It is encoding the value as HTML even though the following is set in Config.groovy:
grails.views.default.codec = "none"
This was not a problem in Grails 1.3.6. It does it for all tags throughout the entire project. This is necessary to pass in the links this way, as we are passing in links based on the language.
Any idea why this is not working even though it was working before the upgrade, or a workaround?
If the HTML is in the .properties file, that renders fine. If the variable is just embedded into the page, it works fine. It the act of passing it in as an argument to g:message that causes it to error. I have attempted to use the <%=someHTML%> way to pass it in, but it doesn't seem to like that, telling me that I am missing a quote.
g:message was changed because of a XSS vulnerability (GRAILS-7170). See http://jira.grails.org/browse/GRAILS-10099 for a workaround for continuing to use HTML arguments in certain cases (such as your use case).

Struts2 tag <s:text> I need to lower case the string returned

So I have a tag like this
<s:text name="furniture_logs" />
Where s is defined:
<%# taglib prefix="s" uri="/WEB-INF/tld/struts-tags.tld"%>
and "furniture_logs" is a key from the database (returnes a string) . I want to show the lower case of this string, what can I do ? I am new to Struts2 and i didnt find any reference for that.
The <s:text> tag is for rendering a I18n text messages. If you really want to use it then you could try calling getText method and toLowerCase in <s:property> tag like that:
<s:property value="getText('furniture_logs').toLowerCase()"/>
Struts2 uses OGNL that means you can call Java methods inside Struts2 tags. Read about OGNL http://commons.apache.org/ognl/.
Update
If using <s:text> tag is a must to you, then use var attribute of it.
<s:text var="flogs" name="furniture_logs"/>
<s:property value="#flogs.toLowerCase()"/>

Struts2 <s:url> / <s:param> tags outputting date parameter values that fail in type conversion

I'm struggling with a Struts2 date formatting issue. If I understand correctly, type conversion in Struts2 is locale aware, and any form fields/parameters that map to Date objects should be strings formatted in their Locale specific SHORT format; the default output for a Date object on the value stack is also output as the Locale specific SHORT format (unless overridden with custom formatting).
Although form fields have worked fine with dates, when using the <s:url> tag I can't seem to get the <s:param> tag to encode date parameters correctly. When I try something such as this
<s:url action="foo" >
<s:param name="endDateParam" value="#endDate"/>
</s:url>
the result is pretty obviously not the SHORT format:
/foo.action?endDateParam=Sat+Jan+14+00%3A00%3A00+EST+2012
I re-read the Struts2 documentation but they mostly discuss creating custom date formats in the i18n'ized properties files, which doesn't seem like the right solution.
Any help with this problem would be greatly appreciated.
You can send it like that :
<s:param name="dateFrom">
<s:date name="dateFrom" format="dd.MM.yyyy"/>
</s:param>
You've probably already solved this by formatting a string in the action the way you need it. This I would advise first, unless you are supper stickler for model/view separation or there isn't a one to one mapping between the action and the view in which case this zealousness my be justified.
Lets say you felt the formatting wasn't the business of the action in that case you could use OGNL to it's full effect:
Here is an example that displays the current date (it uses new to construct a new date but you can very easily just replace "new java.util.Date()" with "endDate". It was constructed this way so anyone can just paste it into their JSP without any action dependencies.
<p>
<s:property value="#java.text.DateFormat#getDateInstance(#java.text.DateFormat#SHORT, #java.util.Locale#CANADA).format(new java.util.Date())"/>
</p>
NOTE: requires OGNL static method access to be true. Easiest way to do that is to add the following to struts.xml:
<constant name="struts.ognl.allowStaticMethodAccess" value="true"/>
Using OGNL to this level is a bit suspicious but it is easy to read and the intention is clearly view/presentation related. Although it isn't that easy to construct... The easiest way is to write everything as one line of java and then apply ognl syntax rules which you would find here:
http://commons.apache.org/ognl/language-guide.html
Also for quick reference:
http://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html
http://docs.oracle.com/javase/7/docs/api/java/util/Locale.html

using struts-tags in struts 2 is mandatory. is it?

Is it mandatory in struts2 to use struts-tags. I mean can't we use following:
<td bgcolor="DEDBC6"><div align="left"><font color="#000000"><strong>
Employee Name * :</strong></font></div></td>
<td><input name="empname" type="text" id="empname" size="32"></td>
instead of following:
<s:textfield label="Employee Salary " name="empsal" required="true"/>
I had tried both but when i used 1st i didn't get validation message i.e "empname is required" that i wrote in action-validation.xml although validation is working well.
please comment ?
No, it is not mandatory. In fact a lot of struts2 users are sufficiently dissatisfied with OGNL and choose to use regular HTML instead.
But using the standard HTML tags has the drawback of loosing some functionality, after all that's why the custom tags are there in the first place. It is possible to get validation through validate methods on the controller class even with standard HTML.
If you are just starting out with the framework I suggest you learn the struts tags properly before going off the beaten path.
Is it mandatory in struts2 to use struts-tags.
No, but if you aren't using tags at all then you're not really getting very much out of Struts as a framework. Might as well do it yourself.
i didn't get validation message
If you're using your own markup you'll have to tell it to display the error message. eg.:
<s:fielderror><s:param>empcode</s:param></s:fielderror>
please comment ?
Please stop asking the same questions over and over again.
I don't see why you need any tags at all. Couldn't you just use Struts to generate JSON result types and then use calls to those in your otherwise static jsp pages using JavaScript and JQuery?

Resources