new line character in JSP - java

I need to call a method from a java bean that returns a string composed of multiple lines
However I need to insert the newline character in the JSP Form.
In java bean, I am using the following method:
for (String s : descriptionContrats){
if(s.startsWith("Contrat")){
contratsBuilder.append(s+"\r\n ");
}
}
contrats=contratsBuilder.toString();
I wish to display the string contrats in multiple lines according to the number of iterations
But when the variable is called in JSP and I use it in the following manner:
<tr><td><%- contrats %> </td></tr>
It prints simply as a single string in a single line.
Update
I have already replaced contratsBuilder.append(s+"\r\n "); by contratsBuilder.append(s).append("<br/>"); but my <br /> tags getting converted to <br /> in the html. However when it renders on browser, it has the <br /> and therefore there is no line break???.

You are displaying the page in HTML, so you need to use HTML's newline tag - <br/>:
for (String s : descriptionContrats) {
if (s.startsWith("Contrat")) {
contratsBuilder.append(s).append("<br/>");
}
}
contrats = contratsBuilder.toString();

Related

multiline in shortdesc tooltip in adf inputText

I want to display the whole password requirements in the shortdesc attribute of Inputtext. But every time I pass a String, it is displaying the text in the same row.
For example I am attaching the code with 'hello world' as shortdesc.Below is the screen for the same:
I want 'hello' in one line and 'world' in another line.Can it be done?If yes, Can anyone help me.
Thanks in advance.
The only way that have worked for me is editing the proper underlying css class of the "shortDesc component" (AFNoteWindowShortDesc) in the skin file and reading the value with the breakline character from a managed bean if you want to control where to break each line:
In my css-skin file:
.AFNoteWindowShortDesc {
white-space: pre; /* To produce the line break */
}
In a managed bean:
private String multilineText = "Hello\nWorld";
public String getMultilineText() {
return multilineText;
}
Finally in the page fragment:
<af:inputText label="Multiline shortDesc in ADF" id="it1"
shortDesc="#{pageFlowScope.departmentManagedBean.multilineText}"/>
Result:
But if your shortDesc text is long and you only want it break automatically, then do this:
Skin file:
.AFNoteWindowShortDesc {
word-break: break-word;
}
Result:
It can be done by adding escape="false" and a <br/> in the middle of your shortDesc.
<af:inputText label="label" id="dc_it1" shortDesc="hello <br /> world" escape="false"/>
The escape=false allow the <br/> to not being HTML-escaped.
For more info see: How to put "new line" in JSP's Expression Language?

Thymeleaf ignores "\n" in String returned from method

