| At line 14 changed 2 lines. |
| * [5] Add ''testSearch'' methods to Action Test |
| * [6] Add ''search '' method to Action |
| * [5] Create PersonControllerTest |
| * [6] Create PersonController |
| At line 89 changed 1 line. |
| 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]. |
| 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]. One thing you might notice is that the "First Name" and "Last Name" labels now have asterisks next to them - indicating required fields. This is achieved with a LabelTag that looks up the validation rules from Commons Validator. |
| At line 100 changed 1 line. |
| [validation-required-nojs.png] |
| [ValidationAndList/validation-required-nojs.png] |
| At line 102 removed 7 lines. |
|
| If you don't see these validation errors, there are a couple possibilities: |
|
| * The form saves with a success message, but the firstName and lastName fields are now blank. |
| ;:''This is because the <html:form> in web/pages/personForm.jsp has action="editPerson" - make sure it has __action="savePerson"__.'' |
| * You click save, but a blank page appears. |
| ;:''The blank page indicates that the "input" attribute of you "savePerson" forward is incorrectly configured. Make sure it relates to a local or global action-forward. In this example, it should be __input="edit"__, which points to the .personDetail tile's definition. From my experience, the input's value must be a forward, not a path to an action.'' |
| At line 153 changed 2 lines. |
| !!Add testSearch method to Action Test [#5] |
| Open test/web/**/action/PersonActionTest.java and add the following method: |
| !!Create PersonControllerTest [#5] |
| 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. |
| At line 156 changed 1 line. |
| ;:%%(color: blue)''I copied the testSearch method from UserActionTest.java and changed the "User" stuff to "Person".'' |
| To begin, create test/web/**/action/PersonControllerTest.java (extends BaseControllerTestCase) and add the following method: |
| At line 152 added 2 lines. |
| ;:%%(color: blue)''I copied the testHandleRequest method from UserControllerTest.java and changed the "User" stuff to "Person".'' |
|
| At line 160 changed 9 lines. |
| public void testSearch() { |
| setRequestPathInfo("/editPerson"); |
| addRequestParameter("action", "Search"); |
| actionPerform(); |
|
| verifyForward("list"); |
|
| assertTrue(getRequest().getAttribute(Constants.PERSON_LIST) != null); |
| verifyNoActionErrors(); |
| public void testHandleRequest() throws Exception { |
| PersonController c = (PersonController) ctx.getBean("personController"); |
| ModelAndView mav = |
| c.handleRequest((HttpServletRequest) null, |
| (HttpServletResponse) null); |
| Map m = mav.getModel(); |
| assertNotNull(m.get(Constants.USER_LIST)); |
| assertEquals(mav.getViewName(), "personList"); |
| At line 172 changed 1 line. |
| This class will not compile until you 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. |
| 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-web -Dtestcase=PersonController__ yet since ''PersonController'' does not exist (yet). Let's add it. |
| At line 186 changed 2 lines. |
| !!Add search method to Action [#6] |
| Open src/web/**/action/PersonAction.java and add the following XDoclet tag at the top - to forward to our list screen. |
| !!Create PersonController [#6] |
| Create src/web/**/action/PersonController.java. It should implement {{org.springframework.web.servlet.mvc.Controller}} and resemble the following: |
| At line 191 changed 2 lines. |
| * @struts.action-forward name="list" path=".personList" |
| }] |
| package org.appfuse.webapp.action; |
| At line 194 changed 1 line. |
| Now add the search method to the body of the PersonAction class. |
| import javax.servlet.http.HttpServletRequest; |
| import javax.servlet.http.HttpServletResponse; |
| At line 196 changed 1 line. |
| ;:%%(color: blue)''I used UserAction.search() as a template for this method.'' |
| import org.apache.commons.logging.Log; |
| import org.apache.commons.logging.LogFactory; |
| At line 198 changed 1 line. |
| [{Java2HtmlPlugin |
| import org.appfuse.Constants; |
| import org.appfuse.model.Person; |
| import org.appfuse.service.PersonManager; |
| At line 200 changed 4 lines. |
| public ActionForward search(ActionMapping mapping, ActionForm form, |
| HttpServletRequest request, |
| HttpServletResponse response) |
| throws Exception { |
| import org.springframework.web.servlet.ModelAndView; |
| import org.springframework.web.servlet.mvc.Controller; |
|
|
| public class PersonController implements Controller { |
| private static Log log = LogFactory.getLog(PersonController.class); |
| private PersonManager mgr = null; |
|
| public void setPersonManager(PersonManager userManager) { |
| this.mgr = userManager; |
| } |
|
| public ModelAndView handleRequest(HttpServletRequest request, |
| HttpServletResponse response) |
| throws Exception { |
| At line 205 changed 1 line. |
| log.debug("Entering 'search' method"); |
| log.debug("entering 'handleRequest' method..."); |
| At line 208 changed 7 lines. |
| // Exceptions are caught by ActionExceptionHandler |
| PersonManager mgr = (PersonManager) getBean("personManager"); |
| List people = mgr.getPeople(null); |
| request.setAttribute(Constants.PERSON_LIST, people); |
|
| // return a forward to the person list definition |
| return mapping.findForward("list"); |
| return new ModelAndView("personList", Constants.PERSON_LIST, |
| mgr.getPeople(new Person())); |
| At line 220 added 1 line. |
| } |
| 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: |
| Now add a definition to web/WEB-INF/action-servlet.xml for this Controller. |
| At line 220 removed 12 lines. |
| {{{ |
| Testcase: testSearch(org.appfuse.webapp.action.PersonActionTest): FAILED |
| was expecting '/appfuse/.personList' but received '/appfuse.personList' |
| }}} |
|
| !!Create personList.jsp and Canoo test [#7] |
| 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. |
|
| Once personList.jsp exists in ''web/pages'', open it for editing. |
|
| At the top of the file is a <bean:struts> tags that exposes the edit screen's forward as a page-scoped variable. This should already have a value of "editPerson". |
|
| At line 234 changed 2 lines. |
| <%-- For linking to edit screen --%> |
| <bean:struts id="editURL" forward="editPerson"/> |
| <bean id="personController" class="org.appfuse.webapp.action.PersonController"> |
| <property name="personManager"><ref bean="personManager"/></property> |
| </bean> |
| 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. |
| 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 242 changed 2 lines. |
| <forward name="editPerson" path="/editPerson.html"/> |
| <forward name="viewPeople" path="/editPerson.html?action=Search"/> |
| <prop key="/people.html">personController</prop> |
| At line 246 changed 1 line. |
| In the first <button> 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: |
| Now we need to create a Tile's definition for the personList.jsp page. |
| At line 248 changed 1 line. |
| [{Java2HtmlPlugin |
| !!Create personList.jsp and Canoo test [#7] |
| 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. |
| At line 250 changed 2 lines. |
| onclick="location.href='<html:rewrite forward="editPerson"/>'"> |
| }] |
| Once personList.jsp exists in ''web/pages'', open it for editing. |
| At line 258 changed 1 line. |
| titleKey="personForm.id"/> |
| titleKey="person.id"/> |
| At line 273 changed 1 line. |
| ;:%%(color: blue)''I usually copy an existing list definition - i.e. .userList.'' |
| ;:%%(color: blue)''I usually copy an existing list definition - i.e. userList.'' |
| At line 278 changed 1 line. |
| <definition name=".personList" extends=".mainMenu"> |
| <definition name="personList" extends="mainMenu"> |
| At line 294 changed 1 line. |
| {{personList.heading}} will be put into an <h1> 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 <h1> tag before any page content. At this point, you could be able to run "ant deploy", start Tomcat and [open the list screen in your browser|http://localhost:8080/appfuse/people.html]. |
| At line 296 removed 8 lines. |
| %%(color:green) |
| ''__Sweet!__''\\ |
| BUILD SUCCESSFUL\\ |
| Total time: 1 minute 0 seconds |
| %% |
|
| At this point, you should be able to view this page in your browser at [http://localhost:8080/appfuse/editPerson.do?action=Search]. |
|