I'm working with JSP pages, and I need to append some HTML and Java codes inside a DIV, I only remember that I should escape " like this \", but I don't know about the other characters and I don't know if all non-letter characters should be escaped, here is the String.
String s ="<% ResultSet joinedRooms = myJavaDB.updateJoinedRooms(loginBean.getId());
while(joinedRooms.next()){%> <div id="<%=joinedRooms.getString(1)%>" class="chatRoom">
<div class="chatRoomName"><%=myJavaDB.getRoomName(joinedRooms.getInt(1))%></div></div><% } %>"
No need to roll your own, take a look at Apache Commons StringEscapeUtils.
Related
I am trying to convert markdown hyperlinks into html hyperlinks in the Apache Velocity template language (for Marketo). I am nearly there by splitting on ']' and then removing the first character from the remaining '[link text' piece, and the first and last characters from the remaining '(url)' piece.
It will let me remove the first character in each, but doesn't like my code for removing the last character. This is simple code so I don't know why it isn't working.
#set( $refArr = $reference.split(']',2) )
<li>
<a href=$refArr[1].substring(1,$refArr[1].length()-1)>$refArr[0].substring(1)</a>
</li>
It just doesn't like the '-1' part, see error. Velocity is supposed to have full Java method access, but it appears that it may be confusing Java for html.
Cannot get email content- <div>An error occurred when procesing the email Body! </div> <p>Encountered "-1" near</p>
I've also tried using regex with the replace method as well but that doesn't work either, whether with the '(' character escaped, double escaped, or not escaped.
Apparently you have to use the MathTool class for Velocity:
#set( $refArr = $reference.split(']',2) )
<li>
<a href=$refArr.get(1).substring(1,$math.sub($refArr.get(1).length(),1))>$refArr.get(0).substring(1)</a>
</li>
Your code should work in recent versions, otherwise you can do it in two steps:
#set($len = $refArr.get(1).length() - 1)
<a href=$refArr[1].substring(1,$len)>$refArr[0].substring(1)</a>
I'm using Thymeleaf to process html templates, I understood how to append inline strings from my controller, but now I want to append a fragment of HTML code into the page.
For example, lets stay that I have this in my Java application:
String n="<span><i class=\"icon-leaf\"></i>"+str+"</span> \n";
final WebContext ctx = new WebContext(request, response,
servletContext, request.getLocale());
ctx.setVariable("n", n);
What do I need to write in the HTML page so that it would be replaced by the value of the n variable and be processed as HTML code instead of it being encoded as text?
You can use th:utext attribute that stands for unescaped text (see documentation). Use this with caution and avoid user input in th:utext as it can cause security problems.
<div th:remove="tag" th:utext="${n}"></div>
If you want short-hand syntax you can use following:
[(${variable})]
Escaped short-hand syntax is
[[${variable}]]
but if you change inner square brackets [ with regular ( ones HTML is not escaped.
Example within tags:
<div>
[(${variable})]
</div>
I'm trying a write a couple small lines of html in my java class that gets some data from another API. I get the data in a JSON string, and would then like to display some of it on a webpage.
To create the HTML, I try:
StringBuilder sb = new StringBuilder();
for(int i=0;i<leads.size();i++){
sb.append("<p>Name: "+leads.get(i).getFirstName()+" "+leads.get(i).getLastName()+"</p>");
sb.append("<p>Email: "+leads.get(i).getEmail()+"</p>");
sb.append("<br />");
}
fullLeadData = sb.toString();
But what ends up being displayed is a literal interpretation of the html tags. Is there a way that I can create this string so that the tags will stay as tags and not the escaped characters?
The java class is a managed bean, so in the html I have:
<body>
<div id="display">
#{PortalView.fullLeadData}
</div>
</body>
Where fullLeadData is the string with the html.
Seems like you're using JSF. Try this:
<div id="display">
<h:outputText value="#{PortalView.fullLeadData}" escape="false"/>
</div>
You may need to replace the escape sequences. The common ones being
‘&’ (ampersand) ‘&‘
‘"’ (double quote) ‘"‘
”’ (single quote) ‘'‘
‘<’ (less than) ‘<‘
‘>’ (greater than) ‘>‘
I need to use regular expression with a string from a rss feed. It is following string.
private String text = "<![CDATA[<table border="0" cellspacing="0" cellpadding="6px"> <tr><td valign="top" align="center" width="150px"><img src="http://static.desktopnexus.com/thumbnails/978620-thumbnail.jpg" border="1" style="border: 1px solid #000000;"><br><strong>Princess,Aurora,Sleeping,Beauty</strong></td><td valign="top" style="font-size: 10pt;">A new wallpaper has been posted to the Entertainment gallery on Desktop Nexus.<br><br>Uploaded By: Jessowey<br>Category: Movies<br>Date Uploaded: 02/23/12<br>Native Resolution: 1024x768<br>Points: +1<br>Download Count: 0<br><br><b>View This Wallpaper Now</b></td></tr></table>]]>";
As you can see there are " inside the string, so I can't use it as a statement. I tried to use raw string but as you know it is not possible in java.
How can I extract img tag from the above statement. I need to do it programatically.
Thanks in advance!
It's very, very difficult (in general) to use regular expressions to parse XML/HTML, there is another post which lists good XML parsers for Java and their strengths, I suggest you use one of these.
If you wnat to use " in String literal in java you have to escape them with backslash like that
String stringWithParenthesis = "text \"in parenthesis\" out ";
Use \" in a string to use the " inside it. For example:
String yourFeed = "My so \"called\" String";
This works for some other special character like the backslash itself:
String antoherFeed = "Hello \"World\", what a nice \\ day \\ ";
You can parse your RSS-FEED for such special characters. Like in this question:
JAVA: check a string if there is a special character in it
and format them to valid string characters.
I don't know how to escape ' in a java properties file. I'm using struts2 and getText function to fill i18n and text tags.
For example I use:
config.users.title= taula d'usuaris ---> config.users.title= taula d\'usuaris
But in my JSP, I get : taula dusuaris.
I use this to display text on my JSP:
<label for="title"><s:text name="config.users.title" />:</label>
Also I sometimes I use:
<s:select id="categories" name="categories" headerKey=""
headerValue="%{getText('map.categories.all')}"
list="categories" listKey="id" listValue="name"/>
What's the error?
Thanks!
Thanks lucentmind and BalusC foryour repliees
the solution was this: on my properties file I quoted ' as \'' and works fine.
**taula d \''usuaris**
Thanks
try to use double quote as escape character to show single quote on HTML page.
so you can mention value in propertis file like this : taula d "\'"usuaris