how to break line in jsp from class file in struts - java

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.

Related

JSF Code formatter or Eclipse XML Editor, each tag for new line?

How to format JSF code in eclipse?
I already try as the following :
Format XML code in Eclipse
My XML Editor Configuration Setting is :
Line width 120
Split multiple attributes each on a new like Checked
Align final bracket in mulit-line element tags Checked
Clear all blank lines Checked
If I do Ctrl + Shif + F, eclipse format the code as below.
<tr>
<td><h:outputText value="#{label['MANAGE_USER_TITLE']}"
styleClass="table-title" /></td>
</tr>
My expectation format is, after Ctrl + Shif + F
<tr>
<td>
<h:outputText value="#{label['MANAGE_USER_TITLE']}" styleClass="table-title" />
▲
This line may be wrap base on line size.
</td>
</tr>
How can I do for that? Is there any plugin?
I already try HTML Tidy, I can only format HTML, not JSF code.
Update
Picture -1
Eclipse Setting for Picture-1
Picture - 2 (I want)
Looking at the Settings panel that Maheshbabu Jammula posted, you can see an Inline Elements list. Just remove <tr> and <td> tags from the list and those tags will always land in a new line. Take Care!
Preferences > Web > HTML Files > Editor
Ctrl + Shif + F, only formats code.For indent use ctrl+a and ctrl+i

How to escape the special char in SAX parsing

I am parsing the xml file below:
<description>
<p>
<a href="http://news.yahoo.com/jessica-chastain-talks-princess-diana-biopic- 164102608.html">
<img src="http://l3.yimg.com/bt/api/res/1.2/zD3Iwxezk8JVGQwhow7y4Q--/YXBwaWQ9eW5ld3M7Zmk9ZmlsbDtoPTg2O3E9ODU7dz0xMzA-/http://media.zenfs.com/en_us/News/Reuters/2011-11-07T171906Z_01_BTRE7A61C3Y00_RTROPTP_2_FILM-US-JESSICACHASTAIN.JPG"
alt="photo"
align="left"
title="Actress Chastain poses for photographers as she arrives on the "Wilde Salome" red carpet at the 68th Venice Film Festival" border="0" />
</a>NEW YORK (TheWrap.com) - Jessica Chastain may not win Oscar gold this year, but it appears she will wear a crown.
</p>
<br clear="all"/>
</description>
I am using SAX parser and trying to get the data inside the img tag, title attribute. But because of the special char "Wild Salome" in the text i am getting ExpatParser exception.
Could you please let me know how this can be solved?
The XML is invalid. Attribute value should not contain quotes ("). The program that generated it should replace the inner " characters with ".
If you print the " to a webpage, the browser will automatically show "-character on its place.

getting hidden fields value

I am working on a Struts2 application. I am setting the value of a hidden field in JSP with the purpose to access it by JavaScript.
My JSP code:
<s:iterator value="collegelist">
<tr>
<td align="center"><s:property value="collegename"/></td>
<s:hidden name="hiddenname" key="collegename" />
</tr>
</s:iterator>
My JS code:
var myForm = document.frmAction;
var text = myForm.hiddenname.value;
alert("hidden field text is:" + text);
The alerts shows a blank value.
What is the cause and how can I solve this?
Try
element = document.getElementsByName("hiddenname");
alert(element[0].value);
You generate multiple fields having the same name, since your code is inside a s:iterator tag. You should obviously have such a loop in your Javascript as well :
var hiddenFields = document.getElementsByName("hiddenname");
for (var i = 0; i < hiddenFields.length; i++) {
alert("hidden field text is::" + hiddenFields[i].value);
}
Also, verify the the value is not blank in the generated HTML, and that the hidden fields'a name is hiddenname.
I tried your code and it surely works.. problem is somewhere in your server code itself..
Look here: http://jsbin.com/ajajo4/2/edit
Make sure you have only one form with the name "frmAction" and only one hidden field with the name "hiddenname". If you have multiple, you'll get an array instead of a single value.
The root of the problem is that you are inside of an iterator. Struts updates the name for you in order to correctly hook everything up. If you pull up your page and view source, your hidden field will probably look something like this:
<input type="hidden" name="collegelist[0].hiddenname" value="thename"/>
Regardless, if you want the retrieval by name to work, do not trust the name that you supply to a struts tag. Always pull up the generated source and look at what name the field actually has.

<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.

WhiteSpace Form TextBoxes

I am repopulating a form field that a user previously filled out with Sessions in HTTP. I am grabbing the fields from the form through a servlet:
//On servlet
String polyNum = request.getParameter("policyNum")
session.setAttribute("policyNum", polyNum);
//On JSP
<td align = "right"><font color = "#FF0000">*</font><font face= "Calibri, Arial" size = "3"> Policy #:</font></td>
<td><input type="text" name= "policyNum" size="19" tabindex = "1" value="
<% out.println(session.getAttribute("policyNum");%>">
The Problem: When I run my JSP page, I get a leading whitespace in the textboxes of the form. Therefore, anytime I submit the form, whitespace is inserted into the database as well.
Any ideas? Any help would be greatly appreciated.
You can trim it like this:
String polyNum = request.getParameter("policyNum");
polyNum = polyNum == null ? null : polyNum.trim();
session.setAttribute("policyNum", polyNum );
and if your jsp looks like
<input type ="text" value="
<%= session.getAttribute( "policyNum" ); %>
"/>
you will get whitespace.
Also you don't show where you are inputting into the database.
Take a look at the .trim() method in the String class:
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#trim()
"Returns a copy of the string, with leading and trailing whitespace omitted."
The Apache Commons StringUtils library is good for trimming as it handles nulls.
So
String polyNum = request.getParameter("policyNum");
polyNum = polyNum == null ? null : polyNum.trim();
Can become
String polyNum = StingUtils.stripToEmpty(request.getParameter("policyNum"));
It turned out that my scriplets within my jsp pages were placed syntanically wrong. I didn't need to trim whitespace after all. My forms are working fine now.
Your solution about the Apache library is interesting Damo. I appreciate your feedback.

Categories

Resources