Add dynamic parts in texts - java

When I try to put some dynamic parts in my texts to be used by <s:text> tag in Struts 2, these parts are replaced by the params I defined.
Here is how I write my sentences in my file.properties:
my_error=The event {0} doesn't exist
Here is how I try to display it:
<s:text name="my_error">
<s:param>Event01</s:param>
</s:text>
But in the result, the expression {0} is NOT replaced and I have no error in the log. What's wrong?

I really can't understand. I picked this example, so I have this in my jsp file:
<s:text name="msg.error">
<s:param >Event01</s:param>
</s:text>
<br />
<s:text name="name.msg.param" >
<s:param >mkyong</s:param>
</s:text>
and this in my .properties:
msg.error = This event doesn't exist: {0}
name.msg.param = This is a message from properties file - param : {0}
But the result is:
This event does not exist: {0}
This is a message from properties file - param : mkyong
I do not manage to find a real difference.

The message must be in a resource bundle with the same name as the
action that it is associated with.
If the named message is not found in a property file, then the body of
the tag will be used as default message.
Create or put the resource bundle for the locale you use to the place better described using the search order in the localization guide.

i guess {0} refers to the first index of the list that should be passed as a parameter to the getText method.
Now when you use s:text and pass the param, it should be a list type variable containing the element with value "Event01" in the first index.
Try implementing the same. It might work :)

Related

escapeHtml="false" in ftl template throws argument type mismatch

This is the code.
<#s.property value="content" escapeHtml="false"/>
variable content is retrieved from the database and contains a HTML fragment. If i remove the escapeHtml attribute the content is retrieved successfully and it is written on the screen. The content contains two images and without the escapeHtml attribute the screen prints the html content as a string representation. But i want to view the images in HTML. So i use escapeHtml="false" and an exception is thrown as i can see from the logs.
Caused by: java.lang.IllegalArgumentException: argument type mismatch
What should i do to view the content as HTML?
Certainly that should be escapeHtml=false, without the quotation marks. If you write "false", that's a string in FreeMarker. (BTW, 2.3.22 explains this in the error message, so certainly you aren't using the latest version.)
i used the same piece of code in a jsp. The code in the jsp is :
<s:property value="content" escapeHtml="false"/>. That worked properly. I guess it is a freemarker issue.

How to pass dynamic parameter value in a link using Struts taglib?

I'm trying to use Struts framework in my project. I want to use the html:button to send parameter in the link but I don't understand how to make it.
In other words, I want to translate this line:
<input type="button" onClick="window.location.href='resum.do?action=ViewMessage&&id_message=<%= id_msg %>'" value="View"/>
to Struts taglib, something like this:
<html:button property="" onclick="window.location.href='resum.do?action=ViewMessage&&id_message=<%= id_msg %>'" value="View"></html:button>
But it didn't work.
The html:button tag is used only inside the form tag. See the docs
This tag is only valid when nested inside a form tag body.
Also set the property attribute.

Insert new line in struts2 messages.properties

I want to display the error message in two lines in Struts2
User Name is not valid
Password is not valid
and my property is:
username.password.errrorMsg: User Name is not valid \n Password is not valid.
I added \n but its displaying in single line.
Can you suggest to display in two lines?
If you use a message format then \n symbol add a new line character. If you want to display this message with actionerror or actionmessage tags you need to use <br> and let it not escape. For example
<s:actionmessage escape="false"/>
Because new line/breaking character depends on where do you use/show message it is better to use different messages for that.
invalid.userName = User Name is not valid
invalid.password = Password is not valid
In this way you can use them separately in case you want to show specific message and display them as you want.
If you displaying them in HTML/JSP using S2 <s:text> tag then <br/> should work. But several tags are escaping HTML so for example to use this kind of message in <s:property> with getText() you need to set escapeHTML attribute to false.
Probably < br/> (without the space between < and b :P), as the output format is html.

Thymeleaf string substitution and escaping

I have a string which contains raw data, which I want escaped. The string also contains markers which I want to replace with span tags.
For example my string is
"blah {0}something to span{1} < random chars <"
I would like the above to be rendered within a div, and replace {0} with and {1} with
I have tried a number of things, including doing the substitution in my controller, and trying to use the th:utext attribute, however I then get SAX exceptions.
Any ideas?
You can do this using i18n ?
something like:
resource.properties:
string.pattern=my name is {0} {1}
thymeleaf view:
<label th:text="#{__${#string.pattern('john', 'doe')}__}"></label>
The result should be:
my name is john doe
Im not sure this is a good way. But I hope it could help you
It looks using message parameters is the right approach to output formatted strings. See http://www.thymeleaf.org/doc/usingthymeleaf.html#messages
I suspect you need to pass character entity reference in order to avoid SAX exceptions
<span th:utext = "#{string.pattern(${'<span>john</span>'}, ${'<span>doe</span>'})}"/>
Alternatively place the markup in your .properties file:
string.pattern=my name is <span>{0}</span> <span>{1}</span>

Define a jsp tag which can accept some extra attributes which are not declared in the tld file

I want to define a jsp tag, which can accept some extra attributes which are not declared in the .tld file. Is it possible?
I want this because I want to define a <cms:img> tag, which will generate a html img tag. User can pass some attributes to it, but I don't want to limit user, I hope they can use it just as a normal html img tag.
For example, user can use this tag and pass some required information:
<cms:img id="111" />
Which will generate a html img tag as:
<img src="/show_images?id=111" />
All I need is a id attribute.
But user may treat it as a normal html img tag, and pass some extra attributes to it, like:
<cms:img id="111" width="100px" height="100px" style="..." more attributes .. />
I don't want to declare the other attributes in my tag, because there are too many, and user may have their custom attributes.
So I want to know: Can I just declare the id attribute in the jsp tag, but let it accept all the other undeclared attributes?
Yes, it's possible. These are called dynamic attributes. See http://docs.oracle.com/javaee/1.4/tutorial/doc/JSPTags5.html#wp89775:
dynamic-attributes (optional)
Indicates whether this tag supports
additional attributes with dynamic names. The value identifies a
scoped attribute in which to place a Map containing the names and
values of the dynamic attributes passed during invocation of the tag.
A translation error results if the value of the dynamic-attributes of
a tag directive is equal to the value of a name-given of a variable
directive or the value of a name attribute of an attribute directive.

Categories

Resources