struts logic tag: only works with form bean parameters? - struts-1

I am refactoring an existing web app to use struts tags instead of scriptlets. One of the things occurring multiple tiems is the conditional check like
<%if ("true".equals(requset.getparameter("someParam"))) {%>
some html, javascript
<%else{%>
some other html,javascript
<%}%>
I wish to replace this with struts logic tag to replace scriptlets. Is it possible to do it without storing someParam inside a formbean? By default, it seems the syntax of logic tag works only with formbean parameters.

While not a struts-specific solution: have you thought about using JSTL custom tags? The tags are included in the latest web container specifications and can be easily added older web containers which do not include the JSTL specification by default.
Here is a solution based on JSTL:
<%# taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
...
<c:choose>
<c:when test="${param.someParam eq 'true'}">
some html, javascript
</c:when>
<c:otherwise>
some other html, javascript
</c:otherwise>
</c:choose>
Unless you are using the struts custom tags to generate your UI components, it is always preferred to use the JSTL standard tags whenever possible. However, if you are set on using struts, here is an example with Struts 1:
<bean:parameter id="paramValue" name="someParam" />
<logic:equal name="paramValue" value="true">
some html, javascript
</logic:equal>
<logic:notEqual name="paramValue" value="true">
some other html, javascript
</logic:notEqual>
You will note that this solution takes the request parameter and puts into into a page scoped attribute (paramValue) that can then be accessed by the struts logic custom tag.

Related

How to change label names at runtime in Struts

My application uses Struts messages resources to show message on JSP file.
<message-resources parameter="ApplicationResources" />
This is what I have specified in my Struts XML file.
In my web application I want to give user the freedom to change the label name to some other name. How can I change the label name in Struts at run time, so that updated label is displayed on the screen.
I have tried updating the label using following code in Action class, but it updates the alert message shown on the screen and not the label.
ActionMessages messages = new ActionMessages();
messages.add("App.Screen.ScreenHeading", new ActionMessage("App.Screen.ScreenHeading", "My Heading"));
saveMessages(request, messages);
Actions messages are used with the validation. Better you don't try to set request attributes using this approach because action message are incompatible to the newer versions of Struts framework and you might have problems upgrading those messages. Another approach is to use a form bean or request scope variable to provide the text used to substitute for the message displayed by bean:message tag.
request.setAttribute("App_Screen_ScreenHeading", "My Heading");
the JSP
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<td width="120" class="labelTextSelect"><span class="mandatory">*</span>
<c:if test="${empty App_Screen_ScreenHeading}">
<bean:message key="App.Screen.ScreenHeading" />
</c:if>
<c:if test="${not empty App_Screen_ScreenHeading}">
<c:out value=${App_Screen_ScreenHeading}"/>
</c:if>
</td>

Unable to use JSTL format taglib with Spring MVC form

I am changing some code from a home grown MVC to Spring 2.5 MVC. We have a form to edit an object, so I am using formBackingObject() in my controller to populate the form fields with the current values. In the old MVC, we used the JSTL fmt taglib to format date and money fields. This was nice because the formatting was in the presentation layer.
Now with Spring, the fields are populated correctly with formBackingObject(), but Spring doesn't recognize the the value attribute in the form:input element:
<%# taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<form:form method="post" commandName="editProgramCommand" name="editTitleForm">
<fmt:formatNumber type="NUMBER" value="${program.price}" var="formattedPrice" minFractionDigits="2" />
<form:input path="price" id="price" value="${formattedPrice}" />
... other fields
</form:form>
Thoughts on how to properly format values in a Spring form? I'm not finding much on the web, so I figure its either a really simple syntax error, or I'm completely on the wrong track.
Spring form:input recognize the value of the input from its path attribute and not from the value attribute. If you see the spring form tld, there is no attribute value for the form input tag.
One way which i think is format the values in the back end and bring and set it in the front end.
Otherwise you can use the conventional spring:bind instead of spring form. Spring Bind Reference

Load JSP:Include Based on Session Parameters (Using an MVC/Model 2 Approach)

I am doing some server side form validation and in the case that one or more of the fields is incorrectly filled out, an array gets populated with all of the error messages. On the client side, I have a scriplet that checks for the existence of any error messages and if there are any, it displays them. When the page comes from the servlet it knows if it has failed or not because on a successful submission, it would not reload the form jsp page at all.
This is how I am displaying the error:
<%if(request.getSession().getAttribute("errors") != null){ %>
<jsp:include page="error.jsp"></jsp:include>
<br>
<% } %>
And the error.jsp page is:
<%# page import="java.util.ArrayList" %>
<h3>Oops...We Have a Problem</h3>
Please review and fix the following errors.
<br>
<%
ArrayList errMessages = (ArrayList)request.getSession().getAttribute("errors");
for(int i=0; i<errMessages.size(); i++){
out.println(errMessages.get(i));
%>
<br>
This all works fine, but I am following the MVC/Model 2 Paradigm approach in where I keep the code confined to servlets and the html (display objects) confined to jsp pages. Obviously, this small example breaks the rules.
Is there a way to "pre-build" the jsp page on the servlet so it knows to display the error.jsp and I can do the whole array abstraction on the server? In this example it only seems like a tiny bit of code in the jsp that can't hurt, but in other examples I can see this code becoming a much larger section of the page and that is what I would like to avoid.
Just use taglibs instead of scriptlets to control the flow in JSP. JSTL is a standard JSP taglib and it offers flow control tags.
<c:if test="${not empty errors}">
<jsp:include page="error.jsp" />
</c:if>
and
<c:forEach items="${errors}" var="error">
<c:out value="${error}" /><br/>
</c:forEach>
See also:
How to avoid Java code in JSP files?

Is there a tag like verbatim, except you can set it to dirty?

What I would like is something like following:
<f:verbatim>
<%
out.println("<span id='test'>Data</span>");
%>
</f:verbatim>
I would like the out.println code to be rerun by ajax. However, when I set that form to dirty, the out.println is not rerun.
Is there a way around this, short of using a custom tag?
What I need is a tag, like
<f:writeAsHTML value="#{bean.HTMLproducer}"/>
Is there already a tag like that in myfaces or basic JSF?
Just put it in f:verbatim without that scriptlet
<f:verbatim>
<span id="test">Data</span>
</f:verbatim>
Or just print it plain (only possible on JSF 1.2 or newer)
<span id="test">Data</span>
Or use h:outputText since it renders a <span> anyway
<h:outputText id="test" value="Data" />
Or use h:outputText escape="false" if the HTML comes from a bean property
<h:outputText value="#{bean.html}" escape="false" />
As per your functional requirement which reads like
I would like the out.println code to be rerun by ajax
The problem needs to be solved elsewhere. Are you using JSF 1.x or 2.x? Regardless, you should utilize JSF-provided Ajax components like RichFaces' Ajax4jsf components or JSF 2.0's <f:ajax> components which offers a render attribute to trigger re-rendering of JSF components.

How to create hyperlink in Spring + JSP

What's the proper way to create a hyperlink in Spring+JSP? There must be a better way than just coding in the <a href="..."> tag. Take for example a page that displays people. The URL is people.htm. The corresponding controller gets people from the database and performs optional column sorting. The JSP might look like:
<table>
<tr>
<td>Name</td>
<td>Age</td>
<td>Address</td>
</tr>
...
This seems bad as the URL people.htm is hardcoded in the JSP. There should be a way to have Spring automatically build the <a> tag using the URL defined in servlet.xml.
Edit: Maybe I should be using a Spring form.
The only thing that comes to mind is the JSTL standard tag <c:url>. For example:
<c:url var="thisURL" value="homer.jsp">
<c:param name="iq" value="${homer.iq}"/>
<c:param name="checkAgainst" value="marge simpson"/>
</c:url>
Next
Now this won't get you servlet mapping or the like but nothing will. It's not something you could really do programmatically (after all, a servlet can and usually does map to a range of URLs). But this will take care of escaping for you.
I haven't seen this kind of functionality in pure spring (although grails offers things like that).
For your specific case you might consider removing the file part and only using the query string as the href attribute:
<td>Name</td>
<td>Age</td>
<td>Address</td>
These links append the query string to the path component of the current url.
In Spring MVC in jsp:
You can use:
General Hyperlink:
Click Here
If passing from controller:
Click Here
Jsp tags
<c:url var="URL" value="login">
<c:param name="param" value="${parameter}"/>
</c:url>
Click Here
Hope it Helps.. :)
Better way to create link is:
Name
<%=request.getContextPath() %> makes sure that correct URI will be taken into account.
"sort" parameter you can get over with hidden field and change a value with a little bit of javascript:
<input type="hidden" name="sort" id="sort" value="name">
And controller method should look like this:
#RequestMapping("/people")
public String createUser(String sort) {
...
}
Import this package in your jsp file
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
when you want to redirect new page or url then use for eg.
<a href='<c:url value="url of next page" />'>Home</a>

Resources