Raible's Wiki
Raible Designs AppFuseHomepage- Korean - Chinese - Italian - Japanese QuickStart Guide User Guide Tutorials Other ApplicationsStruts ResumeSecurity Example Struts Menu
Set your name in
UserPreferences
Referenced by
JSPWiki v2.2.33
Hide Menu |
This is version 27.
It is not the current version, and thus it cannot be edited. Part III: Creating Controllers and JSPs - A HowTo for creating Spring Controllers and JSPs in the AppFuse architecture.
About this TutorialThis tutorial will show you how to create a Spring Controller and JSP. It'll also demonstrate writing a JUnit Test to test the Controller. The Controller we create will talk to the PersonManager we created in the Creating Managers tutorial. This tutorial will simplify everything - we will not actually be rendering any data or making the UI look pretty. The next tutorial will show you how to integrate your new JSPs into your webapp.
Let's get started by creating a new Controller and JSP in AppFuse's architecture. If you haven't installed the Spring MVC module at this point, do so by running ant install-springmvc. Table of Contents
Create a skeleton JSP using XDoclet [#1]In this step, we'll generate a skeleton or our JSP for displaying information from the Person object. I say skeleton because it'll just be the <form> itself. It will contain table rows with Spring's "bind" tags for each property in Person.java. The tool that we use to do this was written by Erik Hatcher. It's basically just a single class (FormTagsHandler.java) and a couple of XDoclet templates (FormKeys.xdt and Form_jsp.xdt). All these files are located in extras/viewgen.Here are the simple steps to generating the JSP and a properties file containing the labels for the form elements:
# -- person form -- person.firstName=First Name person.id=Id person.lastName=Last Name
WARNING: There is a bug in the Spring version of viewgen. You can fix it by changing "<c:url page" to <c:url value" in extras/spring/extras/viewgen/src/Form_jsp.xdt.
The web application security for AppFuse specifies that all *.html url-patterns should be protected. This guarantees 1) all Controllers are protected, and 2) you must go through a Controller to get to a JSP (or at least the ones in pages). At this point, you won't be able to view the JSP in your browser because spring's "bind" tags require that the JSP is invoked from the DispatchServlet. Therefore, we need to create a Controller for this JSP, and we should practice TDD and write our Test before we write our Controller. Create PersonFormControllerTest to test our Controller [#2]To create a JUnit Test for our Controller, start by creating a PersonControllerTest.java file in the test/web/**/action directory.
Nothing will compile at this point (ant compile) since we need to create the PersonFormController that we're referring to in this test. Create PersonFormController [#3]Now we have to create a Controller to talk to our Manager and retrieve/save our data. In src/web/**/action, create a PersonFormController.java file with the following contents:
We're not putting much in PersonController at this point because we just want to 1) render the JSP and 2) verify our Test runs. Now we need to add a url-mapping for this controller in the web/WEB-INF/action-servlet.xml file. In the block below, the new line is at the bottom, with <prop key="/editPerson.html">:
We also need to add the <bean> definition for personFormController in this same file:
Everything is almost done for this tutorial, let's get to running our tests! Run the PersonFormControllerTest [#4]Now, if you stop Tomcat, save everything, cd ../.. to the base directory and run ant test-web -Dtestcase=PersonFormController, the "testEdit" method should pass and you should see something like the following in your console: [echo] Testing web... [copy] Copying 1 file to C:\Source\appfuse-springmvc\build\appfuse\WEB-INF [junit] [appfuse] DEBUG [main] PersonFormControllerTest.testEdit(27) | testing edit... [junit] [appfuse] DEBUG [main] AbstractFormController.showNewForm(268) | Displaying new form [junit] Testsuite: org.appfuse.webapp.action.PersonFormControllerTest [junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0.297 sec If you see the message below - Congratulations you've written your first Spring Controller! BUILD SUCCESSFULTotal time: 11 seconds Next Up: Part IV: Configuring Tiles and FormController - Integrating personForm.jsp with Tiles, adding methods for saving, customizing the JSP so it looks good and finally - writing a WebTest to test the JSPs functionality.
|