3,springTest-services.xml的内容:
<bean id="authenticationService" class="com.test.spring.mvc.services.AuthenticationService"/>
4,Java代码:
public class LoginController extends SimpleFormController {
org.springframework.web.servlet.DispatcherServlet t;
protected ModelAndView onSubmit(Object command) throws Exception{
LoginCommand loginCommand = (LoginCommand) command;
authenticationService.authenticate(loginCommand);
LoginDetail loginDetail = authenticationService.getLoginDetail(loginCommand.getUserId());
return new ModelAndView(getSuccessView(),"loginDetail",loginDetail);
}
private AuthenticationService authenticationService;
public AuthenticationService getAuthenticationService() {
return authenticationService;
}
public void setAuthenticationService(
AuthenticationService authenticationService) {
this.authenticationService = authenticationService;
}
....
public class LoginCommand {
private String userId;
private String password;
....
public class LoginDetail {
private String userName;
public class AuthenticationService {
public void authenticate(LoginCommand command) throws Exception{
if(command.getUserId()!= null && command.getUserId().equalsIgnoreCase("test")
&& command.getPassword()!= null && command.getPassword().equalsIgnoreCase("test")){
}else{
throw new Exception("User id is not authenticated");
}
}
public LoginDetail getLoginDetail(String userId){
return new LoginDetail(userId);
}
}
5,JSP文件:放在web-inf的jsp文件夹内
login.jsp:
<html><head>
<title>Login to Spring Test</title></head>
<body>
<h1>Please enter your userid and password.</h1>
<form method="post" action="login.do">
<table width="95%" bgcolor="f8f8ff" border="0" cellspacing="0" cellpadding="5">
<tr>
<td alignment="right" width="20%">User id:</td>
<td width="20%"><input type="text" name="userId" value="test"></td>
<td width="60%">
</tr>
<tr>
<td alignment="right" width="20%">Password:</td>
<td width="20%"><input type="password" name="password" value="test"></td>
<td width="60%">
</tr>
</table>
<br>
<input type="submit" alignment="center" value="login">
</form>
</body>
</html>
loginDetail.jsp:
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<html><head>
<title>Login to Spring Test</title></head>
<body>
<h1>Login Details:</h1>
<br/>
Login as: <c:out value ="${loginDetail.userName}"/>
<br/>
<a href="/S3/login.do">logout</a>
</body>
</html>
在浏览器的地址栏输入http://localhost:8080/XXX/login.do进入login.jsp页面.
对配置的一些扩充点:
1为HandlerMapping和Controller的配置指定文件名称.
<servlet>
<servlet-name>springTestServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/xxx.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
2,加入对MVC注解的支持:
01.<?xml version="1.0" encoding="UTF-8"?>
02.<beans xmlns="http://www.springframework.org/schema/beans"
03. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
04. xmlns:context="http://www.springframework.org/schema/context"
05. xmlns:mvc="http://www.springframework.org/schema/mvc"
06. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
07. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
08. http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
09. <context:annotation-config />
10. <context:component-scan base-package="com.test.spring.mvc.contoller" />
11. <mvc:annotation-driven />
12.
13.@Controller
14.public class AnnotationController {
15. @RequestMapping("annotation.do")
16. protected void test(HttpServletRequest request,
17. HttpServletResponse response) throws Exception{
18. response.getWriter().println("test Spring MVC annotation");
19. }
3,注解和SimpleFormController同时使用需要在DispatcherServlet对应的servlet配置文件(本例是springTestServlet-servlet.xml)里面配置:
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
否则会发生下面的异常:
javax.servlet.ServletException: No adapter for handler [com.test.spring.mvc.contoller.LoginController@6ac615]: Does your handler implement a supported interface like Controller?
org.springframework.web.servlet.DispatcherServlet.getHandlerAdapter(DispatcherServlet.java:982)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:770)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:647)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:552)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
在WEB应用中的context配置文件通过下面的方式load,ContextLoaderListener是一个实现了ServletContextListener的listener,它能够访问<context-param>而得到配置文件的路径,加载后初始化了一个WebApplicationContext,并且将其作为Attribute放在了servletContext中,所有可以访问servletContext的地方则可以访问WebApplicationContext.
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/springTest-services.xml,
classpath:conf/jndiDSAppcontext.xml
</param-value>
</context-param>
