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_pt
CreateActions
CreateActions_de
SpringControllers_ko
ValidationAndList_pt




JSPWiki v2.2.33

[RSS]


Hide Menu

ValidationAndList


Difference between version 52 and version 51:

At line 14 added 1 line.
* [4] Add ''getPeople'' methods to PersonDao and Manager
At line 128 changed 1 line.
person = new Person();
person = new Person();
At line 134 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. Add this parameter in your getPeople() method signature is optional, but the rest of this tutorial assumes you have done this.
At line 146 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 149 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 running the following:
At line 153 changed 1 line.
!!Add testSearch method to Action Test [#5]
!!Add testSearch() method to Action Test [#5]
At line 156 removed 2 lines.
;:%%(color: blue)''I copied the testSearch method from UserActionTest.java and changed the "User" stuff to "Person".''
At line 162 changed 1 line.
addRequestParameter("action", "Search");
addRequestParameter("method", "Search");
At line 167 changed 1 line.
assertTrue(getRequest().getAttribute(Constants.PERSON_LIST) != null);
assertNotNull(getRequest().getAttribute(Constants.PERSON_LIST));
At line 184 changed 1 line.
Now save all your changes. You won't be able to run __ant test-cactus -Dtestcase=PersonAction__ yet since ''PersonAction.search()'' does not exist (yet). Let's add it.
Now save all your changes. You won't be able to run __ant test-cactus -Dtestcase=PersonAction__ yet since ''PersonAction.search()'' does not exist (yet).
At line 191 changed 1 line.
* @struts.action-forward name="list" path=".personList"
* @struts.action-forward name="list" path="/WEB-INF/pages/personList.jsp"
At line 208 removed 1 line.
// Exceptions are caught by ActionExceptionHandler
At line 218 changed 1 line.
Now if you run __ant test-cactus -Dtestcase=PersonAction__, you will get an error that the .personList definition does not exist. Or, at least that is what the following error is trying to say:
Make sure Tomcat is stopped and run __ant test-cactus -Dtestcase=PersonAction__.
At line 220 changed 4 lines.
{{{
Testcase: testSearch(org.appfuse.webapp.action.PersonActionTest): FAILED
was expecting '/appfuse/.personList' but received '/appfuse.personList'
}}}
__Nice!__
%%(color:green)BUILD SUCCESSFUL\\
Total time: 1 minute 26 seconds%%
At line 226 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'' folder of your project. Copy this file to web/pages/personList.jsp. 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=PersonForm__. This will generate a PersonFormList.jsp in extras/viewgen/build.
There should already be a personList.jsp file in ''web/pages''. If not, you can create it using viewgen. From the command-line, navigate to extras/viewgen and run __ant -Dform.name=PersonForm__. This will generate a PersonFormList.jsp in extras/viewgen/build.
At line 238 changed 1 line.
Now we need to add this to the metadata/web/global-forwards.xml, as well as one for viewing the list. This way, they will get included in our struts-config.xml file.
Add this to the metadata/web/global-forwards.xml, as well as one for viewing the list. This way, they will get included in our struts-config.xml file.
At line 246 changed 1 line.
In the first &lt;button&gt; you find in personList.jsp, we need to populate another forward - this time to the Add screen. Make sure your button's onclick event matches the following:
The template you used to create this JSP has the column for the id property hard-coded, so XDoclet adds it twice. To remove this from personList.jsp, delete the following from this file:
At line 250 removed 7 lines.
onclick="location.href='<html:rewrite forward="editPerson"/>'">
}]
The template we used to create this JSP has the column for the id property hard-coded, so XDoclet adds it twice. We need to remove this from personList.jsp - so delete the following from this file:
[{Java2HtmlPlugin
At line 271 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 273 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 294 changed 1 line.
{{personList.heading}} will be put into an &lth1&gt; tag before any page content. At this point, your PersonActionTest should succeed. Save all your changes, navigate to the basedir of your project and try it by executing __ant test-cactus -Dtestcase=PersonAction__.
{{personList.heading}} will be put into an &lth1&gt; tag before any page content.
At line 296 changed 5 lines.
%%(color:green)
''__Sweet!__''\\
BUILD SUCCESSFUL\\
Total time: 1 minute 0 seconds
%%
At this point, you should be able to run __ant clean deploy__, start Tomcat and view this page in your browser at [http://localhost:8080/appfuse/editPerson.html?method=Search].
At line 302 removed 2 lines.
At this point, you should be able to view this page in your browser at [http://localhost:8080/appfuse/editPerson.html?action=Search].
At line 329 changed 2 lines.
{{{<verifytitle description="display Person List"
text=".*$(personList.title}.*" regex="true"/>}}}
{{{<verifytitle description="display Person List" text=".*$(personList.title}.*" regex="true"/>}}}
At line 332 changed 1 line.
We use "viewPeople" instead of "list" so that the search method will be executed, rather than simply forwarding to the .personList definition (which the "list" forward points to).
We use "viewPeople" instead of "list" so that the search method will be executed, rather than simply forwarding to the personForm.jsp (which the "list" forward points to).
At line 334 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, create a new JSP test in test/web/web-tests.xml:
At line 345 changed 1 line.
<invoke description="click View People link" url="/editPerson.html?action=Search"/>
<invoke description="click View People link" url="/editPerson.html?method=Search"/>
At line 399 added 2 lines.
%%note NOTE: The other links in mainMenu.jsp don't use <html:link> so this JSP can be shared among the various web framework implementations in AppFuse (i.e. Spring MVC and WebWork).

Back to ValidationAndList, or to the Page History.