<logic:iterate question - java

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.

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}"/>

Struts <logic:notEmpty> not working or bean property not being written properly to JSP?

I'm beginning on bug fixes for a program with which I have little familiarity. I've changed a section of code on one of the JSPs. I need it to print names with either of the two following formats (depending on whether or not the middle name property exists):
LastName, FirstName M.
LastName, FirstName
I believe my code should print the middle initial and the period if and only if that property exists, but for each name in the list, it prints:
LastName, FirstName .
It prints either no middle initial or a middle initial that is an empty string, followed by the period.
The relevant code is as follows:
<html:link styleClass="recordLink" action="/secure/admin/users?actionMethod=details" paramId="userId" paramName="users" paramProperty="userId">
<bean:write name="users" property="lastName"/>,
<bean:write name="users" property="firstName"/>
<logic:notEmpty name="users" property="middleName">
<bean:write name="users" property="middleName"/>.
</logic:notEmpty>
</html:link>
Why is the <logic:notEmpty> tag not working? Could the middleName property be determined to be non-empty if the property doesn't exist? Is there something wrong with my syntax?
I've also tried to use JSTL tags, but I could not get it working in OC4J (Error: "http://java.sun.com/jsp/jstl/core" is not a registered TLD namespace.)
The tag logic:notEmpty evaluates to true because your middle name string has spaces. You should get rid of spaces before returning it to the tag. Better do it in the form bean like
public String getMiddleName() { return middleName != null? middleName.trim(): middleName;}
I suspect your middleName is empty string or has spaces. if so try using logic:equal to match and print some character instead of period, see what happens. If true, then trim the middleName, before sending it of to JSP.

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.

How to show localization messages with parameters in Spring 3 / Thymeleaf

I'm using Spring 3 and Thymeleaf to make some webpages and I am lost as for how to show messages like this:
welcome.message=Hello {0}, welcome!
and then replace {0} with the user name inside thymeleaf tags:
<h1 th:text="#{welcome.message}">Welcome Placeholder</h1>
I'm not even sure if {0} is the right syntax for the bundle message.
You can use
#{welcome.message(${some.attribute})}
where some.attribute would be the value to use when replacing {0}.
You should be able to comma separate the values between the () to add more values to be used.
You can even use a calculated message key as a parameter:
<p th:text="#{messages.msg1(${param1})}"></p>
<p th:text="#{messages.msg2(${param2},${param3})}"></p>
<p th:text="#{messages.msg3(#{${param4}})}"></p>
Above, the parameter of [msg3] is a message key [#{key}] where key is itself calculated [${param4}]. The benefit is that you can insert internationalized calculated fragments in an internationalized message.
If you need to pass an array of parameters where you don't know the size of the array then you can use:
<p th:text="${#messages.msgWithParams(messageKey, messageParams)}"></p>
<!-- or -->
<p th:text="${#messages.msgOrNullWithParams(messageKey, messageParams)}"></p>
https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#messages-1

how to break line in jsp from class file in struts

Good day,
Below is part of my jsp code in campaignListing.jsp :
<td><bean:write name="row" property="campaignPeriod" /></td>
Below is part of my code in class files:
String cp = null;
cp= "From " + "<br>" + "To ";
cmForm.setCampaignPeriod(cp); //set the campaignPeriod to display cp string
I have a td with property="campaignPeriod" in my jsp file.
I would like to make the "From" and "To" to display in 2 line in browser.
Would like to ask advise on how to make it. I tried put "<br />" , "\n" but it still displayed in 1 line only.
I am do in struts2 java project.
Not sure but you can try with filter
<bean:write name="row" property="campaignPeriod" filter="false"/>
this is what doc say about filter
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.

Categories

Resources