1. 程式人生 > >spring web mvc訪問不了WEB-INF下靜態資源解決方案

spring web mvc訪問不了WEB-INF下靜態資源解決方案

最開始一直認為是自己設定的有問題,參照了無數網址經驗後,發現是jar包問題。之前使用的是spring3.0.5就是不行,使用spring4.1.6一切嗷嗷地正常!


1 環境搭建:

參照:問題:137.  spring--springweb mvc4.1.6環境搭建

2 專案地址:

F:\Tutorial\Java\Spring\SpringMVCTutorial

https://win-9ris1mc6f8k/svn/SmartCode/SpringCruise/SpringMVCTutorial-4.1.6

3 專案結構:


專案依賴jar包


4 關鍵點:

1、web.xml

<?xmlversion

="1.0"encoding="UTF-8"?>

<web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xmlns="http://java.sun.com/xml/ns/javaee"

    xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

    id="WebApp_ID"version="2.5">

    <!--configure the setting ofspringmvcDispatcherServlet and configure the mapping-->

  <servlet>

      <servlet-name>springmvc</servlet-name>

      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

      <init-param>

            <param-name>contextConfigLocation</param-name>

            <param-value>classpath:springmvc-servlet.xml

</param-value>

        </init-param>

        <!-- <load-on-startup>1</load-on-startup>-->

  </servlet>

  <servlet-mapping>

      <servlet-name>springmvc</servlet-name>

      <url-pattern>/</url-pattern>

  </servlet-mapping>

</web-app>

2、springmvc-servlet.xml

<?xmlversion="1.0"encoding="UTF-8"?>

<beansxmlns="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"

    xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd

       http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.1.xsd

       http://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">                   

    <!-- scan the package and the sub package -->

    <context:component-scanbase-package="test.SpringMVC"/>

    <!-- don't handle the static resource -->

    <mvc:default-servlet-handler/>

    <!-- if you use annotation you must configurefollowing setting -->

    <mvc:annotation-driven/>

    <mvc:resourcesmapping="/pages/**"location="/WEB-INF/pages/"/>

<mvc:resourcesmapping="/Pictures/**"location="/WEB-INF/Pictures/"/>

    <!-- configure the InternalResourceViewResolver -->

    <beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"

            id="internalResourceViewResolver">

        <!-- 字首 -->

<propertyname="prefix"value="/WEB-INF/jsp/"/>

<!--字尾 -->

<propertyname="suffix"value=".jsp"/>

    </bean>

</beans>

3、mvcController.java

packagetest.SpringMVC;

importorg.springframework.stereotype.Controller;

importorg.springframework.web.bind.annotation.RequestMapping;

@Controller

@RequestMapping("/mvc")

publicclassmvcController {

    @RequestMapping("/hello")

    public String hello(){       

        return"hello";

    }

    @RequestMapping("/staticPage")//value = "/staticPage", method = RequestMethod.GET)

    public String redirect(){    

       return"redirect:/pages/final.htm";

    }

    @RequestMapping("/staticPictures")//value = "/staticPage", method =RequestMethod.GET)

    public StringredirectPictures() {    

       return"redirect:/Pictures/Jackie.jpg";

    }

}

4、hello.jsp

<%@ pagelanguage="java"contentType="text/html;charset=ISO-8859-1"

    pageEncoding="ISO-8859-1"%>

<!DOCTYPEhtmlPUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<metahttp-equiv="Content-Type"content="text/html; charset=ISO-8859-1">

<title>Insert title here</title>

</head>

<body>

Hello World SPRING!

</body>

</html>

5、final.htm

<html>

<head>

    <title>Spring Static Page</title>

</head>

<body>

<h2>A simple HTML page</h2>

</body>

</html>

138.5 測試網址:

未配置正常效果:

description The requested resource is not available.

HTTP Status 404 - /SpringMVCTutorial/Pictures/Jackie.jpg


http://localhost:8060/SpringMVCTutorial/Pictures/Jackie.jpg

正常後效果:


http://localhost:8060/SpringMVCTutorial/mvc/hello