In my HTML I'm using paragraph that gets content by calling method via thymeleaf:
<p data-th-text="${fund.formatDescription()}"></p>
Method:
private String description;
public String formatDescription() {
return description.replace(";", " \n ");
}
I want my description to have end lines in palce of every semicolon. So that's why I added \n. But thymeleaf ingores new lines and returns continuous text. I tried adding <br/> but it ends up not interpreted as html. What should I add in place of semicolon to force new line in the description?
Html ignores newlines (this isn't thymeleaf's fault). You can either:
Put the description into <pre></pre> tags (or use the css white-space property on the <p> element).
Instead of replacing ; with \n, replace it with <br /> and use th:utext instead of data-th-text. (This means that html will be unescaped, so you better make sure users can't put other html into the description field or you open yourself up to html attacks).
I made a Thymeleaf dialect that makes it easy to keep the line breaks, if the css white-space property isn't an option.
It also bring support for BBCode if you want it.
You can either import it as a dependency (it's very light) or just use it as inspiration to make your own.
Check it out here :
https://github.com/oxayotl/meikik-project

Using OutputRaw in Java Tapestry

I have a web application running Java Tapestry, with a lot of user-inputted content. The only formatting that users may input is linebreaks.
I call a text string from a database, and output it into a template. The string contains line breaks as /r, which I replace with < br >. However, these are filtered on output, so the text looks like b<br>text text b<br> text. I think I can use outputRaw or writeRaw to fix this, but I can't find any info for how to add outputRaw or writeRaw to a Tapestry class or template.
The class is:
public String getText() {
KMedium textmedium = getTextmedium();
return (textmedium == null || textmedium.getTextcontent() == null) ? "" : textmedium.getTextcontent().replaceAll("\r", "<br>");
}
The tml is:
<p class="categorytext" id="${currentCategory.id}">
${getText()}
</p>
Where would I add the raw output handling to have my line breaks display properly?
To answer my own question, this is how to output the results of $getText() as raw html:
Change the tml from this:
<p class="categorytext" id="${currentCategory.id}">
${getText()}
</p>
To this:
<p class="categorytext" id="${currentCategory.id}">
<t:outputraw value="${getText()}"/>
</p>
Note that this is quite dangerous as you are likely opening your site to an XSS attack. You may need to use jsoup or similar to sanitize the input.
An alternative might be:
<p class="categorytext" id="${currentCategory.id}">
<t:loop source="textLines" value="singleLine">
${singleLine} <br/>
</t:loop>
</p>
This assumes a a getTextLines() method that returns a List or array of Strings; it could use the same logic as your getText() but split the result on CRs. This would do a better job when the text lines contain unsafe characters such as & or <. With a little more work, you could add the <br> only between lines (not after each line) ... and this feels like it might be a nice component as well.

Using regex, how to change the attribute values from one pattern to another

I have a html file in which the html elements have name as follows :
<input type="text" name="HCFA_DETAIL_SUPPLEMENTAL" value="" size="64" />
My requirement is to rename the name attribute value in java naming convention as follows :
<input type="text" name="hcfaDetailSupplemental" value="" size="64" />
Since there are large number of such elements, I want to accomplish that using regex. Can anyone suggest my how to achieve that using regex ?
Do not use regular expressions to go over HTML (why here). Using an appropriate framework such as HTML Parser should do the trick.
A series of samples to get you started are available here.
Using jQuery to get the name, and then regexes to replace all the _[a-z] occurances:
$('input').each(function () {
var s = $(this).attr('name').toLowerCase();
while (s.match("_[a-z]"))
s = s.replace(new RegExp("_[a-z]"), s.match("_[a-z]").toString().toUpperCase());
$(this).attr('name', s);
});
In most cases using regex with html is bad practice, but if you must use it, then here is one of solutions.
So first you can find text in name="XXX" attribute. It can be done by using this regex (?<=name=")[a-zA-Z_]+(?="). When you find it, replace "_" by "" and don't forget to lowercase rest of letters. Now you can replace old value by new one using same regex we used before.
This should do the trick
String html="<input type=\"text\" name=\"HCFA_DETAIL_SUPPLEMENTAL\" value=\"\" size=\"64\"/>";
String reg="(?<=name=\")[a-zA-Z_]+(?=\")";
Pattern pattern=Pattern.compile(reg);
Matcher matcher=pattern.matcher(html);
if (matcher.find()){
String newName=matcher.group(0);
//System.out.println(newName);
newName=newName.toLowerCase().replaceAll("_", "");
//System.out.println(newName);
html=html.replaceFirst(reg, newName);
}
System.out.println(html);
//out -> <input type="text" name="hcfadetailsupplemental" value="" size="64"/>

Pass value of text box to java servlets

Alright cannot find this anywhere and I was wondering how to grab the values of a text box from a jsp or servlet and display it in another servlet.
Now my issue isn't passing the data and actually displaying it, my issue is that whenever a space is in the value I can only get that first bit of information. For example:
<form method="post" action="Phase1Servlet">
<p>Favorite Place:</p> <input type="text" name="place"></div>
<input id="submit" type="submit" value="Submit">
</form>
Say The user types in "The Mall"
in the Servlet I use:
String place = request.getParameter("place");
Then output the variable place somewhere in my code I only get the word "The"
Do I need to use request.getParameterValues("place"); instead? If so how do I pass the values from servlet to servlet through a hidden field? When I do this:
String [] placeArr = request.getParameterValues("place");
out.println("<input type=\"hidden\" name=\"place\" value="+ placeArr +">");
The hidden field actually stores [Ljava.lang.String;#f61f5c
Do i have to parse this or convert this somehow?
Should be
String placeArr = request.getParameterValue("place");
out.println("<input type=\"hidden\" name=\"place\" value=\""+ placeArr +"\">");
Escape the string in the hidden field
Are you really sure that when you use
String place = request.getParameter("place");
the place variable contains only word before first space? Because it is rather weird situation. If you want to pass a parameter to another servlet(assuming that another servlet is called from this servlet) you can set a request attribute in first servlet and then dispatch that request to another servlet, for example:
request.setAttribute("place", "The mail");
RequestDispatcher dispatcher=getServletContext().getRequestDispatcher( path_to_another_servlet );
dispatcher.forward( request, response );
and then in another servlet ypu can use it as:
String place = request.getAttribute("place");

Categories