首发地址: http://inmethetiger.iteye.com/blog/1677057
在开发Spring mvc的时候,各种配置下来,往往项目跑起来了,但是一天不写,第二天又忘记怎么搭建了。所以,应该将搭建的思路写下来,这样,开发的时候就会方便许多。就像学武一样,有武功秘籍和没有武功秘籍一样。记住开发流程,思路会变得很清晰,开发起来也就更加快速。
Spring mvc的开发流程如下:
一:配置基础
1>配置目录结构。比如基于eclipse格式还是Maven格式。
2>配置web.xml,主要是以下几个
org.springframework.web.servlet.DispatcherServlet
org.springframework.web.context.ContextLoadListener
org.springframework.web.filter.CharacterEncoding
org.springframework.web.util.Log4jConfigListener
二:开发独立的业务逻辑,并在配置文件中进行实例化(当然,也可以直接new)
三:添加web请求入口
如:<a href="login.do" >登录</a>
四:添加HandlerMapping
DispatchServlet接收到web请求后,将寻求相应的HandlerMapping进行web请求到具体的Controller实现相应的匹配
五:实现对应的Controler并添加到配置文件中
只有实现了Controller,DispatcherServlet并不会直到它的存在,还需要将该控制器在配置文件中进行配置
六:添加视图解析器
DispatchServlet根据Controller中的逻辑视图命查找相应的视图实现。默认的是org.springframework.web.servlet.view.InternalResourceViewResolver
七:实现相应视图
----------------------------------------------------------------------------------------------------------------------------------
接下来,按照上面的描述来建立一个最简单的Spring mvc项目
一:配置目录结构
1>采用的是eclipse目录
2>配置web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>spring_mvc_1</display-name> <!-- The front controller of this Spring Web application, responsible for handling all application requests --> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <!-- Map all requests to the DispatcherServlet for handling --> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <!-- Bootstraps the root web application context before servlet initialization --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- Reads request input using UTF-8 encoding --> <filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping></web-app>
由于只使用Springmvc,所以没有配置ContextLoadListener
二:开发独立的业务逻辑,并添加到配置文件中
package com.spring.service;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class UserService {
protected static Log log = LogFactory.getLog(UserService.class);
public boolean login(String username, String passworld) {
log.info(username + "\t" + passworld);
if (username.equals("admin") && passworld.equals("admin")) {
return true;
}
return false;
}
}
因为只是为了演示思路,所以业务逻辑很简单。并在配置文件中进行配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <bean id="userService" class="com.spring.service.UserService"></bean> </beans>
三:添加web请求入口
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>LOGIN</title> </head> <body> <form action="login.do" method="post"> 用户名 <input type="text" name="username" id="username" /> <br> 密码 <input type="text" name="password" id="password" /> <br> <input type="submit" value="提交" /> </form> </body> </html>
我的是login.do注意,不是/login.do。
四:添加HandlerMapping
<!-- 定义映射 --> <bean id="handlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
采用的是BeanNameUrlHandlerMapping,这是Springmvc默认的。也可以不配置。要求是:需要保证视图模板请求路径与容器中的HandlerBeanName一致
五:实现对应的Controller并添加到配置文件中
package com.spring.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import com.spring.service.UserService;
public class LoginController extends AbstractController {
protected static Log log = LogFactory.getLog(LoginController.class);
private UserService userService;
public UserService getUserService() {
return userService;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView mv = new ModelAndView();
boolean isSuccess = userService.login(request.getParameter("username"),
request.getParameter("password"));
System.out.println(isSuccess);
mv.addObject("login", isSuccess);
if (isSuccess) {
mv.setViewName("success");
} else {
mv.setViewName("failed");
}
System.out.println(mv.getViewName());
return mv;
}
}
配置文件
<!-- 定义控制器 --> <bean name="/login.do" class="com.spring.controller.LoginController"> <property name="userService" ref="userService"></property> </bean>
注意:我请求路径是"login.do",所以过根据BeanNameUrlHandlerMapping。所以配置文件中的也是"/login.do"。如果请求路径是"/login.do"将会出现404错误。
六:添加视图解析器
<!-- 定义ViewReSover --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"></property> <property name="suffix" value=".jsp"></property> </bean>
七:实现相应视图
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> Hello Spring MVC <br>欢迎${login}登录成功 </body> </html>
到此一个简单的Springmvc项目就搭建成功了。不过要注意的是,这七个步骤并不是一定要按照这个顺序。顺序可以有变动,但是开发过程中,记住这七个步骤,思路将会更加清晰