2 bibby28 bibby28 于 2016.09.12 13:41 提问

springMVC 这里怎样才能访问到静态资源

web xml

<?xml version="1.0" encoding="UTF-8"?>
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!-- mavaen创建先修改webapp为3.1 -->

<!-- 配置DispatherServlet -->
<servlet>


    <servlet-name>spring-mvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--配置springMVC需要加载的配置文件 spring-dao.xml,spring-service.xml,spring-web.xml 
        Mybatis->spring->springMVC -->
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/spring-*.xml</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>spring-mvc</servlet-name>
    <!--默认匹配所有请求 -->
    <url-pattern>/</url-pattern>
</servlet-mapping>



<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<!--配置springmvc-->
<!--1:开始SpringMVC注解模式-->
<!--简化配置:-->
<!--1)自动注册DefaultAnnotationHandlerMapping,AnnotationMethodHandlerAdapter-->
<!--2)提供一些列:数据绑定,数字和日期的format @NumberFormat @DataTimeFormart,xml,json 默认读写支持。-->
<mvc:annotation-driven/>
<!--2.静态资源默认servlet配置-->
<!-- 
    1).加入对静态资源处理:js,gif,png
    2).允许使用 "/" 做整体映射
-->
<mvc:default-servlet-handler/>



<!--3:配置JSP 显示ViewResolver-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <property name="suffix" value=".jsp"/>
</bean>
<!--4:扫描web相关的bean-->
<context:component-scan base-package="com.triggerpoint.*"/>

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>






<!-- 启用360浏览器的极速模式(webkit) -->

<!-- 避免IE使用兼容模式 -->

<!-- 针对手持设备优化,主要是针对一些老的不识别viewport的浏览器,比如黑莓 -->

<!-- 微软的老式浏览器 -->

<!-- uc强制竖屏 -->

<!-- QQ强制竖屏 -->

<!-- UC强制全屏 -->

<!-- QQ强制全屏 -->

<!-- UC应用模式 -->

<!-- QQ应用模式 -->

<!-- windows phone 点击无高光 -->

<!-- 适应移动端end -->
水果拼团网站




<!--a1轮播图-->










<!--介绍-->



已售:948


越南进口白肉火龙果

外皮红艳,果肉绵软白嫩,汁液丰富,富含维生素C.





支付开团并邀请2人参团,人数不足自动退款






3人团




原价




        </div> 
    </div>        
    <!--拼团玩法-->
    <div class="step">
        <div class="step_hd">
            拼团玩法<a class="step_more" href="tuan_rule.html">查看详情</a>
        </div>
        <div id="footItem" class="step_list">
            <div class="step_item step_item_on">
                <div class="step_num">1</div>
                <div class="step_detail">
                    <p class="step_tit">选择
                        <br>心仪商品</p>
                </div>
            </div>
            <div class="step_item ">
                <div class="step_num">2</div>
                <div class="step_detail">
                    <p class="step_tit">支付开团
                        <br>或参团</p>
                </div>
            </div>
            <div class="step_item ">
                <div class="step_num">3</div>
                <div class="step_detail">
                    <p class="step_tit">等待好友
                        <br>参团支付</p>
                </div>
            </div>
            <div class="step_item">
                <div class="step_num">4</div>
                <div class="step_detail">
                    <p class="step_tit">达到人数
                        <br>团购成功</p>
                </div>
            </div>
        </div>
    </div>
    <div class="outs-box">
        <h1>商品详情</h1>
        <img src="/resources/images/d1.jpg">
        <img src="/resources/images/d2.jpg">
        <img src="/resources/images/d3.jpg">
    </div>
</div>

<!--detail_footer begin-->
<footer class="detail_footer">
    <div class="return_left_box">
        <a href="index.html"><div class="return_home fl"></div></a>
        <a href="javascript:void(0);"><div class="return_home share_tuan fl"></div></a>    
    </div>  
    <a href="pay.html"><div class="promptly_tuan fl">立即试用</div></a>

    <div class="cl"></div>

</footer>
<!--detail_footer end-->

<!--引入js资源-->
<script src="/resources/js/jquery.min.js"></script>
<script src="/resources/js/amazeui.js"></script>
<script src="/resources/js/handlebars.min.js"></script>
<script src="/resources/js/amazeui.widgets.helper.js"></script>


8个回答

bibby28
bibby28   2016.09.17 15:50
已采纳

结贴,,在资源路径前加上${pageContext.request.contextPath}

xml配置

资源

u013693144
u013693144   2016.09.12 13:48

方法一:配置静态资源地址

 <mvc:resources mapping="/img/**" location="/img/" /> 

方法二:配置默认处理servlet

 <mvc:default-servlet-handler />
bibby28
bibby28   2016.09.12 14:03

有了,就是死活访问不到静态资源

u013829202
u013829202   Rxr 2016.09.12 14:42

浏览器F12看看地址是不是正确的。还是被拦截了

zhaihonghonghzh
zhaihonghonghzh   2016.09.12 17:48

页面放到什么路径下了 访问页面是直接页面路径还是走controller

zhaihonghonghzh
zhaihonghonghzh   2016.09.12 17:46

如果页面在WEB-INF下是不能直接访问的

zealVampire
zealVampire   2016.09.13 10:29

Dude, your configuration should be ok.
1. Assume your webapp dir is in d:/your-webapp, and you should place /resources under this folder but not in d:/your-webapp/WEB-INF
2. Make sure you do not miss the context path, you can use

zealVampire
zealVampire   2016.09.13 10:33
  1. you can use spring url tag to support context path
 <spring:url  value="/resoures/xxx"/>
Csdn user default icon
上传中...
上传图片
插入图片