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.
Related
I am new to Thymeleaf.I am having a Spring-Boot app which used Thymeleaf component in view pages.
Now while using eclipse code assist I came across few thymeleaf tag/attributes namely th:form,th:formaction.
Once I change my above code to following format :
Its stopped working.My webpage is not getting submitted to server.
So,I wanted to understand the following things:
What is the use of th:form tag?
What is the difference between th:action and th:formaction tag?
The ´th:action´ and ´th:formaction´ tags will create the html tags action and formaction. This question is not really related to Thymeleaf but the html tags itself.
The action tag can be placed onto a form to specify the url to pass the form to. In your first example submitting the form will send a POST request to /saveStudent.
The second example is not valid HTML that's why the form will not submit. formaction can be used to override the action attribute of the form within the form. It can be used on input tags:
<form th:action="#{/saveStudent}" th:object="${user}" method="post">
<table>
<tr>
<td>Enter Your name</td>
<td><input type="text" th:field="*{userName}"/></td>
<td><input type="submit" value="Submit"/></td>
<td><input th:formaction="#{/saveSomewhereElse}" type="submit" value="Submit to other url"/></td>
</tr>
</table>
</form>
In this example the default action is to submit to /saveStudent but if someone clicks the second submit button the form will submit to /saveSomewhereElse.
In general you probably will need just the action tag in 99% of the cases.
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>
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?
I'm validating the input field that's bound to path. I'm using hibernate-validator 4 for this.
Now I'd like to highlight the age label so it pops out of the page (bold, red colour etc.).
However I'm wondering what the cleanest way to do this is.
<spring:hasBindErrors name="*"/> seems to be for the whole form object instead of for a specific field. Any input is appreciated.
Spring provides special jsp tags for forms, which support this task (highlighing in case of error):
For example this jsp
...
<%# taglib prefix='form' uri='http://www.springframework.org/tags/form'%>
...
<form:form method="post"
commandName="myCommand">
<form:input path="name"
cssClass="normalLayout"
cssErrorClass="normalLayout error"/>
<form:errors path="name"
cssClass="errorMessage"/>
</form:form>
...
In this case: the input field uses the css class "normalLayout" if every thing is ok, and the css classes "normalLayout" and "name" if there is a validation error for the field.
form:errors is to print the error message generated while validation.
#see http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/view.html#view-jsp-formtaglib
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>