Spring学习之SpringMVC框架快速搭建实现用户登录功能
引用自:http://blog.csdn.net/qqhjqs/article/details/41683099?utm_source=tuicool&utm_medium=referral 的博客
关于SpringMVC的介绍我就不多说了,网上一搜一大堆,好多大鸟的博客都有详细的描述,之前看的跟开涛学SpringMVC,写的非常好,SpringMVC运行的流程和原理讲的非常的细致在此我引用一下开涛前辈的图片和文字,大家要是想看原文就点击上面的链接。
SpringMVC处理请求的流程图
大家一定要仔细的看,最好是拿张纸,画一画,可比你光看有效果,大家可以与纯MVC模式对比一下,这样理解起来就不是那么的难了。
对上面的图在此细化
在此我们可以看出具体的核心开发步骤:
DispatcherServlet在web.xml中的部署描述,从而拦截请求到Spring Web MVC
HandlerMapping的配置,从而将请求映射到处理器
HandlerAdapter的配置,从而支持多种类型的处理器
ViewResolver的配置,从而将逻辑视图名解析为具体视图技术
处理器(页面控制器)的配置,从而进行功能处理
把上面的五个步骤和图一一对应起来,之后的开发就是按照这五个步骤进行的!
后来我想实现一个简单的用户登录验证的例子来验证一下,毕竟实践是验证理论的唯一途径也是学习的最好方式!
没有用到数据库,没有必要,我这里只是学习SpringMVC的运行机制
首先建立一个Web项目取名为SpringLoginDemo
在导入所需要的包,如图:(下面有下载)
接下来做什么呢?看上面的那五个步骤,
1、部署web.xml
第一步部署web.xml,从而拦截请求到spring Web MVC
- <?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>
- <!-- Spring配置文件 -->
- <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>
2、编写eneity类,User.java
- 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;
- }
- }
3、LoginController.java类
- 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);
- //保存相应的参数,通过ModelAndView返回
- 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;
- }
- }
- }
4、核心配置applicationContext.xml
- <?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>
来看一下图片
5、三个jsp页面
index.jsp
- <%@ page language="java" contentType="text/html; charset=GB18030"
- pageEncoding="GB18030"%>
- <!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=GB18030">
- <title>Insert title here</title>
- </head>
- <body>
- <a href="login.jsp">进入</a>
- </body>
- </html>
login.jsp
- <%@ page language="java" contentType="text/html; charset=GB18030"
- pageEncoding="GB18030"%>
- <!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=GB18030">
- <title>Insert title here</title>
- </head>
- <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>
- </html>
showUser.jsp
- <%@ page language="java" contentType="text/html; charset=GB18030"
- pageEncoding="GB18030"%>
- <!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=GB18030">
- <title>Insert title here</title>
- </head>
- <body>
- 用户信息<br>
- 用户名:${user.username }<br>
- 密码:${user.password }<br>
- <font color="red">欢迎登陆!!!</font><br>
- </body>
- </html>
6、启动Tomcat服务器运行
附件源码