At line 1 changed 1 line. |
__Part V:__ [Adding Validation and List Screen|ValidationAndList] - Adding validation logic to the personForm so that firstName and lastName are required fields and adding a list screen to display all person records in the database. |
__Part V:__ [Adding Validation and List Screen|ValidationAndListSpring] - Adding validation logic to the person object so that firstName and lastName are required fields and adding a list screen to display all person records in the database. |
At line 3 changed 1 line. |
;:''This tutorial depends on __Part IV:__ [Configuring Tiles and Action CRUD methods|ConfiguringTiles].'' |
;:''This tutorial depends on __Part IV:__ [Configuring Tiles and FormController|ConfiguringTilesSpring].'' |
At line 20 changed 1 line. |
To use Commons Validator with Struts (or Spring MVC), normally you have to write a validation.xml file by hand. If you're not using AppFuse, you also have to configure the Validator Plugin and error keys in your ApplicationResources_en.properties. For more information on this, see the [Validation Made Easy Tutorial|http://www.reumann.net/do/struts/lesson3/step8]. |
To use Commons Validator with Spring MVC, normally you have to write a validation.xml file by hand. However, using XDoclet, it's much easier - we just need to add a couple of ''@spring.validator'' tags to our POJO (Person.java). Let's open it up (src/dao/**/persistence/Person.java) and modify your setFirstName and setLastName methods to resemble the following: |
At line 22 removed 2 lines. |
Using XDoclet, it's much easier - we just need to add a couple of ''@struts.validator'' (or ''@spring.validator'') tags to our POJO (Person.java). Let's open it up (src/dao/**/persistence/Person.java) and modify your setFirstName and setLastName methods to resemble the following: |
|
At line 27 changed 3 lines. |
* @return Returns the firstName. |
* @struts.validator type="required" |
* @hibernate.property column="first_name" length="50" |
* @spring.validator type="required" |
At line 31 changed 2 lines. |
public String getFirstName() { |
return this.firstName; |
public void setFirstName(String firstName) { |
this.firstName = firstName; |
At line 36 changed 3 lines. |
* @return Returns the lastName. |
* @struts.validator type="required" |
* @hibernate.property column="last_name" length="50" |
* @spring.validator type="required" |
At line 40 changed 2 lines. |
public String getLastName() { |
return this.lastName; |
public void setLastName(String lastName) { |
this.lastName = lastName; |
At line 45 removed 2 lines. |
%%(color: green)__Spring MVC:__ If you're using Spring for your MVC layer - use @spring.validator tags and put them on the ''setter'' methods, rather than the getters.%% |
|
At line 54 changed 1 line. |
The default key for type="required" is already ''errors.required'', so I usually leave it to the default. This key is defined in web/WEB-INF/classes/ApplicationResources_*.properties. You'll notice that we put these tags on the ''getters'' of this class even though the [XDoclet documentation|http://xdoclet.sourceforge.net/tags/apache-tags.html#@struts.validator%20(0..*)] says to put them on the setters. This is because we are generating our PersonForm.java - the template file (metadata/template/struts_form.xdt) takes care of putting these tags onto the setters in the generated file. |
The default key for type="required" is already ''errors.required'', so I usually leave it to the default. This key is defined in web/WEB-INF/classes/ApplicationResources_*.properties. |
At line 56 changed 1 line. |
Now if you save Person.java and run __ant webdoclet__, a validation.xml file will be generated in build/appfuse/WEB-INF/. It's contents should have now have an entry for "personForm". <span style="color: green">For the Spring MVC version, "personForm" will be "person". |
Now if you save Person.java and run __ant clean webdoclet__, a validation.xml file will be generated in build/appfuse/WEB-INF/. It's contents should have now have an entry for "person". |
At line 60 changed 1 line. |
<form name="personForm"> |
<form name="person"> |
At line 64 changed 1 line. |
<arg0 key="personForm.firstName"/> |
<arg0 key="person.firstName"/> |
At line 69 changed 1 line. |
<arg0 key="personForm.lastName"/> |
<arg0 key="person.lastName"/> |
At line 76 changed 1 line. |
{{{<html:javascript formName="personForm" cdata="false" |
{{{<html:javascript formName="person" cdata="false" |
At line 79 changed 1 line. |
src="<html:rewrite page="/scripts/validator.jsp"/>"></script>}}} |
src="<c:url value="/scripts/validator.jsp"/>"></script>}}} |
At line 81 changed 1 line. |
%%(color: green) For the Spring MVC version, change formName="personForm" to __formName="person"__.%% |
You'd also need to uncomment the "validator" property we commented out earlier. Make sure the "personFormController" <bean> in web/WEB-INF/action-servlet.xml has the following XML: |
At line 83 removed 4 lines. |
!!View JSP with validation added and test [#2] |
|
Now that we have Validation configured for this form, whenever this form is used in an action-mapping with validate="true", these rules will be applied. In the [last tutorial|ConfiguringTiles], we added the "savePerson" action-mapping for PersonAction. The XDoclet tags for this action-mapping were: |
|
At line 89 changed 2 lines. |
* @struts.action name="personForm" path="/savePerson" scope="request" |
* validate="true" parameter="action" input="edit" |
<bean id="personFormController" class="org.appfuse.webapp.action.PersonFormController"> |
<property name="commandName"><value>person</value></property> |
<property name="commandClass"><value>org.appfuse.model.Person</value></property> |
<property name="validator"><ref bean="beanValidator"/></property> |
<property name="formView"><value>personForm</value></property> |
<property name="successView"><value>mainMenu.html</value></property> |
<property name="personManager"><ref bean="personManager"/></property> |
</bean> |
At line 93 changed 1 line. |
So now, as long as your web/pages/personForm.jsp has <html:form action="savePerson">, validation should kick in when we try to save this form. Run __ant db-load deploy__, start Tomcat and go to [http://localhost:8080/appfuse/editPerson.do?id=1]. |
!!View JSP with validation added and test [#2] |
At line 89 added 2 lines. |
Now that we have Validation configured for this form, let's make sure it works. Run __ant db-load deploy__, start Tomcat and go to [http://localhost:8080/appfuse/editPerson.html?id=1]. |
|
At line 98 changed 1 line. |
[validation-required.png] |
[ValidationAndList/validation-required.png] |