??xml version="1.0" encoding="utf-8" standalone="yes"?>
]]>
Step 20 – Add unit test for the SpringappController
Before we create any unit tests, we want to prepare Ant and our build script to be able to handle this. Ant has a built in JUnit target, but we need to add junit.jar to Ant's lib directory. I used the one that came with the Spring distribution spring-framework-1.2/lib/junit/junit.jar. Just copy this file to the lib directory in your Ant installation. I also added the following target to our build script.
<target name="junit" depends="build" description="Run JUnit Tests"> <junit printsummary="on" fork="false" haltonfailure="false" failureproperty="tests.failed" showoutput="true"> <classpath refid="master-classpath"/> <formatter type="brief" usefile="false"/> <batchtest> <fileset dir="${build.dir}"> <include name="**/Test*.*"/> </fileset> </batchtest> </junit> <fail if="tests.failed"> |
Now I add a new sub-directory in the src directory that I name tests. This directory will, as you might have guessed, contain all the unit tests.
After all this, we are ready to start writing the first unit test. The SpringappController depends on both the HttpServletRequest, HttpServletResponse and our application context. Since the controller does not use the request or the response, we can simply pass in null for these objects. If that was not the case, we could create some mock objects using EasyMock that we would pass in during our test. The application context can be loaded outside of a web server environment using a class that will load an application context. There are several available, and for the current task the FileSystemXmlApplicationContext works fine.
springapp/src/tests/TestSpringappController.java |
package tests; |
The only test is a call to handleRequest, and we check the products that are returned in the model. In the setUp method, we load the application context that I have copied into a WEB-INF directory in the src/tests directory. I create a copy just so this file will work during tests with a small set of beans necessary for running the tests. So, copy springapp/war/WEB-INF/springapp-servlet.xml to springapp/src/tests/WEB-INF directory. You can then remove the “messageSource”, "urlMapping" and "viewResolver" bean entries since they are not needed for this test.
springapp/src/tests/WEB-INF/springapp-servlet.xml |
<?xml version="1.0" encoding="UTF-8"?> |
When you run this test, you should see a lot of log messages from the loading of the application context.
Step 21 – Add unit test and new functionality for ProductManager
Next I add a test case for the ProductManager, and I also add a test for a new method to increase the prices that I am planning on adding to the ProductManager.
springapp/src/tests/TestProductManager .java |
package tests; |
For this test, there is no need to create an application context. I just create a couple of products in the setUp method and add them to the product manager. I add tests for both getProducts and increasePrice. The increasePrice method is a cross the board increase based on the percentage passed in to the method. I modify the ProductManager class to implement this new method.
springapp/src/bus/ProductManager.java |
package bus; |
Next I build and run the tests. As you can see, this test is just like any regular test – the business classes don't depend on any of the servlet classes so these classes are very easy to test.
Step 22 – Adding a form
To provide an interface in the web application, I add a form that will allow the user to enter a percentage value. This form uses a tag library named “spring” that is provided with the Spring Framework. We have to copy this file from the Spring distribution spring-framework-1.2/dist/spring.tld to the springapp/war/WEB-INF directory. Now we must also add a <taglib> entry to web.xml.
springapp/war/WEB-INF/web.xml |
<?xml version="1.0" encoding="UTF-8"?> |
We also have to declare this taglib in a page directive in the jsp file. We declare a form the normal way with a <form> tag and an <input> text field and a submit button.
springapp/war/WEB-INF/jsp/priceincrease.jsp |
<%@ include file="/WEB-INF/jsp/include.jsp" %> |
The <spring:bind> tag is used to bind an <input> form element to a command object PriceIncrease.java, that is used together with the form. This command object is later passed in to the validator and if it passes validation it is passed on to the controller. The ${status.errorMessage} and ${status.value} are special variables declared by the framework that can be used to display error messages and the current value of the field.
springapp/src/bus/PriceIncrease.java |
package bus; |
This is a very simple JavaBean class, and in our case there is a single property with a getter and setter. The validator class gets control after the user presses submit. The values entered in the form will be set on the command object by the framework. The method validate is called and the command object and an object to hold any errors are passed in.
springapp/src/bus/PriceIncreaseValidator.java |
package bus; |
Now we need to add an entry in the springapp-servlet.xml file to define the new form and controller. We define properties for command object and validator. We also specify two views, one that is used for the form and one that we will go to after successful form processing. The latter which is called the success view can be of two types. It can be a regular view reference that is forwarded to one of our JSP pages. One disadvantage with this approach is, that if the user refreshes the page, the form data is submitted again, and you would end up with a double priceincrease. An alternative way is to use a redirect, where a response is sent back to the users browser instructing it to redirect to a new url. The url we use in this case can't be one of our JSP pages, since they are hidden from direct access. It has to be a url that is externally reachable. I have choosen to use 'hello.htm' as my redirect url. This url maps to the 'hello.jsp' page, so this should work nicely.
springapp/war/WEB-INF/springapp-servlet.xml |
<?xml version="1.0" encoding="UTF-8"?> |
Next, let's take a look at the controller for this form. The onSubmit method gets control and does some logging before it calls the increasePrice method on the ProductManager object. It then returns a ModelAndView passing in a new instance of a RedirectView created using the url for the successView.
springapp/src/web/PriceIncreaseFormController.java |
package web; |
We are also adding some messages to the messages.properties resource file.
springapp/war/WEB-INF/classes/messages.properties |
title=SpringApp |
Finally, we have to provide a link to the priceincrease page from the hello.jsp.
springapp/war/WEB-INF/jsp/hello.jsp |
<%@ include file="/WEB-INF/jsp/include.jsp" %> |
Compile and deploy all this and after reloading the application we can test it. This is what the form looks like with errors displayed.
This is what we have to start with.
An introduction page index.jsp.
A DispatcherServlet with a corresponding springapp-servlet.xml configuration file.
A controller springappController.java.
A view hello.jsp.
We will now improve on these parts to build a more useful application.
Step 13 – Improve index.jsp
We will make use of JSP Standard Tag Library (JSTL) so I will start by copying the JSTL files we need to our WEB-INF/lib directory. Copy jstl.jar from the 'spring-framework-1.2/lib/j2ee' directory and standard.jar from the 'spring-framework-1.2/lib/jakarta-taglibs' directory to the springapp/war/WEB-INF/lib directory. I am also creating a “header” file that will be included in every JSP page that I'm going to write. This will make development easier and I will be sure that I have the same definitions in all JSPs. I am going to put all JSPs in a directory named jsp under the WEB-INF directory. This will ensure that only the controller has access to the views - it is not possible to get to these pages by entering them directly as a URL in the browser. This strategy might not work in all application servers and if this is the case with the one you are using, just move the jsp directory up a level. You would then use springapp/war/jsp as the directory instead of springapp/war/WEB-INF/jsp in all the code examples that will follow.
springapp/war/WEB-INF/jsp/include.jsp |
<%@ page session="false"%> |
Now we can change index.jsp to use this include and since we are using JSTL we can use the <c:redirect> tag for redirecting to our Controller. This ties the index.jsp into our application framework.
springapp/war/index.jsp |
<%@ include file="/WEB-INF/jsp/include.jsp" %> |
Step 14 – Improve the view and the controller
I am going to move the view hello.jsp to the WEB-INF/jsp directory. The same include that was added to index.jsp gets added to hello.jsp. I also add the current date and time as output that I will retrieve from the model, passed to the view, using the JSTL <c:out> tag.
springapp/war/WEB-INF/jsp/hello.jsp |
<%@ include file="/WEB-INF/jsp/include.jsp" %> |
For SpringappController.java there are a few changes we need to make. Change the view to WEB-INF/jsp/hello.jsp since we moved the file to this new location. Also add a string containing the current data and time as the model.
springapp/src/SpringappController.java |
import org.springframework.web.servlet.mvc.Controller; |
Now we are ready to try this after we build and deploy this new code. We enter http://localhost:8080/springapp in a browser and that should pull up index.jsp, which should redirect to hello.htm, which in turn gets us to the controller that sends the data and time to the view.
Step 15 – Decouple the view and the controller
Right now the controller specifies the full path of the view, which creates an unnecessary dependency between the controller and the view. Ideally we would like to map to the view using a logical name, allowing us to switch the view without having to change the controller. You can set this mapping in a properties file if you like using a ResourceBundleViewResolver and a SimpleUrlHandlerMapping class. If your mapping needs are simple it is easier to just set a prefix and a suffix on the InternalResourceViewResolver. The latter approach is the one that I will implement now, so I modify the springapp-servlet.xml and include this viewResolver entry. I have elected to use a JstlView which will enable us to use JSTL in combination with message resource bundles and it will also support internationalization.
springapp/war/WEB-INF/springapp-servlet.xml |
<?xml version="1.0" encoding="UTF-8"?> |
So now I can remove the prefix and suffix from the view name in the controller.
springapp/src/SpringappController.java |
import org.springframework.web.servlet.mvc.Controller; |
Compile and deploy and the application should still work.
Step 16 – Add some classes for business logic
So far our application is not very useful. I would like to add a little bit of business logic in form of a Product class and a class that will manage all the products. I name this management class ProductManager. In order to separate the web dependent logic from the business logic I will create two separate packages for the Java source – web and bus. If this was an application for a real company I would name the packages something like com.mycompany.web and com.mycompany.bus, but since this is just a demo application I will keep the package names real short. The Product class is implemented as a JavaBean – it has the default constructor (automatically provided if we don't specify any constructors) and getters and setters for the two instance variables description and price. I also make it Serializable, not necessary for our application, but could come in handy later on if we have to pass this class between different application layers.
springapp/src/bus/Product.java |
package bus; |
The ProductManager holds a List of Products, and again this this class is implemented as a JavaBean.
springapp/src/bus/ProductManager.java |
package bus; |
Next, I modify the SpringappController to hold a reference to this ProductManager class. As you can see, it is now in a separate package called web – remember to move the source to this new location. I also add code to have the controller pass some product information to the view. The getModelAndView now returns a Map with both the date and time and the product manager reference.
springapp/src/web/SpringappController.java |
package web; |
Step 17 – Modify the view to display business data and add support for message bundle
Using the JSTL <c:forEach> tag, I add a section that displays product information. I have also replaced the title, heading and greeting text with a JSTL <fmt:message> tag that pulls the text to display from a provided 'message' source – I will show this source in a later step.
springapp/war/WEB-INF/jsp/hello.jsp |
<%@ include file="/WEB-INF/jsp/include.jsp" %> |
Step 18 – Add some test data to automatically populate some business objects
I am not going to add any code to load the business objects from a database just yet. Instead, we can “wire up” a couple of instances using Spring's bean and application context support. I will simply put the data I need as a couple of bean entries in springapp-servlet.xml. I will also add the messageSource entry that will pull in the messages resource bundle ('messages.properties') that I will create in the next step.
springapp/war/WEB-INF/springapp-servlet.xml |
<?xml version="1.0" encoding="UTF-8"?> |
Step 19 – Add the message bundle and a 'clean' target to build.xml
I create a 'messages.properties' file in the war/WEB-INF/classes directory. This properties bundle so far has three entries matching the keys specified in the <fmt:message> tags that we added to the hello.jsp.
springapp/war/WEB-INF/classes/messages.properties |
title=SpringApp |
Since we moved some source files around, it makes sense to add a 'clean' and an 'undeploy' target to the build scripts. I add the following entries to the build.xml file.
<target name="clean" description="Clean output directories"> <delete> <fileset dir="${build.dir}"> <include name="**/*.class"/> </fileset> </delete> </target> <target name="undeploy" description="Un-Deploy application"> <delete> <fileset dir="${deploy.path}/${name}"> <include name="**/*.*"/> </fileset> </delete> </target> |
Now stop the Tomcat server, run the clean, undeploy and deploy targets. This should remove all old class files, re-build the application and deploy it. Start up Tomcat again and you should see the following:
Prerequisites:
Java SDK (I am currently using version 1.4.2)
Ant (using version 1.6.2)
Apache Tomcat (using version 5.0.28)
You should also be reasonably comfortable using the above software.
I am not going to cover a lot of background information or theory in this document -- there are plenty of books available that covers this in depth. Instead we will dive right into developing the application.
Step 1 – development directory
We are going to need a place to keep all the source and other files we will be creating, so I create a directory that I name 'springapp'. You can place this directory in your home folder or in some other location. I created mine in a 'projects' directory that I already had in my home directory so the full path to my directory is '/Users/trisberg/projects/springapp'. Inside this directory I create a 'src' directory to hold all Java source files. Then I create another directory that I name 'war'. This directory will hold everything that should go into the WAR file, that we would use to deploy our application. All source files other than Java source, like JSPs and configuration files, belongs in this directory.
Step 2 – index.jsp
I will start by creating a JSP page named 'index.jsp' in the war directory. This is the entry point for our application.
springapp/war/index.jsp |
<html> |
Just to have a complete web application, I create a web.xml in a WEB-INF directory that I create under the war directory.
springapp/war/WEB-INF/web.xml |
<?xml version="1.0" encoding="UTF-8"?> |
Step 3 – deploying the application to Tomcat
Next, I write an Ant build script that we are going to use throughout this document. There are tasks for building and deploying the application. A separate build script contains the app server specific tasks There are also tasks for controlling the application under Tomcat.
springapp/build.xml |
<?xml version="1.0"?> |
This script now contains all the targets that we are going to need to make our development efforts easier. I am not going to cover this script in detail since most if not all of it is pretty much standard Ant and Tomcat stuff. You can just copy the above build file and put it at the root of your development directory tree. We also need a build.properties file that you should customize to match your server installation. This file belongs in the same directory as the build.xml file.
springapp/build.properties |
# Ant properties for building the springapp |
If you are on a system where you are not the owner of the Tomcat install, then the Tomcat owner must either grant you full access to the webapps directory or the owner must create a new directory named 'springapp' in the 'webapps' directory of the Tomcat installation, and also give you full rights to deploy to this newly created directory. On Linux I run the command chmod a+rwx springapp to give everybody full rights to this directory.
If you are using a different web application server, then you can remove the Tomcat specific tasks at the end of the build script. You will have to rely on your server's hot deploy feature, or you will have to stop and start your application manually.
Now I run Ant to make sure that everything is working OK. You should have your current directory set to the 'springapp' directory.
[trisberg@localhost springapp]$ ant Buildfile: build.xml usage: [echo] springapp build file [echo] ----------------------------------- [echo] Available targets are: [echo] build --> Build the application [echo] deploy --> Deploy application as directory [echo] deploywar --> Deploy application as a WAR file [echo] install --> Install application in Tomcat [echo] reload --> Reload application in Tomcat [echo] start --> Start Tomcat application [echo] stop --> Stop Tomcat application [echo] list --> List Tomcat applications BUILD SUCCESSFUL Total time: 2 seconds |
Last action here is to do the actual deployment. Just run Ant and specify 'deploy' or 'deploywar' as the target.
[trisberg@localhost springapp]$ ant deploy Buildfile: build.xml build: |
Step 4 – Test the application
Let's just quickly start Tomcat and make sure that we can access the application. Use the 'list' task from our build file to see if Tomcat has picked up the new application.
[trisberg@localhost springapp]$ ant list Buildfile: build.xml list: |
If it is not listed, use the 'install' task to get the application installed in Tomcat.
[trisberg@localhost springapp]$ ant install Buildfile: build.xml install: [install] OK - Installed application at context path /springapp BUILD SUCCESSFUL Total time: 2 seconds |
Now open a browser and browse to http://localhost:8080/springapp/index.jsp.
Step 5 – Download Spring distribution
If you have not already downloaded the Spring Framework Release file, now is the time to do so. I am currently using 'spring-framework-1.2-with-dependencies.zip' that can be downloaded from www.springframework.org/download.html. I unzipped this file in my home directory. We are going to use several files from this download later on.
This completes the setup of the environment that is necessary, and now we can start actually developing our Spring Framework MVC application.
Step 6 – Modify web.xml in WEB-INF directory
Go to the 'springapp/war/ WEB-INF' directory. Modify the minimal 'web.xml' file that we created earlier. Now we will modify it to suit our needs. We define a DispatcherServlet that is going to control where all our request are routed based on information we will enter at a later point. It also has a standard servlet-mapping entry that maps to the url patterns that we will be using. I have decided to let any url with an '.htm' extension be routed to the 'springapp' dispatcher.
springapp/war/WEB-INF/web.xml |
<?xml version="1.0" encoding="UTF-8"?> |
Next, create a file called 'springapp-servlet.xml' in the springapp/war/WEB-INF directory (you can copy an example of this file from the Spring distributions sample/skeletons/webapp-minimal directory). This is the file where definitions used by the DispatcherServlet should be entered. It is named based on the servlet-name from web.xml with '-servlet' appended. This is a standard naming convention used in the Spring Framework. Now, add a bean entry named springappController and make the class SpringappController. This defines the controller that our application will be using. We also need to add a url mapping so the DispatcherServlet knows which controller should be invoked for different url:s.
springapp/war/WEB-INF/springapp-servlet.xml |
<?xml version="1.0" encoding="UTF-8"?> |
Step 7 – Copy jars to WEB-INF/lib
First create a 'lib' directory in the 'war/WEB-INF' directory. Then, from the Spring distribution, copy spring.jar (spring-framework-1.2/dist/spring.jar) to the new war/WEB-INF/lib directory. Also copy commons-logging jars to the war/WEB-INF/lib directory (spring-framework-1.2/lib/jakarta-commons/commons-logging.jar). We are also going to need a log4j jar. Copy log4j-1.2.9.jar to the war/WEB-INF/lib directory (spring-framework-1.2/lib/log4j/log4j-1.2.9.jar). These jars will be deployed to the server and they are also used during the build process.
Step 8 – Create your Controller
Create your Controller – I named mine SpringappController.java and placed it in the springapp/src directory.
springapp/src/SpringappController.java |
import org.springframework.web.servlet.mvc.Controller; |
This is as basic a Controller as you can use. We will be expanding this later on, and we will also later on extend some provided abstract base implementations. The Controller “handles” the request and returns a ModelAndView. We have not yet defined any Views, so right now there is nothing to do.
Step 9 – Build the Application
Run the 'build' task of the build.xml. Hopefully the code compiles OK.
[trisberg@localhost springapp]$ ant build Buildfile: build.xml build: [javac] Compiling 1 source file to /Users/trisberg/projects/springapp/war/WEB-INF/classes BUILD SUCCESSFUL Total time: 2 seconds |
Step 10 – Copy and modify log4j.properties
The Spring Framework uses log4j for logging so we have to create a configuration file for log4j. Copy the log4j.properties from the sample Petclinic application (spring-framework-1.2/samples/petclinic/war/WEB-INF/log4j.properties) to the war/WEB-INF/classes directory (this directory should have been created in the previous step). Now uncomment or modify the log4j.rootCategory property and change the name and location of the logfile that will be written. I decided to have it written to the same directory as all other Tomcat logs.
springapp/war/WEB-INF/classes/log4j.properties |
# For JBoss: Avoid to setup Log4J outside $JBOSS_HOME/server/default/deploy/log4j.xml! |
Step 11 – Deploy Application
Run the 'deploy' task and then the 'stop' and 'start' tasks of the build.xml. This will force a reload of the application. We have to check the Tomcat logs for any deployment errors – there could be typos in the above xml files or there could be missing classes or jar files. This is an example of what it should look like. (/Users/trisberg/jakarta-tomcat-5.0.28/logs/springapp.log)
2005-04-24 14:58:18,112 INFO [org.springframework.web.servlet.DispatcherServlet] - Initializing servlet 'springapp' |
Step 12 – Create a View
Now it is time to create our first view. I will use a JSP page that I decided to name hello.jsp. I'll put it in the war directory to begin with.
springapp/war/hello.jsp |
<html> |
Nothing fancy here, but it will do for now. Next we have to modify the SpringappController to forward to this view.
springapp/src/SpringappController.java |
import org.springframework.web.servlet.mvc.Controller; |
While I was modifying this class, I also added a logger so we can verify that we actually got here. Changes are highlighted in red. The model that this class returns is actually resolved via a ViewResolver. Since we have not specified a specific one, we are going to get a default one that just forwards to a url matching the name of the view specified. We will modify this later on.
Now compile and deploy the application. After instructing Tomcat to stop and then start the application, everything should get reloaded.
Let's try it in a browser – enter the url http://localhost:8080/springapp/hello.htm and we should see the following:
We can also check the log – I'm only showing the last entries, but we can see that the controller did get invoked and that it forwarded to the hello view. (/Users/trisberg/jakarta-tomcat-5.0.28/logs/springapp.log)
2005-04-24 15:01:56,217 INFO [org.springframework.web.servlet.DispatcherServlet] - FrameworkServlet 'springapp': initialization completed in 372 ms |
Summary
Let's take quick look at the parts of our application that we have created so far.
An introduction page index.jsp that does not do anything useful. It was just used to test our setup. We will later change this to actually provide a link into our application.
A DispatcherServlet with a corresponding springapp-servlet.xml configuration file.
A controller springappController.java with limited functionality – it just forwards a ModelAndView to the ViewResolver. Actually, we only have an empty model so far, but we will fix this later.
A view hello.jsp that again is extremely basic. But the whole setup works and we are now ready to add more functionality.
配置struts-config.xml
1Q?配置struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"<struts-config>
<!-- ======================================== Form Bean Definitions -->
<form-beans>
<form-bean
name="userForm" type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="user" type="com.jandar.model.User"/>
</form-bean>
</form-beans>
<!-- =================================== Global Forward Definitions -->
<global-forwards>
</global-forwards>
<!-- =================================== Action Mapping Definitions -->
<action-mappings>
<action path="/user" type="com.jandar.web.struts.action.UserAction "
name="userForm" scope="request" parameter="method" validate="false">
<forward name="list" path="/userList.jsp"/>
<forward name="edit" path="/userForm.jsp"/>
</action>
</action-mappings>
<!-- ================================ Message Resources Definitions -->
<message-resources parameter="messages"/>
</struts-config>
2Q?通过struts-config.xml把struts和springl合h
UserAction.java中的UserManager需要通过依赖注入Q通过plug-in技术将spring加到struts中,在struts-config.xml中增加一下代?br><plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/applicationContext.xml,
/WEB-INF/action-servlet.xml"/>
</plug-in>
让struts启动同时初始化springQ读取spring的配|文件applicationContext.xml,q且把struts的action也交lspring理Q把action配置到action-servlet.xml?br>新徏action-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"<beans>
<bean name="/user" class="com.jandar.web.struts.action.UserAction" singleton="false">
<property name="userManager"><ref bean="userManager"/></property>
</bean>
</beans>
同时action映射到org.springframework.web.struts.DelegatingActionProxy即修?br><action path="/user" type="org.springframework.web.struts.DelegatingActionProxy"
name="userForm" scope="request" parameter="method" validate="false">
<forward name="list" path="/userList.jsp"/>
<forward name="edit" path="/userForm.jsp"/>
</action>
3Q?新徏web.xml配置文g
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="
xmlns:xsi="
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
<servlet> <servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> <init-param> <param-name>debug</param-name> <param-value>3</param-value> </init-param> <init-param> <param-name>detail</param-name> <param-value>3</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file>index.jsp</welcome-file> </welcome-file-list> 4Q?新徏index.jsp,userList.jsp,userForm.jsp <%@ taglib uri="
<%@ taglib uri="
<%@ taglib uri="
<%@ taglib uri="
<%@ taglib uri="
<%@ taglib uri="<html:html locale="true"> <head> <html:base /> </head> <html:link href="user.do?method=list">List all user</html:link> </body> </html:html> userForm.jsp <td><bean:write name="user" property="lastName"/></td></tr>
<servlet-mapping>
<welcome-file-list>
</web-app>
index.jsp
<%@ page language="java"%>
<title>index.jsp</title>
<body>
<%@ page language="java"%>
<%@ taglib uri="<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html locale="true">
<head>
<html:base />
<title>userform.jsp</title>
</head>
<body>
<html:form action="user.do?method=save" method="post" focus="id">
<html:hidden property="user.id"/>
<table border="0">
<tr>
<td>id:</td>
<td><html:text property="user.firstname"/></td>
</tr>
<tr>
<td>lastname:</td>
<td><html:text property="user.lastname" /></td>
</tr>
<tr>
<td colspan="2" align="center"><html:submit property="tijiao"/></td>
</tr>
</table>
</html:form>
</body>
</html:html>
userList.jsp
<%@ page language="java"%>
<%@ taglib uri="<%@ taglib uri="<%@ taglib uri="<%@ taglib uri="<%@ taglib uri="<%@ taglib uri="<%@ taglib uri="<html:html locale="true">
<head>
<html:base />
<title>userList.jsp</title>
</head>
<body>
This a struts page. <br>
<table class="list">
<thead>
<tr>
<th>id</th>
<th>firstName</th>
<th>lastName</th>
</tr>
</thead>
<tbody>
<logic:iterate id="user" name="users">
<tr>
<td>
<a href="user.do?method=edit&id=<bean:write name="user" property="id"/>"><bean:write name="user" property="id"/></a>
</td>
<td><bean:write name="user" property="firstName"/></td>
</logic:iterate>
</tbody>
</table>
</body>
</html:html>
]]>
<!-- DO NOT EDIT: This is a generated file that is synchronized -->
<!-- by MyEclipse Hibernate tool integration. -->
<!-- Created Mon Jul 24 11:48:15 CST 2006 -->
<hibernate-mapping package="">
<class name="AppUser" table="app_user">
<id name="id" column="id" type="integer">
<generator class="identity"/>
</id>
<property name="firstName" column="firstname" type="string" not-null="true" />
<property name="lastName" column="lastname" type="string" />
</class>
</hibernate-mapping>
2Q在com.jandar.model下分别徏 BaseObject.java 和User.java
package com.jandar.model;
import java.io.Serializable;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
public class BaseObject implements Serializable {
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
}
public boolean equals(Object o) {
return EqualsBuilder.reflectionEquals(this, o);
}
public int hashCode() {
return HashCodeBuilder.reflectionHashCode(this);
}
}
package com.jandar.model;
public class User extends BaseObject {
private Long id;
private String firstName;
private String lastName;
/**
* @return Returns the id.
*/
public Long getId() {
return id;
}
/**
* @param id The id to set.
*/
public void setId(Long id) {
this.id = id;
}
public void getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
创徏DAOQ访问对?
1Q?在src/com.jandar.service.dao新徏IDAO.java接口Q所有的DAO都承该接口
package com.jandar.service.dao;
public interface IDAO {
}
2Q?在src/com.jandar.service.dao下新建IUserDAO.java接口
package com.jandar.service.dao;
import java.util.List;
public interface IUserDAO extends IDAO {
List getUsers();
User getUser(Integer userid);
void saveUser(User user);
void removeUser(Integer id);
}
该接口提供了讉K对象的方法,
3Q?在src/com.jandar.service.dao.hibernate下新建UserDAOHiberante.java
package com.jandar.service.dao.hibernate;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.orm.hibernate.support.HibernateDaoSupport;
import com.jandar.model.User;
import com.jandar.service.dao.IUserDAO;
public class UserDaoHibernate extends HibernateDaoSupport implements IUserDAO {
private Log log=LogFactory.getLog(UserDaoHibernate.class);
/* Q非 JavadocQ?
* @see com.jandar.dao.IUserDAO#getUsers()
*/
public List getUsers() {
return getHibernateTemplate().find("from User");
}
/* Q非 JavadocQ?
* @see com.jandar.dao.IUserDAO#getUser(java.lang.Long)
*/
public User getUser(Integer id) {
// TODO 自动生成Ҏ存根
return (User) getHibernateTemplate().get(User.class,id);
}
/* Q非 JavadocQ?
* @see com.jandar.dao.IUserDAO#saveUser(com.jandar.model.User)
*/
public void saveUser(User user) {
log.debug("xxxxxxx");
System.out.println("yyyy");
getHibernateTemplate().saveOrUpdate(user);
if(log.isDebugEnabled())
{
log.debug("userId set to "+user.getId());
}
}
/* Q非 JavadocQ?
* @see com.jandar.dao.IUserDAO#removeUser(java.lang.Long)
*/
public void removeUser(Integer id) {
Object user=getHibernateTemplate().load(User.class,id);
getHibernateTemplate().delete(user);
if(log.isDebugEnabled()){
log.debug("del user "+id);
}
}
}
在这个类中实CIUserDAO接口的方法,q且l承了HibernateDAOSupportcR这个类的作用是通过hibernate讉K、操作对象,q而实现对数据库的操作?
创徏业务层,声明事务
业务层主要处理业务逻辑Q提供给web层友好的讉K接口和实现访问DAO层。用业务层的另一个好处是Q可以适应数据讉K层从Hibernate技术{Ud其他数据讉K技术?
1Q?在src/com.jandar.service下新Z个IUserManager接口Q该接口有几乎于IUserDAO同样的方法,不同的是处理参数Q应为IUserManager是供web层访问的?
package com.jandar.service;
import java.util.List;
import com.jandar.model.User;
public interface IUserManager {
User getUser(String userid);
List getUsers();
User saveUser(User user);
void removeUser(String userid);
}
2Q?在src/com.jandar.service.spring下新建IUserManager实现c,UserManager.java
package com.jandar.service.spring;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.jandar.model.User;
import com.jandar.service.IUserManager;
import com.jandar.service.dao.IUserDAO;
public class UserManager implements IUserManager {
/* Q非 JavadocQ?
* @see com.jandar.service.IUserManager#getUser(java.lang.String)
*/
private static Log log=LogFactory.getLog(UserManager.class);
public IUserDAO userDao;
/**
* @return q回 userDao?
*/
public IUserDAO getUserDao() {
return userDao;
}
/**
* @param userDao 要设|的 userDao?
*/
public void setUserDao(IUserDAO userDao) {
this.userDao = userDao;
}
public User getUser(String userid) {
User user=userDao.getUser(Integer.valueOf(userid));
if(user==null){
log.warn(" user id "+userid+" not found in database");
}
if(log.isDebugEnabled()){
log.debug("get a user with id "+userid);
}
return user;
}
/* Q非 JavadocQ?
* @see com.jandar.service.IUserManager#getUsers()
*/
public List getUsers() {
// TODO 自动生成Ҏ存根
return userDao.getUsers();
}
/* Q非 JavadocQ?
* @see com.jandar.service.IUserManager#saveUser(com.jandar.model.User)
*/
public User saveUser(User user) {
// TODO 自动生成Ҏ存根
userDao.saveUser(user);
return user;
}
/* Q非 JavadocQ?
* @see com.jandar.service.IUserManager#removeUser(java.lang.String)
*/
public void removeUser(String userid) {
// TODO 自动生成Ҏ存根
userDao.removeUser(Integer.valueOf(userid));
}
}
UserManager.java通过讉Kdao接口实现业务逻辑和数据库操作。同时该cM提供了setҎQ运用了Spring的依赖注入机制。但未使用spring的AOP和声明事务?
配置applicationContext.xml
在WEB-INF 下新建applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
Spring 框架提供了构?Web 应用E序的全功能 MVC 模块。?Spring 可插入的 MVC 架构Q可以选择是用内|的 Spring Web 框架q是 Struts q样?Web 框架。通过{略接口QSpring 框架是高度可配置的,而且包含多种视图技术,例如 JavaServer PagesQJSPQ技术、Velocity、Tiles、iText ?POI。Spring MVC 框架q不知道使用的视图,所以不会强q您只?JSP 技术。Spring MVC 分离了控制器、模型对象、分z֙以及处理E序对象的角Ԍq种分离让它们更Ҏq行定制?/p>
Spring ?Web MVC 框架是围l?DispatcherServlet 设计的,它把h分派l处理程序,同时带有可配|的处理E序映射、视图解析、本地语a、主题解析以及上载文件支持。默认的处理E序是非常简单的 Controller 接口Q只有一个方?ModelAndView handleRequest(request, response)。Spring 提供了一个控制器层次l构Q可以派生子cR如果应用程序需要处理用戯入表单,那么可以l承 AbstractFormController。如果需要把多页输入处理C个表单,那么可以l承 AbstractWizardFormController?/p>
CZ应用E序有助于直观地学习q些Ҏ。银行应用程序允许用h索他们的帐户信息。在构徏银行应用E序的过E中Q可以学到如何配|?Spring MVC 框架和实现框架的视图层,视图层包?JSTL 标记Q用于显C出的数据Q和JavaServer Pages 技术?/p>
配置 Spring MVC
要开始构建示例应用程序,请配|?Spring MVC ?DispatcherServlet。请?web.xml 文g中注册所有配|。清?1 昄了如何配|?sampleBankingServlet?/p>
清单 1. 配置 Spring MVC DispatcherServlet
<servlet>
<servlet-name>sampleBankingServlet</servlet-name>
<servlet-class>
org.springframework.we.servlet.DispatcherServlet
<servlet-class>
<load-on-startup>1<load-on-startup>
<servlet>
DispatcherServlet 从一?XML 文g装入 Spring 应用E序上下文,XML 文g的名U是 servlet 的名U后面加?-servlet 。在q个CZ中,DispatcherServlet 会从 sampleBankingServlet-servlet.xml 文g装入应用E序上下文?
配置应用E序?URL
下一步是配置惌 sampleBankingServlet 处理?URL。同Pq是要在 web.xml 中注册所有这些信息?/p>
清单 2. 配置惌处理?URL
<servlet-mapping>
<servlet-name> sampleBankingServlet<servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
装入配置文g
下面Q装入配|文件。ؓ了做到这点,请ؓ Servlet 2.3 规范注册 ContextLoaderListener 或ؓ Servlet 2.2 及以下的容器注册 ContextLoaderServlet。ؓ了保障后向兼Ҏ,L ContextLoaderServlet。在启动 Web 应用E序ӞContextLoaderServlet 会装?Spring 配置文g。清?3 注册?ContextLoaderServlet?/p>
清单 3. 注册 ContextLoaderServlet
<servlet>
<servlet-name>context>servlet-name>
<servlet-class>
org.springframework.web.context.ContextLoaderServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
contextConfigLocation 参数定义了要装入?Spring 配置文gQ如下面?servlet 上下文所C?/p>
<context-param>
<param-value>contextConfigLocation</param-value>
<param-value>/WEB-INF/sampleBanking-services.xml</param-value>
</context-param>
sampleBanking-services.xml 文g代表CZ银行应用E序服务的配|和 bean 配置。如果想装入多个配置文gQ可以在 <param-value> 标记中用逗号作分隔符?/p>
Spring MVC CZ
CZ银行应用E序允许用户Ҏ惟一?ID 和口令查看帐户信息。虽?Spring MVC 提供了其他选项Q但是我采?JSP 技术作N面。这个简单的应用E序包含一个视N用于用户输入QID 和口令)Q另一|C用L帐户信息?/p>
我从 LoginBankController 开始,它扩展了 Spring MVC ?SimpleFormController。SimpleFormContoller 提供了显CZ HTTP GET h接收到的表单的功能,以及处理?HTTP POST 接收到的相同表单数据的功能。LoginBankController ?AuthenticationService ?AccountServices 服务q行验证Qƈ执行帐户zd?#8220; 配置视图属?”一节中的清?5 描述了如何把 AuthenticationService ?AccountServices q接?LoginBankController?清单 4 昄?LoginBankController 的代码?/p>
清单 4. LoginBankController 扩展 SimpleFormController
public class LoginBankController extends SimpleFormController {
public LoginBankController(){
}
protected ModelAndView onSubmit(Object command) throws Exception{
LoginCommand loginCommand = (LoginCommand) command;
authenticationService.authenticate(loginCommand);
AccountDetail accountdetail = accountServices.getAccountSummary(loginCommand.getUserId());
return new ModelAndView(getSuccessView(),"accountdetail",accountdetail);
}
private AuthenticationService authenticationService;
private AccountServices accountServices;
public AccountServices getAccountServices() {
return accountServices;
}
public void setAccountServices(AccountServices accountServices) {
this.accountServices = accountServices;
}
public AuthenticationService getAuthenticationService() {
return authenticationService;
}
public void setAuthenticationService(
AuthenticationService authenticationService) {
this.authenticationService = authenticationService;
}
}
配置视图属?/strong>
下面Q必L册在接收?HTTP GET h时显C的面。在 Spring 配置中用 formView 属性注册这个页面,如清?5 所C。sucessView 属性代表表单数据提交而且 doSubmitAction() Ҏ中的逻辑成功执行之后昄的页面。formView ?sucessView 属性都代表被定义的视图的逻辑名称Q逻辑名称映射到实际的视图面?/p>
清单 5. 注册 LoginBankController
<bean id="loginBankController"
class="springexample.controller.LoginBankController">
<property name="sessionForm"><value>true</value></property>
<property name="commandName"><value>loginCommand</value></property>
<property name="commandClass">
<value>springexample.commands.LoginCommand</value>
</property>
<property name="authenticationService">
<ref bean="authenticationService" />
</property>
<property name="accountServices">
<ref bean="accountServices" />
</property>
<property name="formView">
<value>login</value>
</property>
<property name="successView">
<value>accountdetail</value>
</property>
</bean>
commandClass ?commandName 标记军_在视图面中活动的 bean。例如,可以通过 login.jsp 面讉K loginCommand beanQ这个页面是应用E序的登录页面。一旦用h交了d面Q应用程序就可以?LoginBankController ?onSubmit() Ҏ中的命o对象索出表单数据?/p>
视图解析?/strong>
Spring MVC ?视图解析?把每个逻辑名称解析成实际的资源Q即包含帐户信息?JSP 文g。我用的?Spring ?InternalResourceViewResolverQ如 清单 6 所C?/p>
清单 6. InternalResourceViewResolver
<bean id="view-Resolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.JstlView</value>
</property>
<property name="prefix"><value>/jsp/</value></property>
<property name="suffix"><value>.jsp</value></property>
</bean>
因ؓ我在 JSP 面中用了 JSTL 标记Q所以用Ld名称解析成资?/jsp/login.jspQ?viewClass 成ؓ JstlView?/p>
验证和帐h?/strong>
像前面提到的,LoginBankController 内部q接?Spring ?AccountServices ?AuthenticationService。AuthenticationService cd理银行应用程序的验证。AccountServices cd理典型的银行服务Q例如查找交易和甉|。清?7 昄了银行应用程序的验证和帐h务的配置?/p>
清单 7. 配置验证和帐h?/p>
<beans>
<bean id="accountServices"
class="springexample.services.AccountServices">
</bean>
<bean id="authenticationService"
class="springexample.services.AuthenticationService">
</bean>
</beans>
以上服务?sampleBanking-services.xml 中注册,然后装入 web.xml 文g中,像 前面讨论的那栗控制器和服务配|好后,q个单的应用E序完成了。现在我们来看看部v和测试它时会发生什?
部v应用E序
把示例应用程序部|在 Tomcat servlet 容器中。Tomcat ?Java Servlet ?Java ServerPagest 技术的官方参考实C使用?servlet 容器。如果以前没q么做过Q请 下蝲 jakarta-tomcat-5.0.28.exe q运行它?Tomcat 安装到自己喜Ƣ的M位置Q例?c:\tomcat5.0?/p>
接下来,下蝲CZ代码 q攑ֈ驱动器(例如 c:\ Q上。创Z Spring 目的文件夹之后Q打开它ƈ?spring-banking 子文件夹拯?c:\tomvat5.0\webapps。spring-banking 文gҎ一?Web 档案Q里面包?Spring MVC CZ应用E序。lib 文g夹包含应用程序需要的 Spring 框架、与Spring 相关?MVC 库以?JSTL 标记库和 jar 文g?/p>
要启?Tomcat 服务器,请用以下命令:
cd bin C:\Tomcat 5.0\bin> catalina.bat start
Tomcat 应当启动q|?Spring MVC CZ应用E序?/p>
试应用E序
要测试应用程序,h开 Web 览器,指向 http://localhost:tomcatport/springbanking q用 Tomcat 服务器实际运行的端口替换 tomcatport。应当看到图 1 所C的d屏幕。输入用?ID “admin”和口?#8220;password”Qƈ按下d按钮。其他用?ID 或口令会造成来自验证服务的错误?/p>
?1. Spring MVC CZd屏幕
d成功之后Q会看到?2 所C的帐户l节面?/p>
?2. Spring MVC CZ帐户l节面
Spring MVC框架的高U配|?br>http://dev2dev.bea.com.cn/techdoc/2006068810.html