How to change label names at runtime in Struts - validation

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>

Related

Display ascii value into Spring input

Using spring form, we display an input like this:
<%# taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
...
<form:input type="text" cssClass="w50" path="lastName" cssErrorClass="w50 error" placeholder="${msgLastName}" />
Sometimes, the users lastname value may content single quote, eg "Job's". This probkem is that we clean the lastname with the OWASP HTML Project, which causes the lastname to be
Job's
When the input is displayed into the browser, the ascii value is also displayed - whici is bad. I would like to display simply "Job's" into the input.
I tested with a simple JSP input, eg.
<input type="text" cssClass="w50" value="${myobject.lastName}" cssErrorClass="w50 error" placeholder="${msgLastName}" />
In this case, the rendering is fine.
My conclusion is that the problem comes from spring, but how to avoid it?
Finally found the answer.
Spring HTML-escapes the values, it doesn't really output
Job's
but
Job&#39;s
As there is an HTML character, it isn't parsed as ascii.
Using Chrome, if I inspect the DOM I cannot see the
&
but if I "edit as HTML" then I see it.

Dandelion Datatables i18n spring resolver not working

I have recently started to integrate datatables in my spring mvc 4 + hibernate 4 + tiles 3 Project.
I want it to display header with various language support.
So I started with this link.
As per this page suggests my header shows ???key??? message.
I want to display Id in column header but it is showing ???table.header.id???.
This link says
If the key cannot be found in the bundle, the ???key??? message will be displayed in the column header.
But I have put following in datatables.properties
i18n.locale.resolver=com.github.dandelion.datatables.extras.spring3.i18n.SpringLocaleResolver
global.i18n.message.resolver=com.github.dandelion.datatables.extras.spring3.i18n.SpringMessageResolver
Also have put in global_en.properties
table.header.id=Id
I also copied same file as global.properties.. but not worked.
My jsp file contains
<datatables:table id="users" ...>
<datatables:column titleKey="table.header.id" property="userId" />
<datatables:table />
My Resource folder structure is
Where should I put table.header.id=Id??
Any help is required. Thanks in advance.
Note: I am using AJAX source + server-side processing.
About the location of your messages
You seem to be using the Spring ResourceBundleMessageSource with global as a basename. So it makes sense to put all translations of header columns in global_*.properties files.
About the ???key??? message
It turns out to be a bug introduced in the v0.10.0.
Waiting for the next version to be released, there is a workaround, but working only with DOM sources.
Here follows the steps.
1) Instead of using the titleKey column attribute, you will use the <spring:message> tag. Theorically, they do the exact same thing: lookup a resource in your configured resource bundle.
Begin by declaring the Spring taglib in your JSP:
<%# taglib prefix="spring" uri="http://www.springframework.org/tags" %>
2) Then you need to update the usage of the Dandelion-Datatables taglib. As a workaround, you can use the <datatables:columnHead> (docs here) tag to insert any content in a column header.
Just use it as follows:
<datatables:table id="users" ... row="user" >
...
<datatables:column>
<%-- Everything inside this tag will only appear in the header column --%>
<datatables:columnHead>
<spring:message code="table.header.id" /> <== this will appear in the column header only
</datatables:columnHead>
<%-- Everything else will appear in all cells --%>
<c:out value="${person.id}" /> <== this will appear in all column cells
</datatables:column>
...
<datatables:table />
Some observations:
You need to add the row table attribute if you need to access the object of the collection being iterated on, as it's done with the <c:out> tag of the JSTL
You need to remove the property column attribute, or the content of the <datatables:column> tag will not be evaluated
This is a lot of work for very little return - sorry for that - but waiting for the next version to be released, at least it works.
A new issue has been added.
If you are using AJAX source + server-side processing.
Make a variable first
<spring:message code="table.header.id" var="titleId" />
and added it in
<datatables:column title="${titleId}" property="userId" />
Also a fix is available here. Kindly upgrade to 0.10.1-SNAPSHOT version.
(Disclaimer required by StackOverflow: I'm the author of Dandelion)

Displaying spring form error outside of form

How can we display form errors out side of form. I know it can be displayed inside of form using <sf:errors path="nb"></sf:errors>. If I want to display it in separate div how can I do it? I am new to spring so please guide me.
You could, if you're planning to display all error messages simultaneously, using the following taglib.
<%#taglib uri="http://www.springframework.org/tags" prefix="spring" %>
Something like,
<spring:hasBindErrors htmlEscape="true" name="someBean">
<c:if test="${errors.errorCount gt 0}">
<h4>The error list :</h4>
<font color="red">
<c:forEach items="${errors.allErrors}" var="error">
<spring:message code="${error.code}"
arguments="${error.arguments}"
text="${error.defaultMessage}"/><br/>
</c:forEach>
</font>
</c:if>
</spring:hasBindErrors>
Note that the name attribute name="someBean" of the tag <spring:hasBindErrors/> is your actual command object which is bound to your form.

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?

Resources