*
The moose likes Struts and the fly likes struts i18n tag not accepting expression value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "struts i18n tag not accepting expression value" Watch "struts i18n tag not accepting expression value" New topic
Author

struts i18n tag not accepting expression value

Prasad Ven
Greenhorn

Joined: Feb 23, 2010
Posts: 3
Hi all, I have code like this:

<%
String test = "sla_ja_JP";
%>
<s:i18n name="<%= test%>">
<s:text name="sla.text.title"/>
</s:i18n>

the above code does not work and gives an error:
javax.servlet.ServletException:
According to TLD or attribute directive in tag file,
attribute name does not accept any expressions

when I change the tag to - <s:i18n name="sla_ja_JP">
then everything is fine and I see japanese chars in the UI

my question is - how do I make this dynamic, in my app
the locale is retrieved from a database and the property
value (in this case sla_ja_JP) needs to be set accordingly,
so I cannot just hard-code <s:i18n name="sla_ja_JP">,
the value "sla_ja_JP" needs to be passed in dynamically,
can someone suggest a solution, thanks in advance.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Expose the name attribute as an action property (or anything else on the value stack). Scriptlets have no business in JSPs anymore.
Prasad Ven
Greenhorn

Joined: Feb 23, 2010
Posts: 3
Hi David, thanks for your reply, can you give me a quick example
on how to do that (or point me to a tutorial), I tried doing something
like this but did not work - <s:i18n name="%{#page.test}">

David Newton wrote:Expose the name attribute as an action property (or anything else on the value stack). Scriptlets have no business in JSPs anymore.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

How did you put the value into page scope?

In any case--all you need to do is set the locale and the text messages will be pulled from the appropriate resource file.

See:

http://struts.apache.org/2.1.8.1/docs/localization.html
http://struts.apache.org/2.1.8.1/docs/i18n-interceptor.html
Prasad Ven
Greenhorn

Joined: Feb 23, 2010
Posts: 3
sorry David I forgot to show the code for the "page scope" part,
this is what I did:

<%
String slaBundle = "sla_ja_JP";
pageContext.setAttribute("slaBundle",slaBundle);
%>
<input type="text" name="name1" value="<%= pageContext.getAttribute("slaBundle")%>"/>
<BR>
<s:i18n name="%{#page.slaBundle}">
<!-- output in struts -->
<s:text name="sla.text.test_prov"/>
</s:i18n>
<BR>
<!-- this works -->
<s:i18n name="sla_ja_JP">
<s:text name="sla.text.test_prov"/>
</s:i18n>

in the above code, when I set i18n name="sla_ja_JP" then it works,
but when I do i18n name="%{#page.slaBundle}" then I just get back
"sla.text.test_prov" in the UI (I dont get the japanese chars), do I have
the right syntax there?

I verified that the variable is actually in the page context by doing
a pageContext.getAttribute("slaBundle") and it displays the value
properly.

also, how do I retrieve the value of slaBundle within a struts
textfield, I tried the following tags but it did not work:
<s:textfield label="text1" name="text1" value="%{#page.slaBundle}"/>
<s:textfield label="text2" name="text2" value="#page.slaBundle"/>

Thanks for your help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: struts i18n tag not accepting expression value