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表達式來替代相似有「<%%>」這樣的代碼格式。

相關文章
相關標籤/搜索