shiro 静态页面资源不显示 解决方案

最近作一个ssm+shiro的框架整和javascript

不加shiro以前ssm中css和图片显示正常。加上之后没法显示。css

解决方案:html

 shiro有静态资源过滤。java

  配置资源匿名访问便可jquery

<property name="filterChainDefinitions">
            <value>
                /css/**= anon
                /images/**= anon
                /img/** =anon
                /js/** =anon
                
                /login.jsp = anon
         
                # everything else requires authentication:
                /** = authc
            </value>
    </property>

 

配置成功仍是没法显示程序员

配置springMVC.xmlweb

 

SpringMVC提供<mvc:resources>来设置静态资源,可是增长该设置若是采用通配符的方式增长拦截器的话仍然会被拦截器拦截,可采用以下方案进行解决:spring

方案1、拦截器中增长针对静态资源不进行过滤(涉及spring-mvc.xml)bootstrap

复制代码
 1 <mvc:resources location="/" mapping="/**/*.js"/>  2 <mvc:resources location="/" mapping="/**/*.css"/>  3 <mvc:resources location="/assets/" mapping="/assets/**/*"/>  4 <mvc:resources location="/images/" mapping="/images/*" cache-period="360000"/>  5  6 <mvc:interceptors>  7 <mvc:interceptor>  8 <mvc:mapping path="/**/*"/>  9 <mvc:exclude-mapping path="/**/fonts/*"/> 10 <mvc:exclude-mapping path="/**/*.css"/> 11 <mvc:exclude-mapping path="/**/*.js"/> 12 <mvc:exclude-mapping path="/**/*.png"/> 13 <mvc:exclude-mapping path="/**/*.gif"/> 14 <mvc:exclude-mapping path="/**/*.jpg"/> 15 <mvc:exclude-mapping path="/**/*.jpeg"/> 16 <mvc:exclude-mapping path="/**/*login*"/> 17 <mvc:exclude-mapping path="/**/*Login*"/> 18 <bean class="com.luwei.console.mg.interceptor.VisitInterceptor"></bean> 19 </mvc:interceptor> 20 </mvc:interceptors>
复制代码

 

方案2、使用默认的静态资源处理Servlet处理静态资源(涉及spring-mvc.xml, web.xml)浏览器

在spring-mvc.xml中启用默认Servlet

1 <mvc:default-servlet-handler/>

在web.xml中增长对静态资源的处理

