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服务器运行
附件源码
Spring学习之SpringMVC框架快速搭建实现用户登录功能的更多相关文章
- Struts+Hibernate+Spring实现用户登录功能
通过登录案例实现三大框架之间的整合,登录功能是任何系统和软件必不可少的一个模块,然而通过这个模块来认识这些复杂的框架技术,理解数据流向和整个设计思路是相当容易的.只有在掌握了这些小模块的应用后,才能轻 ...
- JavaWeb学习记录(六)——用户登录功能
使用JDBC.spring框架.servlet实现一个简单的用户登录功能. 一.mySql数据库 SET FOREIGN_KEY_CHECKS=0; -- ---------------------- ...
- sau交流学习社区第三方登陆github--oauth来实现用户登录
sau交流学习社区第三方登陆github--oauth来实现用户登录 最近在丰富nodejsBlog开发的“交流学习社区”(https://www.mwcxs.top)的其他功能以及修复一些bug. ...
- 使用 Flask 框架写用户登录功能的Demo时碰到的各种坑(五)——实现注册功能
使用 Flask 框架写用户登录功能的Demo时碰到的各种坑(一)——创建应用 使用 Flask 框架写用户登录功能的Demo时碰到的各种坑(二)——使用蓝图功能进行模块化 使用 Flask 框架写用 ...
- 使用 Flask 框架写用户登录功能的Demo时碰到的各种坑(四)——对 run.py 的调整
使用 Flask 框架写用户登录功能的Demo时碰到的各种坑(一)——创建应用 使用 Flask 框架写用户登录功能的Demo时碰到的各种坑(二)——使用蓝图功能进行模块化 使用 Flask 框架写用 ...
- 使用 Flask 框架写用户登录功能的Demo时碰到的各种坑(一)——创建应用
使用 Flask 框架写用户登录功能的Demo时碰到的各种坑(一)——创建应用 使用 Flask 框架写用户登录功能的Demo时碰到的各种坑(二)——使用蓝图功能进行模块化 使用 Flask 框架写用 ...
- 使用 Flask 框架写用户登录功能的Demo时碰到的各种坑(二)——使用蓝图功能进行模块化
使用 Flask 框架写用户登录功能的Demo时碰到的各种坑(一)——创建应用 使用 Flask 框架写用户登录功能的Demo时碰到的各种坑(二)——使用蓝图功能进行模块化 使用 Flask 框架写用 ...
- 使用 Flask 框架写用户登录功能的Demo时碰到的各种坑(三)——使用Flask-Login库实现登录功能
使用 Flask 框架写用户登录功能的Demo时碰到的各种坑(一)——创建应用 使用 Flask 框架写用户登录功能的Demo时碰到的各种坑(二)——使用蓝图功能进行模块化 使用 Flask 框架写用 ...
- ASP.NET MVC项目框架快速搭建实战
MVC项目搭建笔记---- 项目框架采用ASP.NET MVC+Entity Framwork+Spring.Net等技术搭建,采用”Domain Model as View Model“的MVC开发 ...
随机推荐
- Day4作业及默写
1,写代码,有如下列表,按照要求实现每一个功能 li = ["alex", "WuSir", "ritian", "barry&q ...
- Python 命名元组
from collections import namedtuple # 类 p = namedtuple("Point", ["x", "y&quo ...
- posix信号量与互斥锁
1.简介 POSIX信号量是一个sem_t 类型的变量,但POSIX 有两种信号量的实现机制:无名信号量和命名信号量.无名信号量可以用在共享内存的情况下, 比如实现进程中各个线程之间的互斥和同步.命名 ...
- 1.4 Chrome浏览器
1.4 Chrome浏览器 前言selenium2启动Chrome浏览器是需要安装驱动包的,但是不同的Chrome浏览器版本号,对应的驱动文件版本号又不一样,如果版本号不匹配,是没法启动起来的. ## ...
- python之路,正则表达式
python3 正则表达式 前言: (1). 处理文本称为计算机主要工作之一(2)根据文本内容进行固定搜索是文本处理的常见工作(3)为了快速方便的处理上述问题,正则表达式技术诞生,逐渐发展为一个单独技 ...
- supervisor-program配置
[program:check_server_state]directory=/sunlight/shellcommand=/usr/bin/sh check_server_state.shautost ...
- vs2015连接mysql进行数据库操作
要求:电脑提前安装好vs,mysql. 1.在需要连接mysql的项目上右键选择“属性” -> “C/C++” -> “常规” ->选择“附加包含目录” 在弹出窗口中添加mysql的 ...
- sed 等相关的复习
sed相打印两行之间的内容: sed -n '/111/,/aad/p' fuxi.txt grep -n ".*" fuxi.txt sed -n '2,9'p fuxi.txt ...
- Brute Force Sorting(HDU6215)
题意:给你长度为n的数组,定义已经排列过的串为:相邻两项a[i],a[i+1],满足a[i]<=a[i+1].我们每次对当前数组删除非排序过的串,合并剩下的串,继续删,直到排序完成. 题解:用双 ...
- c++——数据结构
1.写一个函数PrintN,使得传入一个N,打印从1到N的全部整数 #include<stdio.h> //循环实现 void PrintN(int N){ int i; ;i<=N ...