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.
é…ç½®struts-config.xml
1åQ?é…ç½®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>
2åQ?通过struts-config.xml把strutså’Œspring¾l“åˆèµä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å¯åŠ¨åŒæ—¶åˆå§‹åŒ–springåQŒè¯»å–spring的酾|®æ–‡ä»¶applicationContext.xml,òq¶ä¸”把strutsçš„action也交¾l™spring½Ž¡ç†å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>
3åQ?æ–°å¾web.xmlé…置文äšg
<?xml version="1.0" encoding="UTF-8"?>
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
<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-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
4åQ?æ–°å¾index.jsp,userList.jsp,userForm.jsp
index.jsp
<%@ page language="java"%>
<%@ taglib uri="<html:html locale="true">
<head>
<html:base />
<title>index.jsp</title>
</head>
<body>
<html:link href="user.do?method=list">List all user</html:link>
</body>
</html:html>
userForm.jsp
<%@ 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>
<td><bean:write name="user" property="lastName"/></td></tr>
</logic:iterate>
</tbody>
</table>
</body>
</html:html>
  Spring 框架æä¾›äº†æž„å»?Web 应用½E‹åºçš„全功能 MVC 模å—。ä‹Éç”?Spring 坿’入的 MVC æž¶æž„åQŒå¯ä»¥é€‰æ‹©æ˜¯ä‹É用内¾|®çš„ Spring Web 框架˜q˜æ˜¯ Struts ˜q™æ ·çš?Web 框架。通过½{–略接å£åQŒSpring 框架是高度å¯é…置的,而且包å«å¤šç§è§†å›¾æŠ€æœ¯ï¼Œä¾‹å¦‚ JavaServer PagesåQˆJSPåQ‰æŠ€æœ¯ã€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Œå¯ä»¥æ´¾ç”Ÿå¾c…R€‚如果应用程åºéœ€è¦å¤„ç†ç”¨æˆ¯‚¾“入表å•,那么å¯ä»¥¾l§æ‰¿ AbstractFormControllerã€‚å¦‚æžœéœ€è¦æŠŠå¤šé¡µè¾“å…¥å¤„ç†åˆîC¸€ä¸ªè¡¨å•,那么å¯ä»¥¾l§æ‰¿ AbstractWizardFormControllerã€?/p>
  ½CÞZ¾‹åº”用½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™ä¸ª½CÞZ¾‹ä¸ï¼ŒDispatcherServlet 会从 sampleBankingServlet-servlet.xml æ–‡äšg装入应用½E‹åºä¸Šä¸‹æ–‡ã€?
  é…置应用½E‹åºçš?URL
ã€€ã€€ä¸‹ä¸€æ¥æ˜¯é…置惌™®© sampleBankingServlet 处ç†çš?URLã€‚åŒæ øP¼Œ˜q˜æ˜¯è¦åœ¨ 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 é…置文äšgåQŒå¦‚下é¢çš?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代表½CÞZ¾‹é“¶è¡Œåº”用½E‹åºæœåŠ¡çš„é…¾|®å’Œ bean é…置。如果想装入多个é…置文äšgåQŒå¯ä»¥åœ¨ <param-value> æ ‡è®°ä¸ç”¨é€—å·ä½œåˆ†éš”符ã€?/p>
  Spring MVC ½CÞZ¾‹
  ½CÞZ¾‹é“¶è¡Œåº”用½E‹åºå…许用户æ ÒŽ®æƒŸä¸€çš?ID å’Œå£ä»¤æŸ¥çœ‹å¸æˆ·ä¿¡æ¯ã€‚虽ç„?Spring MVC æä¾›äº†å…¶ä»–选项åQŒä½†æ˜¯æˆ‘ž®†é‡‡ç”?JSP æŠ€æœ¯ä½œä¸ø™§†å›ùN¡µé¢ã€‚这个简å•的应用½E‹åºåŒ…å«ä¸€ä¸ªè§†å›ùN¡µç”¨äºŽç”¨æˆ·è¾“å…¥åQˆID å’Œå£ä»¤ï¼‰åQŒå¦ä¸€™å‰|˜¾½Cºç”¨æˆïLš„叿ˆ·ä¿¡æ¯ã€?/p>
  我从 LoginBankController 开始,它扩展了 Spring MVC çš?SimpleFormController。SimpleFormContoller æä¾›äº†æ˜¾½CÞZ»Ž HTTP GET è¯äh±‚接收到的表å•的功能,以åŠå¤„ç†ä»?HTTP POST 接收到的相åŒè¡¨å•æ•°æ®çš„功能。LoginBankController ç”?AuthenticationService å’?AccountServices æœåŠ¡˜q›è¡ŒéªŒè¯åQŒåÆˆæ‰§è¡Œå¸æˆ·‹zÕdЍã€?#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 beanåQŒè¿™ä¸ªé¡µé¢æ˜¯åº”用½E‹åºçš„登录页é¢ã€‚一旦用æˆäh交了ç™Õd½•™åµé¢åQŒåº”用程åºå°±å¯ä»¥ä»?LoginBankController çš?onSubmit() æ–ÒŽ³•ä¸çš„命ä×o对象‹‚€ç´¢å‡ºè¡¨å•æ•°æ®ã€?/p>
  视图解æžå™?/strong>
  Spring MVC çš?视图解æžå™?把æ¯ä¸ªé€»è¾‘åç§°è§£æžæˆå®žé™…的资æºåQŒå³åŒ…å«å¸æˆ·ä¿¡æ¯çš?JSP æ–‡äšg。我用的æ˜?Spring çš?InternalResourceViewResolveråQŒå¦‚ æ¸…å• 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Œæ‰€ä»¥ç”¨æˆïLš„ç™Õd½•åç§°è§£æžæˆèµ„æº?/jsp/login.jspåQŒè€?viewClass æˆäØ“ JstlViewã€?/p>
  验è¯å’Œå¸æˆähœåŠ?/strong>
  ž®±åƒå‰é¢æåˆ°çš„,LoginBankController 内部˜qžæŽ¥äº?Spring çš?AccountServices å’?AuthenticationService。AuthenticationService ¾cÕd¤„ç†é“¶è¡Œåº”用程åºçš„验è¯ã€‚AccountServices ¾cÕd¤„ç†å…¸åž‹çš„银行æœåŠ¡å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>
  接下æ¥ï¼Œä¸‹è²½CÞZ¾‹ä»£ç òq‰™‡Šæ”‘Öˆ°é©±åŠ¨å™¨ï¼ˆä¾‹å¦‚ c:\ åQ‰ä¸Šã€‚创å»ÞZº† Spring ™å¹ç›®çš„æ–‡ä»¶å¤¹ä¹‹åŽåQŒæ‰“å¼€å®ƒåÆˆæŠ?spring-banking åæ–‡ä»¶å¤¹æ‹¯‚´åˆ?c:\tomvat5.0\webapps。spring-banking æ–‡äšgå¤ÒŽ˜¯ä¸€ä¸?Web 档案åQŒé‡Œé¢åŒ…å?Spring MVC ½CÞZ¾‹åº”用½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 ½CÞZ¾‹åº”用½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 ½CÞZ¾‹ç™Õd½•å±å¹•
  ç™Õd½•æˆåŠŸä¹‹åŽåQŒä¼šçœ‹åˆ°å›?2 所½Cºçš„叿ˆ·¾l†èŠ‚™åµé¢ã€?/p>
å›?2. Spring MVC ½CÞZ¾‹å¸æˆ·¾l†èŠ‚™åµé¢
Spring MVC框架的高¾U§é…¾|?br>http://dev2dev.bea.com.cn/techdoc/2006068810.html