关于SpringMVC的介绍我就不多说了,网上一搜一大堆,好多大鸟的博客都有详细的描述,之前看的跟开涛学SpringMVC,写的非常好,SpringMVC运行的流程和原理讲的非常的细致在此我引用一下开涛前辈的图片和文字,大家要是想看原文就点击上面的链接。
SpringMVC处理请求的流程图
大家一定要仔细的看,最好是拿张纸,画一画,可比你光看有效果,大家可以与纯MVC模式对比一下,这样理解起来就不是那么的难了。
对上面的图在此细化
在此我们可以看出具体的核心开发步骤:
- DispatcherServlet在web.xml中的部署描述,从而拦截请求到Spring Web MVC
- HandlerMapping的配置,从而将请求映射到处理器
- HandlerAdapter的配置,从而支持多种类型的处理器
- ViewResolver的配置,从而将逻辑视图名解析为具体视图技术
- 处理器(页面控制器)的配置,从而进行功能处理
把上面的五个步骤和图一一对应起来,之后的开发就是按照这五个步骤进行的!
后来我想实现一个简单的用户登录验证的例子来验证一下,毕竟实践是验证理论的唯一途径也是学习的最好方式!
没有用到数据库,没有必要,我这里只是学习SpringMVC的运行机制
首先建立一个Web项目取名为SpringLoginDemo
在导入所需要的包,如图:(下面有下载)
接下来做什么呢?看上面的那五个步骤,第一步部署web.xml,从而拦截请求到spring Web MVC
web.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.5"
- xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
- http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
- <display-name></display-name>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- <servlet>
- <servlet-name>Dispatcher</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
-
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:applicationContext.xml</param-value>
- </init-param>
- </servlet>
- <servlet-mapping>
- <servlet-name>Dispatcher</servlet-name>
- <url-pattern>*.do</url-pattern>
- </servlet-mapping>
- </web-app>
不要忘记了编写eneity
- package com.example.entity;
-
- public class User {
- private String username;
- private String password;
-
- public User(String username, String password) {
- super();
- this.username = username;
- this.password = password;
- }
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
-
- }
接下来是LoginController
- package com.example.controller;
-
- import java.util.HashMap;
- import java.util.Map;
-
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- import org.springframework.web.servlet.ModelAndView;
- import org.springframework.web.servlet.mvc.AbstractController;
-
- import com.example.entity.User;
-
- public class LoginController extends AbstractController {
-
- private String successView;
- private String failView;
-
- public String getSuccessView() {
- return successView;
- }
-
- public void setSuccessView(String successView) {
- this.successView = successView;
- }
-
- public String getFailView() {
- return failView;
- }
-
- public void setFailView(String failView) {
- this.failView = failView;
- }
-
- @Override
- protected ModelAndView handleRequestInternal(HttpServletRequest request,
- HttpServletResponse response) throws Exception {
-
- String username = request.getParameter("username");
- String password = request.getParameter("password");
- User user = getUser(username, password);
-
- Map<String ,Object> model=new HashMap<String,Object>();
- if(user !=null){
- model.put("user", user);
- return new ModelAndView(getSuccessView(),model);
- }else{
- model.put("error", "用户名或密码输入错误!!!");
- return new ModelAndView(getFailView(),model);
- }
- }
-
- public User getUser(String username,String password){
- if(username.equals("test") && password.equals("test")){
- return new User(username,password);
- }else{
- return null;
- }
- }
- }
接下来就是核心配置文件applicationContext.xml非常重要,包括上面步骤中的2,3,4
- <?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:aop="http://www.springframework.org/schema/aop"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
- <bean id="loginController" class="com.example.controller.LoginController">
-
- <property name="successView" value="showUser"></property>
- <property name="failView" value="login"></property>
- </bean>
- <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
- <property name="mappings">
- <props>
- <prop key="/login.do">loginController</prop>
- </props>
- </property>
- </bean>
- <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="prefix" value="/"></property>
- <property name="suffix" value=".jsp"></property>
- </bean>
- </beans>
来看一下图片
其实A应该是web.xml里的
- <servlet-mapping>
- <servlet-name>Dispatcher</servlet-name>
- <url-pattern>*.do</url-pattern>
- </servlet-mapping>
这才是开始接下来就是上面箭头所指的操作流程了
还有两个jsp页面
- login.jsp
- <body>
- ${error }
- <form action="login.do" method="post">
- 用户登陆<br>
- <hr>
- 用户名:<input type="text" name="username"><br>
- 密码:<input type="text" name="password"><br>
- <input type="submit" value="登陆">
- </form>
-
- </body>
- showUser.jsp
- <body>
- 用户信息<br>
- 用户名:${account.cardNo }<br>
- 密码:${account.password }<br>
- <font color="red">欢迎登陆!!!</font><br>
- </body>
最最后就是运行了。。。。
源码下载
本文部分引用的内容来自:http://jinnianshilongnian.iteye.com/blog/1594806