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 9.
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 [#2]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
We copy the JSPs to the web folder instead of web/pages because the pages directory ends up in WEB-INF/pages when the application is packaged into a WAR file. This is a recommended practice when building secure web applications.
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). All this is to say that putting the personForm.jsp in the web folder will allow us to view it without making a Tile for it. We'll get to that in the next tutorial. 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 a new ControllerTest to test our Controller [#3]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 a new Controller [#4]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">:
Now we need to change the "action" attribute in the <personForm.jsp> to be action="editPerson".
Everything is almost done for this tutorial, let's get to running our tests! Display the JSP in a browser and run the ActionTest [#5]To test the JSP visually in your browser, save everything, run ant deploy, start Tomcat, and navigate to http://localhost:8080/appfuse/personForm.jsp. You should see something similar to the following image in your browser:
Now, if you stop Tomcat and run ant test-cactus -Dtestcase=PersonAction, that should work too! BUILD SUCCESSFULTotal time: 51 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.
|