Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

struts i18n tag not accepting expression value

 
Prasad Ven
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic