Replacing a word with trademark in JSP - java

I am displaying data from my oracle database onto my JSP page. Requirement is to locate “PEFC” text, remove PEFC and replace with: PEFC™
I am able to replace the word in JSTL but don't know how to put TM at the end. Here is what I have done so far which is not working
<c:out value="${fn:replace(pageScope.searchResultDisplay.productGroup.name,'PEFC','PEFC'+ ™)}" ></c:out>

I am able to resolve the issue by putting
escapeXml="false"
at the end.

Related

JSTL conditional in Spring

I’m trying to use a JSTL conditional statement on a .jsp page in a Spring MVC app.
I have a model called user and an attribute called gender. The value of gender is displayed when I use this line in the .jsp:
${user.gender}
Specifically, in this case the value is ‘M’. But I want to display a literal ‘Male’ when the value is ‘M’. Based on the documentation I’ve found, my code should be:
<c:if test="${user.gender=='M'}">Male</c:if>
However, that doesn’t display anything. Any ideas what I’m doing wrong here?
The gender variable is defined as a String in my User model class.
I found a fix. As it turns out, my ${user.gender} value was for some reason getting padded with spaces. Thus, I had to trim off those spaces for the conditional to work. For that I used the JSTL trim function:
<c:set var="genderstring" value="${user.gender}"/>
<c:set var="genderstringtrimmed" value="${fn:trim(genderstring)}" />
Then I changed the conditional, which now works:
<c:if test="${genderstringtrimmed=='M'}">Male</c:if>
try
<c:if test="${user.gender.equals('M')}">Male</c:if>

Is it possible to create a local page scope in a JSP?

I'm working on a componentization system based on JSPs. This means that parts of the JSP can be moved from one JSP to an other one via drag and drop and leads to a need for a local page scope as variable of a component defined in one JSP my collide in an other JSP.
I can use Servlet 3.0 / JSP 2.2 / JSTL 1.2.
++ Tag File ++
The straight way of doing that would be to create a tag file for a component or something similar as they have this local page scope. But for every change in the tag file it would need to get redeployed and needing to create a tag file needs some time by itself. But if there is no other solution this direction (custom tags, tag files, JSP includes,...) is probably the way to go.
++ Namespace prefixing/suffixing ++
In Portlets one is supposed to concatenate each variable with a namespace provided from the portlet response object. I could do something similar but this can lead to side effects and unexpected behavior. If someone forgets to prefix/suffix the namespace it might work for some time but stops working at an other time without changing the code when the component moved to an other JSP with a conflicting variable name.
++ Custom Tag wrapping ++
I was hoping that I as a component framework developer can wrap the component code of a component developer with a tag file for a component tag like this
<a:component>
<div data-component-id="9">
<c:set var="componentId" value="9"/>
<c:set var="path" value='${abc:getCurrentPath()}_${componentId}'/>
<c:set var="resource" value='${abc:getResourceFromPath(path)}'/>
<c:set var="val" value="${resource.getValue('paragraphValue')"/>
<p><c:out value="${value}"/></p>
</div>
</a:component>
to have the variable in the local page context of the tag file. But instead they are in the context of the surrounding page. I'm looking for something like this
<% { %>
<div data-component-id="9">
<%
String componentId = 9;
String path = obj.getCurrentPath()+componentId;
Resource resource = otherObj.getResourceFromPath(path);
String value = resource.getValue("paragraphValue");
%>
<p><c:out value="<%=value%>"/></p>
</div>
<% } %>
which would create a code block in which variables have their own namespace. But of course for JSTL and JSTL-EL instead of scriptlet code.
I had a look at the JSTL specification but did not find out if there is a way to create such a local page scope. But I didn't check everything as it's huge and I'm not sure if it's possible with custom tags.
It is clear to me that bigger code blocks should not be in the JSP but with the framework I would like to provide simple solutions for smaller components.
++ Changing the JSP code ++
When components are initially placed on a JSP or moved around via drag 'n drop I actually move the JSP code of a component from one JSP to an other or within a JSP. This means I can also programmatically manipulate the JSP code of a component if it doesn't get too complex and it helps solving the problem.
As I thought that custom tag wrapping could be the ideal solution I created an other more specific question and I've got an answer here that solves the problem.
It's simply to remove all pageContext attributes before the evaluation of the body and adding them again at doEndTag().

Selenium div attributes keep changing, how can I find this element?

