3. JSP Example

The search displays all records in the person table and generates links to edit and delete a record. The fmt:message tag retrieves the local sensitive message based on the message resource key passed in.

/WEB-INF/jsp/search/person.jsp
                
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>

<h1><fmt:message key="person.search.title"/></h1>

<table class="search">
    <tr>
        <th><fmt:message key="person.form.firstName"/></th>
        <th><fmt:message key="person.form.lastName"/></th>
    </tr>
<c:forEach var="person" items="${persons}">
    <tr>
        <c:url var="editUrl" value="/info/person.html">
            <c:param name="id" value="${person.id}" />
        </c:url>
        <c:url var="deleteUrl" value="/delete/person.html">
            <c:param name="id" value="${person.id}" />
        </c:url>

       <td>${person.firstName}</td>
        <td>${person.lastName}</td> 
       <td>
            <a href='<c:out value="${editUrl}"/>'><fmt:message key="button.edit"/></a>
            <a href='<c:out value="${deleteUrl}"/>'><fmt:message key="button.delete"/></a>
        </td>
    </tr>
</c:forEach>
</table>
                
            

The form:form custom JSP tag is configured to post to the URL mapping on PersonController.save(Person person) and to bind to the 'person' bean returned in the model. The form:hidden custom JSP tag binds to the Person's id and form:input is used to bind 'firstName' and 'lastName'.

/WEB-INF/jsp/form/person.jsp
                
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>

<h1><fmt:message key="person.form.title"/></h1>

<c:url var="url" value="/save/person.html" /> 
<form:form action="${url}" commandName="person">
    <form:hidden path="id" />

    <fieldset>
        <div class="form-row">
            <label for="firstName"><fmt:message key="person.form.firstName"/>:</label>
            <span class="input"><form:input path="firstName" /></span>
        </div>       
        <div class="form-row">
            <label for="lastName"><fmt:message key="person.form.lastName"/>:</label>
            <span class="input"><form:input path="lastName" /></span>
        </div>
        <div class="form-buttons">
            <div class="button"><input type="submit" value="<fmt:message key="button.save"/>" /></div>
        </div>
    </fieldset>
</form:form>