其他分享
首页 > 其他分享> > SpringMVC 07: WEB-INF下的资源访问 + SpringMVC拦截器

SpringMVC 07: WEB-INF下的资源访问 + SpringMVC拦截器

作者:互联网

WBE-INF目录下的资源访问

image

    <!-- 添加视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 配置前缀-->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <!-- 配置后缀-->
        <property name="suffix" value=".jsp"/>
    </bean>
package com.example.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class WebInfAction {

    @RequestMapping("/showIndex")
    public String showIndex(){
        return "index";
    }

    @RequestMapping("/showMain")
    public String showMain(){
        return "main";
    }
}

image

image

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>index.jsp</title>
</head>
<body>
<a href="${pageContext.request.contextPath}/showLogin">去登陆</a>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>login.jsp</title>
</head>
<body>

<!-- 完成登陆信息的提交 -->
    
<h1>登陆</h1>
<form action="${pageContext.request.contextPath}/login" method="post">
    用户名:<input type="text" name="username">
    密码:<input type="password" name="password">
    <input type="submit" value="提交">
</form>
    
<!-- 如果登陆失败,显示登陆失败信息-->
${msg}
    
</body>
</html>
package com.example.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
public class WebInfAction {

    @RequestMapping("/showIndex")
    public String showIndex(){
        return "index";
    }

    @RequestMapping("/showMain")
    public String showMain(){
        return "main";
    }

    @RequestMapping("/showLogin")
    public String showLogin(){
        return "login";//转发并返回登陆页面
    }

    //对登陆信息进行判断,用户信息符合是转发跳转到main.jsp,信息不符合时,填入登陆失败的信息并重新转发到登陆页面
    @RequestMapping("/login")
    public String login(String username, String password, HttpServletRequest request){
        if(username.equals("荷包蛋") && password.equals("hebaodan")){
            return "main";
        }else{
            request.setAttribute("msg", "用户名或密码错误");
            return "login";
        }
    }
}

image

image

SpringMVC拦截器

image

    @RequestMapping("/login")
    public String login(String username, String password, HttpServletRequest request){
        if(username.equals("荷包蛋") && password.equals("hebaodan")){
            //如果登陆成功,则设置session信息
            request.getSession().setAttribute("user", username);
            return "main";
        }else{
            request.setAttribute("msg", "用户名或密码错误");
            return "login";
        }
    }
package com.example.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("user") == null){
            request.setAttribute("msg", "还未登陆,清先登陆");
            //打回登陆页面(页面转发实现页面跳转)
            request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response);
            return false;//验证信息未通过
        }
        return true;//验证信息通过,放行
    }
}
    <!-- 配置SpringMVC拦截器-->
    <mvc:interceptors>
        <!-- 可以配置多个拦截规则,形成拦截链-->
        <mvn:interceptor>
            <!-- 映射要拦截的请求:所有请求-->
            <mvn:mapping path="/**"/>
            <!-- 设置要放行的请求:有些必备的登陆请求不能拦截-->
            <mvn:exclude-mapping path="/showLogin"/>
            <mvn:exclude-mapping path="/login"/>
            <!-- 配置实现拦截器功能的实现类-->
            <bean class="com.example.interceptor.LoginInterceptor"/>
        </mvn:interceptor>
    </mvc:interceptors>

image

image

标签:WEB,拦截器,return,SpringMVC,jsp,登陆,INF
来源: https://www.cnblogs.com/nefu-wangxun/p/16652364.html