I am trying to find an element with Selenium and Java, the problem is that the element's id, class, and name always increment so I am not able to find it with selenium. Below is what I am currently trying:
WebElement field = driver.findElement(By.xpath("//input[contains(#linkText, 'Broadcast copy')]"));
In my html file these are the attributes that keeps changing:
id="files[%2Fopt%240%2Frules%2F%2F000102%2.xml][%2Fcluster%2Fname]"
name="files[%2Fopt%240%2Frules%2F%2F000102%2.xml][%2Fcluster%2Fname]"
value="copy (Cluster 102)"
Entire html
<tbody>
<tr class='rowOdd'>
<td><b>Name</b></td>
<td> <input type='text' data-validation='required validate-name-unique validate-name-not-empty' size='65' id='files[%2Fopt%240%2Frules%2F%2F000102%2Fcluster.xml][%2Fcluster%2Fname]' name='files[%2Fopt%240%2Frules%2F%2F000102%2Fcluster.xml][%2Fcluster%2Fname]' value='copy (Cluster 102)' /> </td>
These always increment and I have no access to the html file to change anything. So my question is how can I find this input element? Thanks in advance.
UPDATE
I get the error:
Unable to locate element:{"method":"id", "selector":"files[.*][.*]"}
I believe the xpath you are using is incorrect. Use
//input[contains(text(), 'Broadcast copy')]
instead of
//input[contains(#linkText, 'Broadcast copy')]
According to the html you have provide the following should work as well
//body[contains(.,'Name')]//input
Try this..
In case "copy (Cluster" text in value attribute is not changing, then you can try below xpath:-
//body[contains(.,'Name')]//input[contains(#value,'copy (Cluster')]
Since the attributes of id, class, and css were constantly changing, 'data-validation' was one that stayed the same all the time. So the code below worked for me.
driver.findElement(By.xpath("//input[#data-validation='required validate-name-unique validate-name-not-empty']"));

Spring is eating </textarea> tags

In my web application (my first with Java, Spring, OR Roo), I'm building a form that has nothing to do with any JPA objects, it's just a form. I really don't want to use JSTL to build my forms here, because there's no data backing for them at this point. I'm using tiles to assemble the pages, so the guts of this form comes from a view, but apart from that there's nothing JSPish about it; it's just a form.
Inside that form, I have a text area that I've written:
<textarea id="whatever" name="whatever"></textarea>
When that comes to the screen, the </textarea> tag is gone. Different browsers deal with that differently, up to and including swallowing up the whole rest of the body HTML inside the text area field.
So I tried putting some content inside that textarea. Spaces and line breaks don't change its behavior, but it appears that any non-space character does. If I go
<textarea>.</textarea>
... it respects my close textarea tag. But then of course my text area renders on the screen with a dot in it, which isn't what I want.
Is this a known issue? Am I doing something wrong?
EDIT:
#bozho: Here's a pertinent chunk of my jsp:
<div id="notes" class="detailPanel">
<div class="panelLabel">Notes</div>
<table >
<thead><tr><th>Date</th><th>By</th><th>Note</th></tr></thead>
<tbody id="notesBody"></tbody>
</table>
<textarea id="newNote" rows="5" cols="80" >.</textarea>
<button id="addNewNote" onClick="saveNote();">Add New Note</button>
</div>
Absolutely nothing fancy going on here (I populate the tbody with rows on the client, is why that's empty). Without the dot in the third-to-last line, the closing textarea tag does not come out in the resulting HTML.
EDIT2 (Solution):
This URL became googlable after hearing some key words from people responding here:
http://www.jroller.com/komu/entry/textareas_with_jspx
Turns out that when jspx pages are parsed, empty tags are collapsed into a single self-closing tag, which breaks text areas. The solution is to put an empty jsp:text in the middle:
<textarea><jsp:text /></textarea>
(Which is STAGGERINGLY stupid, but there it is.)
You are using jspx files right?
In general jspx remove something (or in your case it shorten it: check this: I expect that it addes a slash to the former opening tag, so it becomes: <textarea id="whatever" name="whatever"/> ) where it belives that is not needed. What exactly depends ona bit on the implementation.
So put a <jsp:text> tag in the text area tag to prevent it from "closing"
<jsp:text>
<textarea id="whatever" name="whatever"></textarea>
</jsp:text>
<textarea id="whatever" name="whatever"><jsp:text /></textarea>
for an more complex example have a look at this answer: websphere 7 (and Spring Roo) incompatible with javax.el.ELException

How to write trivial JSP that just returns static XML page

I'm trying to write a trivial JSP that returns the contents of a static xml file. I need to run this in tomcat. Eventually, this will be more dynamic, but at first, I just want to return an xml file. Can anyone point me to a demo for such a trivial beast, I'm trying to learn what are the minimum chunks I need to create the web app and install in tomcat.
Mucho appreciato,
pawpaw17
Following this document is always a good start.
But you may have issues.
First, it's basically trivial to do something like:
http://example.com/app/mydynamicxml.jsp
that returns an XML blob. Just paste the XML in to that file.
But it won't have an XML content type. You can fix that by adding directives to the JSP:
<%#page contentType="application/xml" %>
However, that brings on MORE problems.
Specifically, an XML file CAN NOT start with white space. It MUST start with <?.
That directive will very likely insert a blank line in to you XML file.
So, what you really want is:
<%#page contentType="application/xml" %><?xml version...
Finally, there IS a JSPX version of JSP, which uses an XML syntax, and avoids all of those white space issues. There's also a directive to Tomcat that can eliminate the white space issue. But, out the gate, this is the fastest, "obvious" tact to take.
The main thing would be to specify the content type as <%# page contentType="text/xml" %>
<%-- Set the content type
--%><%# page contentType="text/xml" %><%--
--%><?xml version="1.0" encoding="UTF-8"?>
<root><entry key="key1" value="value1" /><entry key="key2" /></root>
Check out the article on Sun site
Tried adding a trimDirectiveWhitespaces="true" page directive, but that wasn't supported on my server.
The solution was simply to remove any newlines after any page directives.

Categories