初步实现基础登录(通过路径可以直接访问,不符合实际,需要添加拦截器)
项目目录
登录页面 login
<%--
Created by IntelliJ IDEA.
User: HP
Date: 2022-07-06
Time: 18:49
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录</title>
</head>
<body>
<br>
<br>
<br>
<br>
<h1>登录账户</h1>
<form action="${pageContext.request.contextPath}/login">
用户名:<input name="name"><br>
密码:<input type="password" name="password">
<input type="submit" value="登录">
</form>
<br>
<br>
${message}
</body>
</html>
处理器 WebAction
package com.ys.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
@Controller
public class WebAction {
// 进入登录页面
@RequestMapping("/showLogin")
public String show3(){
System.out.println("访问login页面");
return "login";
}
// 登录的业务判断,账号密码正确返回main页面,错误则重新进入login页面并输出错误提示
@RequestMapping("/login")
public String login(String name, String password, HttpServletRequest request){
if("admin".equalsIgnoreCase(name)&&"123456".equalsIgnoreCase(password)){
return "main";
}else {
request.setAttribute("message","账户或密码错误,请重新登录");
return "login";
}
}
}
登录成功页面 main
<%--
Created by IntelliJ IDEA.
User: HP
Date: 2022-07-06
Time: 15:49
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h2>main............登录成功!</h2>
</body>
</html>
服务器启动展示页面 index
<%--
Created by IntelliJ IDEA.
User: HP
Date: 2022-06-25
Time: 16:06
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<br>
<br>
<br>
<a href="${pageContext.request.contextPath}/showLogin">登录界面</a><br>
</body>
</html>
Springmvc配置文件springmvc
<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 添加包扫描-->
<context:component-scan base-package="com.ys.controller"></context:component-scan>
<!-- 添加视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置前缀-->
<!-- value的值是指定跳转到包名 -->
<property name="prefix" value="/WEB-INF/jsp/"></property>
<!-- 配置后缀-->
<!-- value的值是确定页面的后缀 -->
<property name="suffix" value=".jsp"></property>
<!-- 将前后缀进行拼接加上返回值就是跳转的页面-->
</bean>
<!-- 添加注解驱动(ajax和date都必须要)-->
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
maven项目依赖 pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>SpringMVC_006</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<!-- 添加jstl依赖,用于在页面上遍历,或使用函数时提供支持-->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
webapp配置 web
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!-- 中文编码过滤器配置-->
<filter>
<filter-name>encode</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<!-- 查看CharacterEncodingFilter中,得到的三个变量
private String encoding; 字符格式
private boolean forceRequestEncoding = false; 请求的强制转换编码
private boolean forceResponseEncoding = false; 响应的强制转换编码
-->
<!-- 配置参数-->
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceRequestEncoding</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>forceResponseEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encode</filter-name>
<url-pattern>/*</url-pattern>
<!-- 拦截所有请求-->
</filter-mapping>
<!-- 注册SpringMVC框架-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
启动Tomcat服务器后如果报错,注意查看target是否存在springmvc文件
拦截器
针对请求和响应进行的额外的处理,在请求和响应的过程中添加预处理,后处理和最终处理。
1.preHandle():在请求被处理之前进行操作----预处理(权限验证都是在预处理中进行)
2.postHandle():在请求被处理之后,但结果还没有渲染前进行操作,可以改变响应结果----后处理
3.afterCompletio:所有的请求响应结束后执行善后工作,清理对象,关闭资源----最终处理
拦截器的两种实现方式
1.继承HandlerInterceptorAdapter的父类(单一)
2.实现HandlerInterceptor接口,推荐使用实现接口
拦截器的实现步骤
1.改造登录方法,在session中存储用户信息,用于进行权限验证
package com.ys.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
@Controller
public class WebAction {
@RequestMapping("/showIndex")
public String show1(){
System.out.println("访问index页面");
return "index";
}
@RequestMapping("/showMain")
public String show2(){
System.out.println("访问main页面");
return "main";
}
// 进入登录页面
@RequestMapping("/showLogin")
public String show3(){
System.out.println("访问login页面");
return "login";
}
// 登录的业务判断,账号密码正确返回main页面,错误则重新进入login页面并输出错误提示
@RequestMapping("/login")
public String login(String name, String password, HttpServletRequest request){
if("admin".equalsIgnoreCase(name)&&"123456".equalsIgnoreCase(password)){
// 将name传入Session中
request.getSession().setAttribute("users",name);
return "main";
}else {
request.setAttribute("message","账户或密码错误,请重新登录");
return "login";
}
}
}
2.开发拦截器的功能,实现HandlerInterceptor接口,重写preHandle()方法
package com.ys.interceptor;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
// 是否登录过的判断,取出Session中的数据做判断
if(request.getSession().getAttribute("users")== null){
// 当Session中没有存放信息为空时,则说明没有登录,则进入到登录页面
request.setAttribute("message","还没有登录,请先去登录");
// 跳转到index页面
request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request,response);
return false;
}
// 放行请求
return true;
}
}
3.在springmvc配置文件中注册拦截器
<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 添加包扫描-->
<context:component-scan base-package="com.ys.controller"></context:component-scan>
<!-- 添加视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置前缀-->
<!-- value的值是指定跳转到包名 -->
<property name="prefix" value="/WEB-INF/jsp/"></property>
<!-- 配置后缀-->
<!-- value的值是确定页面的后缀 -->
<property name="suffix" value=".jsp"></property>
<!-- 将前后缀进行拼接加上返回值就是跳转的页面-->
</bean>
<!-- 添加注解驱动(ajax和date都必须要)-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--注册拦截器-->
<!-- 多个拦截器可以自动形成拦截器链-->
<mvc:interceptors>
<mvc:interceptor>
<!-- 拦截所有请求-->
<mvc:mapping path="/**"/>
<!-- 设置拦截之后直接放行的请求-->
<mvc:exclude-mapping path="/showLogin"/>
<mvc:exclude-mapping path="/login"/>
<!-- 配置具体的拦截器实现功能的类-->
<bean class="com.ys.interceptor.LoginInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
</beans>
如果拦截器不能生效,则需要查看target中的springmvc文件是否改变,如果没有需要把resources下的springmvc粘贴过去进行覆盖