zoukankan      html  css  js  c++  java
  • Spring MVC 拦截器

    SpringMvc Interceptor拦截器的配置与使用

    参考

    loginForm.jsp

    <%--
      Created by IntelliJ IDEA.
      User: mythsky
      Date: 2017/12/5
      Time: 21:57
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>登录页面</title>
    </head>
    <body>
        <h3>登录页面</h3>
        <form action="login" method="post">
            <font color="red">${requestScope.message}</font>
            <table>
                <tr>
                    <td><label>登录名:</label></td>
                    <td><input type="text" id="loginname" name="loginname"></td>
                </tr>
                <tr>
                    <td><label>密码:</label></td>
                    <td><input type="text" id="password" name="password"></td>
                </tr>
                <tr><td><input type="submit" value="登录"></td></tr>
            </table>
        </form>
    </body>
    </html>
    View Code

    main.jsp

    <%--
      Created by IntelliJ IDEA.
      User: mythsky
      Date: 2017/12/5
      Time: 22:04
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>书籍</title>
    </head>
    <body>
        <h3>欢迎[${sessionScope.user.username}]</h3>
        <br>
        <p>${requestScope.book.bookname}</p>
    </body>
    </html>
    View Code

    User

    package org.mythsky.springmvcdemo.model;
    
    import org.springframework.format.annotation.DateTimeFormat;
    import org.springframework.web.multipart.MultipartFile;
    
    import java.io.Serializable;
    import java.util.Date;
    
    public class User implements Serializable {
        private String loginname;
        private String username;
        private String password;
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    
        public String getUsername() {
            return username;
        }
    
        public void setUsername(String username) {
            this.username = username;
        }
    
        private Date birthday;
        private MultipartFile image;
    
        public MultipartFile getImage() {
            return image;
        }
    
        public void setImage(MultipartFile image) {
            this.image = image;
        }
    
        public User() {
            super();
        }
    
        public String getLoginname() {
            return loginname;
        }
    
        public void setLoginname(String loginname) {
            this.loginname = loginname;
        }
    
        public Date getBirthday() {
            return birthday;
        }
    
        public void setBirthday(Date birthday) {
            this.birthday = birthday;
        }
    }
    View Code

    UserController

    package org.mythsky.springmvcdemo.controller;
    
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.mythsky.springmvcdemo.converter.DateEditor;
    import org.mythsky.springmvcdemo.model.User;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.WebDataBinder;
    import org.springframework.web.bind.annotation.*;
    import org.springframework.web.servlet.ModelAndView;
    
    import javax.servlet.http.HttpSession;
    import java.util.Date;
    
    @Controller
    public class UserController {
        private static final Log logger= LogFactory.getLog(UserController.class);
        @InitBinder
        public void initBinder(WebDataBinder binder){
            binder.registerCustomEditor(Date.class,new DateEditor());
        }
        @RequestMapping(value = "/{formName}")
        public String loginForm(@PathVariable String formName){
            return formName;
        }
        @RequestMapping(value = "/register",method = RequestMethod.POST)
        public String register(@ModelAttribute User user,Model model){
            logger.info(user);
            model.addAttribute("user",user);
            return "success";
        }
        @RequestMapping(value = "/login")
        public ModelAndView login(String loginname, String password, ModelAndView mv, HttpSession session){
            if(loginname!=null&&loginname.equals("tom")&&password!=null&password.equals("123456")){
                User user=new User();
                user.setLoginname(loginname);
                user.setPassword(password);
                user.setUsername("管理员");
                session.setAttribute("user",user);
                mv.setViewName("redirect:main");
            }else{
                mv.addObject("message","登录名或密码错误,请重新输入!");
                mv.setViewName("loginForm");
            }
            return mv;
        }
    }
    View Code

    BookController

    package org.mythsky.springmvcdemo.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    public class BookController {
        @RequestMapping(value = "/main")
        public String main(Model model){
            model.addAttribute("bookname","hello world");
            return "main";
        }
    }
    View Code

    services.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:context="http://www.springframework.org/schema/context"
           xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
           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="org.mythsky.springmvcdemo"></context:component-scan>
        <!--<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>-->
        <!--<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>-->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/views/"></property>
            <property name="suffix" value=".jsp"></property>
        </bean>
        <mvc:annotation-driven></mvc:annotation-driven>
        <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
            <!--上传文件大小限制,单位为字节(10MB)-->
            <property name="maxUploadSize">
                <value>10485760</value>
            </property>
            <property name="defaultEncoding">
                <value>UTF-8</value>
            </property>
        </bean>
        <mvc:interceptors>
            <mvc:interceptor>
                <mvc:mapping path="/*"/>
                <bean class="org.mythsky.springmvcdemo.interceptor.AuthorizationInterceptor"></bean>
            </mvc:interceptor>
        </mvc:interceptors>
    </beans>
    View Code

    登录页面

    成功登陆页面

  • 相关阅读:
    POJ 3977 折半枚举
    [CQOI2007]余数求和 (分块+数学
    NOI P1896 互不侵犯 状压DP
    HDU 5446 Unknown Treasure (卢卡斯+CRT
    宁夏邀请赛F FLOYD
    P1414 又是毕业季II (数学?
    P2051 [AHOI2009]中国象棋 DP
    POJ 2449:Remmarguts' Date(A* + SPFA)
    HDU 6215:Brute Force Sorting(链表+队列)
    HDU 6207:Apple(Java高精度)
  • 原文地址:https://www.cnblogs.com/uptothesky/p/7989738.html
Copyright © 2011-2022 走看看