学习了一周的springMVC
今天搭建一个SpringMVC的登录案例
主要是用来巩固一下拦截器的是使用。
1.首先配置web.xml 配置
主要是配置encodingfilter和dispatcher
<?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_3_1.xsd"
version="3.1">
<!--字符集设置-->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--调度器配置-->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.form</url-pattern>
</servlet-mapping>
</web-app>
2.配置dispatcher指定的servlet.xml
主要配置拦截器的相关属性
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--指定注解解析的包-->
<context:component-scan base-package="com.jsu.controller"/>
<!--配置拦截器-->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<!-- 指定拦截器的bean -->
<bean class="com.jsu.intercepter.LoginIntercepter">
<property name="allowAdress">
<!--允许通过的请求的列表-->
<list>
<value>login.form</value>
<value>add.form</value>
</list>
</property>
</bean>
</mvc:interceptor>
</mvc:interceptors>
</beans>
前车之鉴:千万不能使用如下配置不然会无法识别拦截器的拦截名单
<property name="allowAdress">
<!--允许通过的请求的列表-->
<list>
<value>
login.form
</value>
<value>
add.form
</value>
</list>
</property>
3.配置intercepter的类
主要通过实现handlerintercepter接口
public class LoginIntercepter implements HandlerInterceptor {
//拦截名单
List<String> allowAdress;
public void setAllowAdress(List<String> allowAdress) {
this.allowAdress = allowAdress;
}
/**
* 请求方法执行前所执行的方法
* 返回true 则进行拦截
* 否则不进行拦截
*/
@Override
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
//获取请求的路径
String url = httpServletRequest.getRequestURL().toString();
//查看是否已经有请求
Object user = httpServletRequest.getSession().getAttribute("user");
if (user != null){
return true;
}
for (String temp : allowAdress) {
if (url.endsWith(temp)) {
return true;
}
}
//重定向到login页面
httpServletResponse.sendRedirect(httpServletRequest.getContextPath()+"/login.jsp");
return false;
}
@Override
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
}
}
4.配置controller类
@Controller
public class LoginController {
@RequestMapping("/login")
public String login(User user,HttpSession session){
//校验
if ("zzz".equals(user.getName())&&"123".equals(user.getPassword())) {
session.setAttribute("user", user);
return "redirect:index.jsp";
}
return "redirect:login.jsp";
}
@RequestMapping("/add")
public String add(){
System.out.println("add");
return "redirect:index.jsp";
}
}
5.配置实体类
public class User{
private String name;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
6.demo结果
错误结果就重定向到login.jsp页面
时间: 2024-11-08 21:49:06