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

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赵海燕

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值