Raible's Wiki

Raible Designs
Wiki Home
News
Recent Changes

AppFuse

Homepage
  - Korean
  - Chinese
  - Italian
  - Japanese

QuickStart Guide
  - Chinese
  - French
  - German
  - Italian
  - Korean
  - Portuguese
  - Spanish
  - Japanese

User Guide
  - Korean
  - Chinese

Tutorials
  - Chinese
  - German
  - Italian
  - Korean
  - Portuguese
  - Spanish

FAQ
  - Korean

Latest Downloads

Other Applications

Struts Resume
Security Example
Struts Menu

Set your name in
UserPreferences


Referenced by
Articles
Articles_cn
Articles_pt
Articles_zh
SpringControllers
ValidationAndListSpr...




JSPWiki v2.2.33

[RSS]


Hide Menu

ValidationAndListSpring


Difference between version 17 and version 16:

At line 3 changed 1 line.
;:''This tutorial depends on __Part IV:__ [Configuring Tiles and FormController|ConfiguringTilesSpring].''
;:''This tutorial depends on __Part III:__ [Creating Controllers and JSPs|SpringControllers].''
At line 6 changed 1 line.
This tutorial will show you how to add Validation logic (client and server-side) to the personForm object using Struts' Validator. We'll also create a list screen using the [Display Tag Library|http://displaytag.sf.net] to display all the people in the database.
This tutorial will show you how to add Validation logic (client and server-side) to the Person object using Struts' Validator. We'll also create a list screen using the [Display Tag Library|http://displaytag.sf.net] to display all the people in the database.
At line 11 changed 1 line.
* [1] Add XDoclet Validator to Person.java
* [1] Add XDoclet Validator tags to Person.java
At line 14 added 1 line.
* [4] Add ''getPeople'' methods to PersonDao and Manager
At line 20 changed 1 line.
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/**/dao/Person.java) and modify your setFirstName and setLastName methods to resemble the following:
To use Commons Validator with Spring MVC, normally you have to write a validation.xml file by hand. However, thanks to XDoclet, it's much easier - you just need to add a couple of ''@spring.validator'' tags to our POJO (Person.java). Let's open it up (src/dao/**/dao/Person.java) and modify your setFirstName and setLastName methods to resemble the following:
At line 43 changed 1 line.
@struts.validator type="required" msgkey="errors.required"
@spring.validator type="required" msgkey="errors.required"
At line 66 changed 1 line.
To enable validation in our personForm.jsp, you'll need to make sure your personForm.jsp has the following at the bottom:
To enable validation in personForm.jsp, you'll need to make sure your personForm.jsp has the following at the bottom:
At line 99 changed 1 line.
%%(border: 1px solid black; margin: 0 auto; height: 202px; width: 423px)
%%(border: 1px solid black; margin: 0 auto; height: 215px; width: 521px)
At line 121 changed 1 line.
person = new Person();
person = new Person();
At line 127 changed 1 line.
The reason I'm passing in a person object to the ''getPeople'' method is to allow for filtering (based on values in person) in the future.
The reason I'm passing in a person object to the ''getPeople'' method is to allow for filtering (based on values in person) in the future. Adding this parameter in your getPeople() method signature is optional, but the rest of this tutorial assumes you have done this.
At line 139 changed 1 line.
Save all your files and make sure everything compiles with __ant clean compile__. Now you should be able to run both tests by running the following:
In order for these tests to compile, you need to add the ''getPeople()'' method to the PersonDao and PersonManager interfaces, and their implementations.
At line 142 added 50 lines.
!!Add getPeople() method to DAO and Manager [#4]
Open src/dao/**/dao/PersonDao.java and add the getPeople() method signature:
[{Java2HtmlPlugin
public List getPeople(Person person);
}]
Now add the same method signature to src/service/**/service/__PersonManager.java__. Save all your files and adjust the imports in your tests. Next you need to implement the getPeople() method in your implementation classes. Open src/dao/**/dao/hibernate/PersonDaoHibernate.java and add the following method:
[{Java2HtmlPlugin
public List getPeople(Person person) {
return getHibernateTemplate().find("from Person");
}
}]
<div class="note" style="margin-left: 30px">
You'll notice here that nothing is being done with the ''person'' parameter. This is just a placeholder for now - in the future you may want to filter on it's properties using [Hibernate's Query Language|http://www.hibernate.org/hib_docs/reference/en/html/queryhql.html] (HQL) or using [Criteria Queries|http://www.hibernate.org/hib_docs/reference/en/html/querycriteria.html].
''An example using a Criteria Query:''
[{Java2HtmlPlugin
Example example = Example.create(person)
.excludeZeroes() // exclude zero valued properties
.ignoreCase(); // perform case insensitive string comparisons
try {
return getSession().createCriteria(Person.class)
.add(example)
.list();
} catch (Exception e) {
throw new DataAccessException(e.getMessage());
}
return new ArrayList();
}]
</div>
Now implement the ''getPeople()'' method in src/service/**/impl/PersonManagerImpl.java:
[{Java2HtmlPlugin
public List getPeople(Person person) {
return dao.getPeople(person);
}
}]
After saving all your changes, you should be able to run both tests by executing the following:
At line 144 changed 1 line.
If everything works - ''nice job!'' Now we need to add this ''retrieve all'' functionality to the web tier.
If everything works - ''nice job!'' Now you need to add this ''retrieve all'' functionality to the web tier.
At line 147 changed 2 lines.
In the last couple of tutorials, we've been working with the PersonFormController to interact with our HTML form. Now we need to create a new Controller that'll simply handle getting and displaying a list of people in the database. You could also use a
[MultiActionController|http://www.springframework.org/docs/api/org/springframework/web/servlet/mvc/multiaction/MultiActionController.html] to handle all your List screens.
In the last couple of tutorials, we've been working with the PersonFormController to interact with our HTML form. Now we need to create a new Controller that'll simply handle getting and displaying a list of people in the database.
At line 200 added 3 lines.
;:''You could also use a
[MultiActionController|http://www.springframework.org/docs/api/org/springframework/web/servlet/mvc/multiaction/MultiActionController.html] to handle all your List screens.''
At line 152 removed 2 lines.
;:%%(color: blue)''I copied the testHandleRequest method from UserControllerTest.java and changed the "User" stuff to "Person".''
At line 207 added 12 lines.
package org.appfuse.webapp.action;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.appfuse.Constants;
import org.springframework.web.servlet.ModelAndView;
public class PersonControllerTest extends BaseControllerTestCase {
At line 228 added 1 line.
}
At line 167 changed 1 line.
This class will not compile until you (1) create the PersonController class and (2) add the PERSON_LIST variable to the src/dao/**/Constants.java file.
This class will not compile until you (1) create the PersonController class and (2) add the PERSON_LIST variable to the src/dao/**/Constants.java file. Some folks have suggested that this class be named ''PersonControllerList'' - the name choice is up to you. I prefer Controller and FormController because it tells me one implements [Controller|http://www.springframework.org/docs/api/org/springframework/web/servlet/mvc/Controller.html] while the latter extends [SimpleFormController|http://www.springframework.org/docs/api/org/springframework/web/servlet/mvc/SimpleFormController.html].
At line 182 changed 1 line.
Create src/web/**/action/PersonController.java. It should implement {{org.springframework.web.servlet.mvc.Controller}} and resemble the following:
Create src/web/**/action/PersonController.java. It should implement {{org.springframework.web.servlet.mvc.Controller}} and read as follows:
At line 201 removed 1 line.
At line 284 added 1 line.
At line 232 changed 1 line.
;:%%(color:blue)''I'm going to suggest this class be named PersonListController, so that it parallels PersonFormController.''
Now if you run __ant test-web -Dtestcase=PersonController__, the test should pass. Next, create a URL mapping for this controller. To do this, search for the "urlMapping" bean in web/WEB-INF/action-servlet.xml and add the following line to the "mappings" property:
At line 234 removed 2 lines.
Now if you run __ant test-web -Dtestcase=PersonController__, the test should pass. Now we need to create a URL mapping for this controller. To do this, search for the "urlMapping" bean in web/WEB-INF/action-servlet.xml and add the following line to the "mappings" property:
At line 244 changed 1 line.
Let's create the JSP to hold our list and a Tile's definition for it. There should already be a personList.jsp in the ''web/pages'' folder of your project. If it's not there, you can use the ViewGen Tool to create it. To do this from the command-line, navigate to extras/viewgen and run __ant -Dform.name=Person__. This will generate a personList.jsp in extras/viewgen/build.
There should already be a personList.jsp in the ''web/pages'' folder of your project. If it's not there, you can use the ViewGen Tool to create it. To do this from the command-line, navigate to extras/viewgen and run __ant -Dform.name=Person__. This will generate a personList.jsp in extras/viewgen/build.
At line 266 changed 1 line.
Now we need to add a new definition to tiles-config.xml for this list screen. Open web/WEB-INF/tiles-config.xml and add the following XML:
Finally, add the title and heading keys (personList.title and personList.heading) to web/WEB-INF/classes/ApplicationResources_en.properties. Open this file and add the following:
At line 268 removed 14 lines.
;:%%(color: blue)''I usually copy an existing list definition - i.e. userList.''
[{Java2HtmlPlugin
<!-- Person List definition -->
<definition name="personList" extends="mainMenu">
<put name="titleKey" value="personList.title" />
<put name="headingKey" value="personList.heading" />
<put name="content" value="/WEB-INF/pages/personList.jsp"/>
</definition>
}]
Finally, we need to add these keys (personList.title and personList.heading) to web/WEB-INF/classes/ApplicationResources_en.properties. Open this file and add the following:
At line 315 changed 1 line.
{{{<verifytitle description="display Person List" text=".*$(personList.title}.*" regex="true"/>}}}
{{{<verifytitle description="display Person List" text=".*${personList.title}.*" regex="true"/>}}}
At line 317 changed 1 line.
To test that displaying this page works, we can create a new JSP test in test/web/web-tests.xml:
To test that displaying this page works, you can create a new JSP test in test/web/web-tests.xml:
At line 401 changed 1 line.
You've completed the full lifecycle of developing a set of master-detail pages with AppFuse and Spring's MVC framework - __Congratulations__! Now the real test is if you can run all the tests in your app without failure. To test, stop tomcat and run __ant clean test-all__. This will run all the unit tests within your project. As a reminder, it should be easy to setup and test AppFuse from scratch using __ant setup-db setup-tomcat test-all__. Also, if you're looking for more robust examples - checkout [StrutsResume].
You've completed the full lifecycle of developing a set of master-detail pages with AppFuse and Spring's MVC framework - __Congratulations__! Now the real test is if you can run all the tests in your app without failure. To test, stop tomcat and run __ant clean test-all__. This will run all the unit tests within your project. As a reminder, it should be easy to setup and test AppFuse from scratch using __ant setup-db setup-tomcat test-all__. Also, if you're looking for more robust examples - checkout [Struts Resume].
At line 406 changed 1 line.
Total time: 2 minutes 31 seconds
Total time: 4 minutes 21 seconds

Back to ValidationAndListSpring, or to the Page History.