Spring学习之SpringMVC框架快速搭建实现用户登录功能

时间:2021-07-27 01:27:33

引用自:http://blog.csdn.net/qqhjqs/article/details/41683099?utm_source=tuicool&utm_medium=referral  的博客

关于SpringMVC的介绍我就不多说了,网上一搜一大堆,好多大鸟的博客都有详细的描述,之前看的跟开涛学SpringMVC,写的非常好,SpringMVC运行的流程和原理讲的非常的细致在此我引用一下开涛前辈的图片和文字,大家要是想看原文就点击上面的链接。
SpringMVC处理请求的流程图

Spring学习之SpringMVC框架快速搭建实现用户登录功能

大家一定要仔细的看,最好是拿张纸,画一画,可比你光看有效果,大家可以与纯MVC模式对比一下,这样理解起来就不是那么的难了。
对上面的图在此细化

Spring学习之SpringMVC框架快速搭建实现用户登录功能

在此我们可以看出具体的核心开发步骤:
DispatcherServlet在web.xml中的部署描述,从而拦截请求到Spring Web MVC
HandlerMapping的配置,从而将请求映射到处理器
HandlerAdapter的配置,从而支持多种类型的处理器
ViewResolver的配置,从而将逻辑视图名解析为具体视图技术
处理器(页面控制器)的配置,从而进行功能处理
把上面的五个步骤和图一一对应起来,之后的开发就是按照这五个步骤进行的!
后来我想实现一个简单的用户登录验证的例子来验证一下,毕竟实践是验证理论的唯一途径也是学习的最好方式!
没有用到数据库,没有必要,我这里只是学习SpringMVC的运行机制
首先建立一个Web项目取名为SpringLoginDemo
在导入所需要的包,如图:(下面有下载)

Spring学习之SpringMVC框架快速搭建实现用户登录功能

接下来做什么呢?看上面的那五个步骤,

1、部署web.xml

第一步部署web.xml,从而拦截请求到spring Web MVC

Spring学习之SpringMVC框架快速搭建实现用户登录功能


  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="2.5"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  6. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  7. <display-name></display-name>
  8. <welcome-file-list>
  9. <welcome-file>index.jsp</welcome-file>
  10. </welcome-file-list>
  11. <servlet>
  12. <servlet-name>Dispatcher</servlet-name>
  13. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  14. <!-- Spring配置文件 -->
  15. <init-param>
  16. <param-name>contextConfigLocation</param-name>
  17. <param-value>classpath:applicationContext.xml</param-value>
  18. </init-param>
  19. </servlet>
  20. <servlet-mapping>
  21. <servlet-name>Dispatcher</servlet-name>
  22. <url-pattern>*.do</url-pattern>
  23. </servlet-mapping>
  24. </web-app>

2、编写eneity类,User.java

Spring学习之SpringMVC框架快速搭建实现用户登录功能


  1. package com.example.entity;
  2. public class User {
  3. private String username;
  4. private String password;
  5. public User(String username, String password) {
  6. super();
  7. this.username = username;
  8. this.password = password;
  9. }
  10. public String getUsername() {
  11. return username;
  12. }
  13. public void setUsername(String username) {
  14. this.username = username;
  15. }
  16. public String getPassword() {
  17. return password;
  18. }
  19. public void setPassword(String password) {
  20. this.password = password;
  21. }
  22. }

3、LoginController.java类


  1. package com.example.controller;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import javax.servlet.http.HttpServletRequest;
  5. import javax.servlet.http.HttpServletResponse;
  6. import org.springframework.web.servlet.ModelAndView;
  7. import org.springframework.web.servlet.mvc.AbstractController;
  8. import com.example.entity.User;
  9. public class LoginController extends AbstractController {
  10. //成功与失败字段
  11. private String successView;
  12. private String failView;
  13. public String getSuccessView() {
  14. return successView;
  15. }
  16. public void setSuccessView(String successView) {
  17. this.successView = successView;
  18. }
  19. public String getFailView() {
  20. return failView;
  21. }
  22. public void setFailView(String failView) {
  23. this.failView = failView;
  24. }
  25. @Override
  26. protected ModelAndView handleRequestInternal(HttpServletRequest request,
  27. HttpServletResponse response) throws Exception {
  28. //不应该是这样写,但是这样看起来比较容易理解
  29. String username = request.getParameter("username");
  30. String password = request.getParameter("password");
  31. User user = getUser(username, password);
  32. //保存相应的参数,通过ModelAndView返回
  33. Map<String ,Object> model=new HashMap<String,Object>();
  34. if(user !=null){
  35. model.put("user", user);
  36. return new ModelAndView(getSuccessView(),model);
  37. }else{
  38. model.put("error", "用户名或密码输入错误!!!");
  39. return new ModelAndView(getFailView(),model);
  40. }
  41. }
  42. //为了方便直接写的验证方法
  43. public User getUser(String username,String password){
  44. if(username.equals("test") && password.equals("test")){
  45. return new User(username,password);
  46. }else{
  47. return null;
  48. }
  49. }
  50. }

4、核心配置applicationContext.xml


  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:tx="http://www.springframework.org/schema/tx"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
  7. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
  8. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
  9. <bean id="loginController" class="com.example.controller.LoginController">
  10. <!-- 注意这里的两个属性,对应的是两个需要跳转的页面,一个是显示用户,一个是登录失败还是登录界面 -->
  11. <property name="successView" value="showUser"></property>
  12. <property name="failView" value="login"></property>
  13. </bean>
  14. <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
  15. <property name="mappings">
  16. <props>
  17. <prop key="/login.do">loginController</prop>
  18. </props>
  19. </property>
  20. </bean>
  21. <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  22. <property name="prefix" value="/"></property>
  23. <property name="suffix" value=".jsp"></property>
  24. </bean>
  25. </beans>

来看一下图片

Spring学习之SpringMVC框架快速搭建实现用户登录功能

5、三个jsp页面

index.jsp


  1. <%@ page language="java" contentType="text/html; charset=GB18030"
  2. pageEncoding="GB18030"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=GB18030">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. <a href="login.jsp">进入</a>
  11. </body>
  12. </html>

login.jsp


  1. <%@ page language="java" contentType="text/html; charset=GB18030"
  2. pageEncoding="GB18030"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=GB18030">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. ${error }
  11. <form action="login.do" method="post">
  12. 用户登陆<br>
  13. <hr>
  14. 用户名:<input type="text" name="username"><br>
  15. 密码:<input type="text" name="password"><br>
  16. <input type="submit" value="登陆">
  17. </form>
  18. </body>
  19. </html>

showUser.jsp


  1. <%@ page language="java" contentType="text/html; charset=GB18030"
  2. pageEncoding="GB18030"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=GB18030">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. 用户信息<br>
  11. 用户名:${user.username }<br>
  12. 密码:${user.password }<br>
  13. <font color="red">欢迎登陆!!!</font><br>
  14. </body>
  15. </html>

6、启动Tomcat服务器运行

Spring学习之SpringMVC框架快速搭建实现用户登录功能

Spring学习之SpringMVC框架快速搭建实现用户登录功能

Spring学习之SpringMVC框架快速搭建实现用户登录功能

附件源码

下载地址  : https://gitee.com/KingXin666/SpringMVCDemo