复制代码
1 <servlet-mapping> 2 <servlet-name>default</servlet-name> 3 <url-pattern>*.js</url-pattern> 4 <url-pattern>*.css</url-pattern> 5 <url-pattern>/assets/*"</url-pattern> 6 <url-pattern>/images/*</url-pattern> 7 </servlet-mapping> 
复制代码

可是当前的设置必须在Spring的Dispatcher的前面

 

方案3、修改Spring的全局拦截设置为*.do的拦截(涉及web.xml)

复制代码
 1 <servlet>  2 <servlet-name>SpringMVC</servlet-name>  3 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  4 <init-param>  5 <param-name>contextConfigLocation</param-name>  6 <param-value>classpath:spring-mvc.xml</param-value>  7 </init-param>  8 <load-on-startup>1</load-on-startup>  9 <async-supported>true</async-supported> 10 </servlet> 11 <servlet-mapping> 12 <servlet-name>SpringMVC</servlet-name> 13 <url-pattern>*.do</url-pattern> 14 </servlet-mapping>
复制代码

这样设置,Spring就会只针对以'.do'结尾的请求进行处理,再也不维护静态资源

 

针对这三种方案的优劣分析:

第一种方案配置比较臃肿,多个拦截器时增长文件行数,不推荐使用;第二种方案使用默认的Servlet进行资源文件的访问,Spring拦截全部请求,而后再将资源文件交由默认的Sevlet进行处理,性能上少有损耗;第三种方案Spring只是处理以'.do'结尾的访问,性能上更加高效,可是再访问路径上必须都以'.do'结尾,URL不太文雅;

综上所述,推荐使用第二和第三中方案

转载https://www.cnblogs.com/banning/p/6195072.html

博主用的是

<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>SsmS</display-name>
  <welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
    <welcome-file>index1.jsp</welcome-file>
     
  </welcome-file-list>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <listener>
    <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
  </listener>
  <filter>
    <filter-name>encoding</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>encoding</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <servlet>
    <servlet-name>springDispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
 <servlet-mapping>
    <servlet-name>springDispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping> 
  
  
  <filter>
    <filter-name>shiroFilter</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    <init-param>
      <param-name>targetFilterLifecycle</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>shiroFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

springmvc.xml

         <mvc:annotation-driven />
         <mvc:default-servlet-handler />
        
         <!-- 静态资源过滤 -->
         <mvc:resources location="/resources/" mapping="/resources/**"/>
         <mvc:resources location="/css/" mapping="/css/**" cache-period="2592000"/>
        <mvc:resources  location="/img/" mapping="/img/**" cache-period="2592000"/> 
          <mvc:resources location="/js/" mapping="/js/**" cache-period="2592000"/>
          <mvc:resources  location="/images/" mapping="/images/**" cache-period="2592000"/>

重点来了困扰了三天

然而博主配置了shiro和springmvc后登陆的静态页面是能够显示了

可是页面里边的样式没法显示,火狐控制台查看仍是404。此去省略一万字。

解决办法:页面中的全部路径和请求都加上它 <%=request.getContextPath()%>

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link type="text/css" rel="stylesheet" href="<%=request.getContextPath()%>/css/bootstrap.css">
<link href="<%=request.getContextPath()%>/css/common.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/css/jquery.datetimepicker.css"/>
<script type="text/javascript" src="<%=request.getContextPath()%>/js/jquery.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/js/bootstrap.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/js/Validform_v5.3.2.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/js/jquery.datetimepicker.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/js/popwin.js"></script>

<%=request.getContextPath()%>

这篇博主写的不错转载:http://blog.csdn.net/u010010428/article/details/51246491

作的一个web项目,须要在jsp页面中获取js、css和图片等资源,本想采用相对路径来写,可是发现可移植性太差,在本身电脑上还好使,但辛辛苦苦调好代码后,放到其余电脑上又得再次辛辛苦苦修改相关路径。因而决定采用绝对路径来写。而后在网上找寻相关方法,发现用的比较多的两个:${pageContext.request.contextPath}和<%=request.getContextPath()%>,但具体用哪一个我也不大清楚,因而继续查找二者的区别,但让我郁闷的是,网上“抄袭”的真多啊!并且说了一大堆!满是些不痛不痒的专业名词,关键的真没几个!因此我决定靠本身理解,如今明白了!我想用一种比较通俗的语言分享一下我对此的认识!

      能够说${pageContext.request.contextPath}等价于<%=request.getContextPath()%>!它们的意思就是取得当前项目名称(或者是--取出部署的应用程序名,不过这么说太官方了,试问有多少人知道“取出部署的应用程序名”的义)
      那么什么是当前的项目名称呢?

      

      假定你的web应用名称为hotel,这个hotel就是当前的项目名称,不过你在浏览器中输入请求路径时,例如输入http//:localhost:8080/hotel/login.jsp 

      ${pageContext.request.contextPath}或<%=request.getContextPath()%>就是从这个请求路径(URL)上截取(是截取) /hotel ,看清楚,前面是有"/",而这个“/”表明的是"http//:localhost:8080",看清楚这里是没有"/"的!

      对应到真是的物理地址,即为图中WebContent目录!

      另外,若是你是在Tomcat的server.xml文件中配置了虚拟目录,例如

      

      那么咱们在对应的物理目录F:\javaweb中建立test_jsp.jsp文件,内容为

       

      开启Tomcat,进行测试,发现输出结果为

      

      能够看到,此时输出为虚拟目录,而且两者彻底等价!

      所以,在表单<formaction="${pageContext.request.contextPath}/hotel/login.jsp">中,这样写路径永远对,翻译过来${pageContext.request.contextPath}/hotel/login.jsp其中的含义,就是http//:localhost:8080/hotel/login.jsp,至关于你写了一全路径!固然前提是你的JSP页面等等必须放置的位置正确才能够,因此才说明路径永远正确。

       为何这么要作呢?由于学过一些MVC模式的程序员都知道,JSP的做用是用来显示的(表现的),其余的做用最好不要在这个页面上显示,尤为是Java代码!因此就用EL表达式来替代相似有“<%%>”这样的代码格式。

相关文章
相关标签/搜索