Spring MVC Error Messages
Hello, Spring Fellows,
I have a form that is validated by the Spring Validation once submitted. Each field on the form may contain multiple errors messages if validation fails, so error messages are displayed below the field, not next to it. Here's the code snippet.
<tr>
<td><form:input path="name" /></td>
</tr>
<tr>
<td>
<form:errors path="name*" />
</td>
</tr>
Note that there is a star at the end of the path value to indicate that all error messages for the name must be displayed.
As you can see, the problem is that, if there is no error message, there will be an extra row on the page that looks out of place to the user. The code above is an overly simplied version, so the actual code has a lot more stuff in it, which prevents me from moving the <form:errors> tag inside the tag containing the field.
Is there a way to find out if there is any message associated to a given path on the JSP level? Basically, I would like to do the following:
<c:if test="${what do I write here?}">
<tr>
<td>
<form:errors path="name*" />
</td>
</tr>
</c:if>
Thanks!
You can do something like this (notice that bind is from spring taglib):
<spring:bind path = "name*">
<c:if test="${status.error}">
<tr>
<td>
<form:errors path="name*" />
</td>
</tr>
</c:if>
</spring:bind>
I solved your problem by doing this:
<table>
<form:errors path="firstName">
<tr>
<td colspan="2">
<form:errors path="firstName"/>
</td>
</tr>
</form:errors>
<tr>
<td><form:label path="firstName"><spring:message code="helloworld.label.firstName"/></form:label></td>
<td><form:input path="firstName"/></td>
</tr>
</table>
errors tag body will be evaluated only if there are errors on the path.
The simplest answer is to not use tables for page layout. Utilizing div tags alleviates this problem altogether since divs are completely dimensionless if set to hidden.
Related
I am working on a Java-Project with html/ftl and so on...
I have a ftl file with a list like this:
<table id="availableHOs">
<tr>
<th>#</th>
<th>Street</th>
<th>Town</th>
<th>Capacity</th>
</tr>
<#list availableOffers as ho>
<tr>
<td>${ho.id}</td>
<td>${ho.addressData.street}</td>
<td>${ho.addressData.town}</td>
<td>${ho.capacity}</td>
</tr>
</#list>
</table>
(That is a template which we got from the university.)
My problem is that the Link in the table (with the title "Make Booking" uses a GET and not a POST. Could somebody help me to change this to a POST?
I only have an example with a submit button and a form:
<form method="POST" action="guestgui?action=projekteSuchenU">
But I dont know how I would use this in the table.
In summary, I would like to have a table / list where I have a link for each line that works with POST
I am abolut new in this topic, so please sorry for my bad explanation!
Thank you!
Add form instead tag 'a':
<table id="availableHOs">
<tr>
<th>#</th>
<th>Street</th>
<th>Town</th>
<th>Capacity</th>
</tr>
<#list availableOffers as ho>
<tr>
<td>
<form method="POST" action="guestgui?action=selectHolidayOffer&hid=${ho.id}">
<input type="submit" value="${ho.id}"/>
</form>
</td>
<td>${ho.addressData.street}</td>
<td>${ho.addressData.town}</td>
<td>${ho.capacity}</td>
</tr>
</#list>
</table>
There are 2 tables here:
The table below is using purely container to display populate the tables from database whereas the one above is using datatable.
However i wish to duplicate the last column of displaying a icon menu for edit and delete actions which is contained inside actions.jsp inside the table above.
This is my partial code in view.jsp displaying the datatables.
<%
List<Guestbook> guestbookList = GuestbookLocalServiceUtil.getGuestbooks(scopeGroupId);
request.setAttribute("guestbookList", guestbookList);
%>
<table id="gbdb" class="table table-bordered table-striped" style="width:100%">
<thead>
<tr>
<th>Name</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<c:forEach items="${guestbookList}" var="guestbook">
<tr>
<td>${guestbook.name}</td>
<td>${guestbook.status}</td>
<td><jsp:include page="${actions.jsp}" flush="true" /></td>
</tr>
</c:forEach>
</tbody>
</table>
However as you can see my function of diplaying actions.jsp is not similar to the method for using container where i can just referenced the path using
<td> <liferay-ui:search-container-column-jsp
align="right"
path="/guestbookadminportlet/guestbook_actions.jsp" /></td>
Please help me with the correct way of displaying by referencing to actions.jsp
You should not include whole jsp as an action column if using jQuery data-table,
Let's assume you need to add Delete book action so your td should be replace by following code.
<td>
<portlet:actionURL name="<%=DELETE_BOOK%>" var="deleteBookUrl">
<portlet:param name="bookId" value="${book.id}"/>
</portlet:actionURL>
<input type="button" onclick="location.href='${deleteBookUrl}'" value="Delete"/>
</td>
Hope this will help you.
I am working on Struts2 Application;
my displaying objects are Map objects:
Map<String, HashMap<String, List<PlanDAO>>> channelRoomTypePlanmap =
new HashMap<String,HashMap<String,List<PlanDAO>>>();
Map<String, List<RateInventoryDAO>> rateInventoryMap =
new HashMap<String, List<RateInventoryDAO>>();
Here, I have two objects, where rateInventoryMap key is dependent on channelRoomTypePlanmap value combination.
Like when I print first value of channelRoomTypePlanmap I want to create key using combination of its value, and find it in rateInventoryMap , if its true then it'll print its value.
<div>
<table id="mainTable" >
<tr>
<td>
<s:iterator value="channelRoomTypePlanmap">
<table border="1">
<tr>
<td><s:property value="key" />
</td>
<td>
<table>
<s:iterator value="value">
<tr>
<td><s:property value="key" />
</td>
<td>
<table border="1">
<s:iterator value="value">
<tr>
<td><s:property value="planName" />
</td>
<s:iterator value="rateInventoryMap">
<s:iterator value="value">
<td><s:property value="currentRate" />
</td>
</s:iterator>
</s:iterator>
</tr>
</s:iterator>
</table>
</td>
</tr>
</s:iterator>
</table>
</td>
</tr>
</table>
</s:iterator>
</td>
</tr>
</table>
</div>
I tried this, which is showing in wrong format, means showing all data from second Map in single row for all. I've also heard that scriptlet code should be avoided.
How can I join these two Maps on JSP ?
Current View using above code_
Your model structure
new HashMap<String,HashMap<String,List<PlanDAO>>>();
should be improved; it is too noisy, you need five nested iterators in your page to do what you need, never know in which iterator you are without counting them, and, more important, even if this structure can be used to display data, it can't be used to send it back (you never know).
I strongly suggest you to read the following answer:
Struts2 Object Modelling
That said, when dealing with Collections in a JSP you must read about two great features of OGNL:
OGNL List Selection
OGNL List Projection
I am working on a small Struts 1.2.4 Phonebook app.
The app contains forms in a single JSP to add , search , and edit/delete the data. My problem lies in displaying the ResultSet which is Obtained on performing a wildcard search. Here's the flow of the program :
User enters a search string.
Request is passed to a DispatchAction class's search() method , which in turn calls a searchContact(String name , HttpServletRequest request) method of a 'model' class.
The method executes SELECT query on the DB using a PreparedStatement, which returns a resultset, the values of which are put in an ArrayList< PhoneBook >. The class PhoneBook is a JavaBean with two fields, 'c_name' and 'p_num' , with getters and setters.
logic:bean and logic:iterate tags are used to print the contents of the ArrayList< PhoneBook > in the JSP.
In the JSP :
<html:form action="/PhoneBookAction.do" method="post" >
<table>
<tr>
<td>Name</td>
<td><html:text property="name"/></td>
</tr>
</table>
<br>
<html:submit property="method" value="Search"/>
<br><br><br>
<table align="center" border="2" bordercolor="royal blue">
<thead>
<tr>
<td><b> Select </b></td>
<td><b> Name </b></td>
<td><b> Phone Number </b></td>
</tr>
</thead>
<logic:iterate id="search_resId" name="phonebookform" property="search_res">
<tr>
<td> <bean:write name="search_resId" property="c_name" /> </td>
<td> <bean:write name="search_resId" property="p_num" /> </td>
</tr>
</logic:iterate>
</table>
</html:form>
This usually gives a "Bean not found in any scope" error, or prints nothing. I tried the fixes suggested in some similar threads, But none of them seem to work. Looking forward to a solution. Thank you.
following is my snippet, which was working good untill I migrated from spring 2 to spring 3 and Jstl 1.1 to jstl 1.2. Now, its not working and keep on giving the error
fEvents cannot found on object location
<c:forEach items="${location.fEvents}" var="item" varStatus="loop">
<tr><td><form:input path="fEvents[${loop.index}].hostName" size="30" maxlength="200"/></td>
<td><form:input path="fEvents[${loop.index}].directory" size="30" maxlength="200"/></td>
<td><form:input path="fEvents[${loop.index}].userName" size="20" maxlength="20"/></td>
<td><form:input path="fEvents[${loop.index}].password" size="20" maxlength="20"/></td>
</tr>
</c:forEach>
need to iterate the ftpEvents and show them on jsp
Any suggestion is appreciated!!!
It looks like the object called "location" does not have an fEvents property. Is it actually called ftpEvents? Do you need to just change the variable name?
Even with that, though, you'll probably want to do something more like this:
<c:forEach items="${location.ftpEvents}" var="item">
<tr><td><form:input path="item.hostName" size="30" maxlength="200"/></td>
<td><form:input path="item.directory" size="30" maxlength="200"/></td>
<td><form:input path="item.userName" size="20" maxlength="20"/></td>
<td><form:input path="item.password" size="20" maxlength="20"/></td>
</tr>
</c:forEach>
...you don't need to even use the loop.index at all, if I'm interpreting your code correctly.