springmvc+jsp 拦截器下如何设置欢迎页面
Posted An
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springmvc+jsp 拦截器下如何设置欢迎页面相关的知识,希望对你有一定的参考价值。
0.需求
如何让用户在浏览器地址栏键入[http://XXX.XXX.XX.XX:端口号/应用名/]以后自动跳转到系统的登录界面
1.web.xml
1.1 注意welcome-file-list的配置,将welcome-file赋值为"/"
1.2 springmvc 的 dispatcherServlet的url-pattern是"/"
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>leslie</display-name> <welcome-file-list> <welcome-file>/</welcome-file> </welcome-file-list> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mybatis.xml,/WEB-INF/leslie-dispatcher-servlet.xml,/WEB-INF/applicationContext-timertask.xml </param-value> </context-param> <!-- 编码过滤器 --> <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> <!-- Spring监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 防止Spring内存溢出监听器 --> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <servlet> <servlet-name>leslie-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>leslie-dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>logbackConfigLocation</param-name> <param-value>classpath:logback.xml</param-value> </context-param> <listener> <listener-class>ch.qos.logback.ext.spring.web.LogbackConfigListener</listener-class> </listener> <context-param> <param-name>spring.profiles.active</param-name> <param-value>dev</param-value> </context-param> <context-param> <param-name>spring.profiles.default</param-name> <param-value>dev</param-value> </context-param> <context-param> <param-name>spring.liveBeansView.mbeanDomain</param-name> <param-value>dev</param-value> </context-param> </web-app>
2.dispatcherServlet配置文件
2.1 注意我配置了一个判断用户是否登录的拦截器
<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" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"> <context:component-scan base-package="com.leslie" /> <mvc:annotation-driven /> <!--url拦截器,对指定的Url进行拦截,判断Session中是否存在用户信息,防止用户通过直接输入url的方式越级访问 --> <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**" /> <bean class="com.leslie.interceptor.UserLoginInterceptor" /> </mvc:interceptor> </mvc:interceptors> <!--国际化property文件 --> <!-- <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basenames"> <list> <value>classpath:/messages/messages</value> </list> </property> <property name="useCodeAsDefaultMessage" value="true" /> </bean> --> <!--避免IE执行AJAX时,返回JSON出现下载文件 --> <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> </list> </property> </bean> <!-- 启动SpringMVC的注解功能,完成请求和注解POJO的映射 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="mappingJacksonHttpMessageConverter" /> <!-- JSON转换器 --> </list> </property> </bean> <!-- 定义跳转的文件的前后缀 ,视图模式配置 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> <!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 默认编码 --> <property name="defaultEncoding" value="utf-8" /> <!-- 文件大小最大值 --> <property name="maxUploadSize" value="10485760000" /> <!-- 内存中的最大值 --> <property name="maxInMemorySize" value="40960" /> </bean> </beans>
2.2 拦截器的实现代码
2.2.1 字符串数组IGNORE_URI定义了拦截器不拦截的uri,包括用户登录界面以及check用户登录信息两个逻辑是不拦截的。
preHandle方法在执行controller之前被执行,主要的逻辑是首先看当前url是不是在IGNORE_URI中定义的,
如果是就放行,返回true。如果不是,就从session里取用户登录信息,如果取到了用户对象则视为用户已经登录,那么放行,返回true。否则,视为用户未登录,不放行,返回false。
2.2.2 我们的目的是让拦截器为"/"放行,但我们不能将"/"配置到IGNORE_URI里,因为"/"表示任意的url-pattern,这样拦截器势必会对所有的url放行,失去了
拦截的意义。所以就有了requestUrl.endsWith(applicationName)这个逻辑,也就是手动判断当前url是不是[http://XXX.XXX.XX.XX:端口号/应用名/]风格的,如果是就放行。
package com.leslie.interceptor; import java.io.File; import javax.interceptor.Interceptor; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import com.leslie.controller.SJLoginController; import com.leslie.model.User; /** * 登录拦截器 * * */ @Interceptor public class UserLoginInterceptor extends HandlerInterceptorAdapter { private static final String[] IGNORE_URI = { "/loginPage", "/checkLogin", "/hello" }; @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { String applicationName = "/" + request.getContextPath().split("/")[request.getContextPath().split("/").length - 1] + "/"; String requestUrl = request.getRequestURL().toString(); if (requestUrl.endsWith(applicationName)) { return true; } boolean flag = false; String url = request.getRequestURL().toString(); for (String s : IGNORE_URI) { if (url.contains(s)) { flag = true; break; } } if (!flag) { User user = SJLoginController.getLoginUser(request); if (user != null) { // List<String> urls = // SJLoginController.getUserRoleUrls(request); // if(urls.contains(url)){ // flag = true; // } flag = true; } } return flag; } }
3.为欢迎页面单独分配一个controller
3.1 使用forward将页面跳转到兄弟controller,在这里是用户登录controller的打开登录页面url
package com.leslie.util; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller public class SJWelcomeController { @RequestMapping("/") public String welcome(){ return "forward:/login/loginPage"; } }
以上是关于springmvc+jsp 拦截器下如何设置欢迎页面的主要内容,如果未能解决你的问题,请参考以下文章
请问,java高手,spring mvc拦截器如何拦截所有的请求啊,包括html和jsp页面?