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 1.
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 two Spring Controllers - one for a list screen, and one for the form. It'll also demonstrate writing a JUnit Test to test the Controllers, and two JSPs. 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 Action and JSP in AppFuse's architecture. 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 PersonForm. I say skeleton because it'll just be the <form> itself. It will contain table rows and Struts' <html:text> tags for each property in PersonForm.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 StrutsForm_jsp.xdt). All these files are located in extras/jspgen.Here are the simple steps to generating the JSP and a properties file containing the labels for the form elements:
# -- person form -- personForm.firstName=First Name personForm.id=Id personForm.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 Actions are protected, and 2) you must go through an Action 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 the <html:form> tag in personForm.jsp has action="savePerson" - and this action-mapping doesn't exist (yet) in struts-config.xml. You can try it yourself (cd ../.. first) by setting up AppFuse on Tomcat using ant setup-db setup-tomcat deploy. Then, start Tomcat and then go to http://localhost:8080/appfuse/personForm.jsp. This will result in the following error: javax.servlet.jsp.JspException: Cannot retrieve mapping for action /savePersonTherefore, we need to create an Action for this JSP, and we should probably create a Test before we write our Action. Create a new ActionTest to test our Action [#3]To create a StrutsTestCase Test for our Action, start by creating a PersonActionTest.java file in the test/web/**/action directory.
If you did copy UserActionTest, make sure and change UserFormEx to PersonForm. The reason for UserFormEx is to support indexed properties and non-struts validation. Since the UserForm is generated, it's not very feasible to do it in the User.java object. When we do create an Action (in [step 4]), we're only going to create an execute method, rather than all the different CRUD methods. So let's just test that method to start.
Everything should compile at this point (ant compile-web) since we're not referring to the PersonAction directly in our test. However, if you try to run ant test-cactus -Dtestcase=PersonAction, it won't work (make sure Tomcat is not running if you decide to try this). Create a new Action [#4]Now we have to create an Action (a.k.a. the Controller) to talk to our Manager and retrieve/save our data. In src/web/**/action, create a PersonAction.java file with the following contents:
We're not putting much in PersonAction at this point because we just want to 1) render the JSP and 2) verify our Test runs. The XDoclet tags (beginning with @struts.action) will generate the following XML in the build/appfuse/WEB-INF/struts-config.xml file (when you run ant webdoclet):
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 Action CRUD methods - Integrating personForm.jsp with Tiles, replacing execute with different CRUD methods (add, edit, delete), customizing the JSP so it looks good and finally - writing a WebTest to test the JSPs functionality.
|