Insert new line in struts2 messages.properties - java

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.

Related

Cannot escape a quotation(") character when retriveing a string containg quotation inside a string from DB in jsp

I have saved quotation(") in a string using escape character i database. That is working ok. But when i am retrieving the value in a jsp field from database, the string is being ended at the first quotation it gets in the whole string. I am giving an example below:
Lets take a string that i have stored in database as -
" Hello David. This is a "customer"."
Now, i am somehow need to save the string back from databse into a hidden field in a jsp page like below-
<input type="hidden" name="string_from_database" id="string_from_database" value="<%=some varibale that holds the data from database%>">
issue is -
Part of the string is getting exposed (means it is being written on top of the page) which i do not want. In this case,the below phrase is written on the beginning of the jsp page, which i don't want.
customer".
kindly suggest on how to resolve this issue.
Using this function you could replace the quote marks with the html entity variant ". Here's a simple function for it. Hope it fits into your templating system, but should be easy to modify if not.
function escapeQuotes(str){
return str.replace(/"/g,'"');
}
Here's a working fiddle
Use Jstl rather than scriptlets for further Explanation
use EL - Expression Language (${variable}) to get the Value eg. ${welcome}
<c:out value="${some varibale that holds the data from database}"/>

How to print on a new line in JSP

I had a database name which consists of two tables. I retrieved the data from both tables in the same JSP page.
I got the correct output, but I also got the both table data in only one line. I want to print a newline between each table.
I tried printing \n and out.newline()but it did not work.
Print <br /> instead of \n
\n is a new line character in Java not in HTML.
If you want to have new line in HTML rendered by JSP, you could use <BR>. If you want to use it in scriptlet, use the following code:
<jsp:scriptlet>
out.println("<BR>");
</jsp:scriptlet>

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>

<logic:iterate question

I need to pull all teh messages from a table and display on the jsp page. I have the code like below:
I have the list of messages stored as :
SimpleStringVO string value: Welcome to the XYZ Tool homepage. ,SimpleStringVO string value: Here you can enter your account number ,SimpleStringVO string value: ,SimpleStringVO string value: thank you
When I tried to display this in jsp page, the message is not formated as it is stored. "thank you" comes immediately after the 2nd string. I need to display the space string and then the "thank you" string. (3rd string has just spaces) My code in Jsp is like this :
<td><logic:iterate name="AllNewsCashe" id="news" type="com.fw.valueobject.SimpleStringVO">
<bean:write name="news" property="stringValue"/>
</logic:iterate>
</td>
how to display thise messages as it is without formating ?
If you need to force a space in HTML, you will need to store it as instead of " ". However, since are you using bean:write tag, you can probably use filter property to retain " ":-
<bean:write name="news" property="stringValue" filter="false" />
Here's the description of filter from Struts documentation:-
If this attribute is set to true, the
rendered property value will be
filtered for characters that are
sensitive in HTML, and any such
characters will be replaced by their
entity equivalents.

In a spring messages.properties, how to ensure line break of error message when using an error code as key?

In messages.properties:
error.code=This is error message.\nThis is next line of error message.
Now, when I set "errors.rejectValue" with this "error.code" for a form field, I cannot get the line break of '\n' to display on the jsp page when displaying the error message using the form:errors element.
Instead of '\n', using <br/> also does not work and gets displayed as is on the page.
In order to display a <br> as a line break, or for any other html tag in the error message body to take effect e.g. a <b>, simply turn html escaping off at tag level by adding htmlEscape="false" to your form:errors element.
The layout of distinct lines in an HTML page is not really something that a message bundle can deal with, it's just not suitable for the task. If you need multiple lines to be displayed, then realistically you're going to need multiple messages, with multiple entries in the properties file.
Perhaps you could cook up something which iterates over a sequence of properties, with something like this in your properties file:
error.code.1=This is error message.
error.code.2=This is next line of error message.
It then becomes the job of the JSP to render the sequence of messages. Not very elegant, though, but I can't think of a better solution right now :)
i had the same problem while loading the messages from the database, meaning that the '\n' (new line) sequence was escaped. so here is the sample solution, replace the "\\n" with "\n" :
String twoLinesMessage=((String)context.getMessage("error.code", null, locale)).replace("\\n","\n");
Extending the spring tag form:errors and adding conversion from \n to <br /> is also a non-beatiful solution, but one that will probably work.
Use "<br/>" in place of "\n" in your code.
For example:
return i + " x " + j + " = "+ (Integer.valueOf(i * j) + "<br/>"));
This returns the multiplication table in the web with line breaks.

Categories

Resources