Page tree
Skip to end of metadata
Go to start of metadata

Throughout the following pages, this example will be used to show how Rabbtor improves the model localization and labeling in a Spring application. 

Below is a couple of interrelated model messages which we validate with JSR 303 validation annotations:

The example is written in Groovy but Java version will be the same only with additional get/set methods.  

 

Command objects

 

class RegisterCommand
{

    @NotNull
    String name

    @NotNull
    Long departmentId

    @Valid
    AddressCommand address

    @Valid
    List<AddressCommand> addresses = []

    RegisterCommand()
    {
        // Add one address to be displayed on form
        addresses.add( new AddressCommand())
    }
}

class AddressCommand
{

    @NotEmpty
    String zipcode

}

 

And below is the register.jsp file which holds a form to fill in the RegisterCommand command object.

register.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<body>

<form:form modelAttribute="registerCommand" >

    <p>
        <label for="name" >Name</label>
    		<form:input path="name" />
    		<form:errors path="name" />
    </p>

    <p>
        <label for="department" >Department</label>
	    <form:input path="department" />
    		<form:errors path="department" />
    </p>

    <h3>Addresses</h3>
    <c:forEach items="${addCommand.addresses}" var="address" varStatus="i" >
        <h5>Address: ${i.index}</h5>
        <label for="addresses[${i.index}].zipcode" >Zipcode</label>
        <form:input path="addresses[${i.index}].zipcode" />
    </c:forEach>


    <input type="submit" value="Submit" />
</form:form>

</body>
</html>

And here is the messages.properties file for some basic localization of error messages:

messages.properties

 

NotEmpty={0} can not be empty.
NotNull={0} can not be null.
zipcode=Zipcode


  • No labels