不登录不能进入main.jsp
一.搭环境
跟学习笔记二相同,不同的做如下说明
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 扫描注解 -->
<context:component-scan base-package="com.bjsxt.controller"></context:component-scan>
<!-- 注解驱动 -->
<!-- org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping -->
<!-- org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 静态资源 -->
<mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
<mvc:resources location="/css/" mapping="/css/**"></mvc:resources>
<mvc:resources location="/images/" mapping="/images/**"></mvc:resources>
<mvc:resources location="/files/" mapping="/files/**"></mvc:resources>
<!-- 视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/page/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 配置拦截器 -->
<mvc:interceptors>
<bean class="com.bjsxt.interceptor.LoginInterceptor"></bean>
</mvc:interceptors>
</beans>
二.后端代码
1.pojo
Users
package com.bjsxt.pojo;
public class Users {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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;
}
}
2.controller
DemoController.java
package com.bjsxt.controller;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import com.bjsxt.pojo.Users;
@Controller
public class DemoController {
@RequestMapping("{page}")
public String main(@PathVariable String page){
System.out.println("restful");
return page;
}
@RequestMapping("login")
public String login(Users users,HttpSession session){
//登录成功,需要放到main,并且存到session作用域
if(users.getUsername().equals("admin")&&users.getPassword().equals("123")){
session.setAttribute("users", users);
return "main";
}else{
return "redirect:/login.jsp";
}
}
}
3.interceptor
package com.bjsxt.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
public class LoginInterceptor implements HandlerInterceptor {
@Override
public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
// TODO Auto-generated method stub
}
@Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
throws Exception {
// TODO Auto-generated method stub
}
@Override
public boolean preHandle(HttpServletRequest req, HttpServletResponse res, Object arg2) throws Exception {
String uri = req.getRequestURI();
if(uri.equals("/springmvc03/login")){//如果请求的是login的控制器
return true;
}else{
Object obj = req.getSession().getAttribute("users");
if(obj!=null){
return true;
}
res.sendRedirect("/springmvc03/login.jsp");//重定向跳转到的/表示服务器根 目录,所以要在/前面加上项目名/springmvc03
return false;
}
}
}
三.前端页面
在WEB-INF下新建page文件夹,在page下新建main.jsp等
1.main.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
mian.jsp
</body>
</html>
1.login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="login" method="post">
用户名:<input type="text" name="username"/><br/>
密码:<input type="password" name="password"/><br/>
<input type="submit" value="登录"/>
</form>
</body>
</html>
四.总结
使用springmvc拦截器实现登陆验证
1. 把页面放入到WEB-INF中.
1.1 放入到WEB-INF中后必须通过控制器转发到页面.
1.2 springmvc拦截器拦截的是控制器,不能拦截jsp
2. 通过拦截器拦截全部控制器,需要在拦截器内部放行login控制器