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

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

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

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

解决方案:

 shiro有静态资源过滤。

  配置资源匿名访问即可

复制代码

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

复制代码

配置成功还是无法显示

配置springMVC.xml

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

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

复制代码

复制代码

 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>

复制代码

复制代码

方案二、使用默认的静态资源处理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的前面

方案三、修改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表达式来替代类似有“<%%>”这样的代码格式。

猜你喜欢

转载自blog.csdn.net/cxu123321/article/details/86319453
今日推荐