zoukankan      html  css  js  c++  java
  • 记录-springMVC访问web-inf下文件问题+在jsp页面导入jquery插件路径不对问题

    环境:spring + springMvc + mybatis + maven

    关于在springMVC环境访问web-inf目录下文件,其一有在springMVC xml文件下加

    <!-- 对静态资源文件的访问 不支持访问WEB-INF目录 -->  
        <mvc:default-servlet-handler />  

    如果还是不能访问,继续在配置文件中追加

    <mvc:resources mapping="/res/**" location="/WEB-INF/res/" />

    mapping:映射

    location:本地资源路径,注意必须是webapp根目录下的路径。

    两个*,它表示映射resources/下所有的URL,包括子路径(即接多个/)

    这样我们就可以直接访问该文件夹下的静态内容了。

    如在jsp页面中映入jquery文件

    <script type="text/javascript" src="${pageContext.request.contextPath}/res/js/jquery/jquery-1.8.3.js"></script>

     --------------------------------------------------以下内容为之前记录、上面的解决办法已可解决jsp导入jquery文件错误等问题---------------------------------------

    在新搭建的项目中,建立一个jsp页面在导入jquery插件时

    浏览器调试提示:GET http://localhost:8080/storm/js/jquery-2.1.1.js 404 (Not Found)

    看到这个一直围绕着路径的问题去了,各种虐。

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
        <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    <script  type="text/javascript" src="<%=basePath%>js/jquery-2.1.1.js"> </script>
    <script type="text/javascript">
        function sub(){
            alert('5555');
            var tt=$('.text');
            alert(tt);
            var text=document.getElementById('text').value;
            alert(text.toString());
        }
    </script>

    结果问题都不在我页面中些问题

    偶然在eclipse看到了下面这个提示:

    No mapping found for HTTP request with URI [/storm/webapp/js/jquery-2.1.1.js] in DispatcherServlet with name 'SpringMVC'

    解决方案如下:

    在springMvc配置文件中加入以下配置

    <mvc:annotation-driven/>
    <mvc:default-servlet-handler/>

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"  
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
        xmlns:context="http://www.springframework.org/schema/context"  
        xmlns:mvc="http://www.springframework.org/schema/mvc"  
        xsi:schemaLocation="http://www.springframework.org/schema/beans    
                            http://www.springframework.org/schema/beans/spring-beans-3.1.xsd    
                            http://www.springframework.org/schema/context    
                            http://www.springframework.org/schema/context/spring-context-3.1.xsd    
                            http://www.springframework.org/schema/mvc    
                            http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">  
        <!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->  
        <context:component-scan base-package="com.cn.syky.controller" />  
        <!--避免IE执行AJAX时,返回JSON出现下载文件 -->  
        <bean id="mappingJacksonHttpMessageConverter"  
            class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">  
            <property name="supportedMediaTypes">  
                <list>  
                    <value>text/html;charset=UTF-8</value>  
                </list>  
            </property>  
        </bean>  
        <!-- 启动SpringMVC的注解功能,完成请求和注解POJO的映射 -->  
        <bean  
            class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">  
            <property name="messageConverters">  
                <list>  
                    <ref bean="mappingJacksonHttpMessageConverter" /> <!-- JSON转换器 -->  
                </list>  
            </property>  
        </bean>  
        <!-- 定义跳转的文件的前后缀 ,视图模式配置-->  
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
            <!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->  
            <property name="prefix" value="/WEB-INF/jsp/" />  
            <property name="suffix" value=".jsp" />  
        </bean>  
          
        <!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 -->  
        <bean id="multipartResolver"    
            class="org.springframework.web.multipart.commons.CommonsMultipartResolver">    
            <!-- 默认编码 -->  
            <property name="defaultEncoding" value="utf-8" />    
            <!-- 文件大小最大值 -->  
            <property name="maxUploadSize" value="10485760000" />    
            <!-- 内存中的最大值 -->  
            <property name="maxInMemorySize" value="40960" />    
        </bean>   
      <mvc:annotation-driven/> 
      <mvc:default-servlet-handler/>
    </beans>  

    具体原因看:http://www.blogjava.net/Steven-bot/articles/361333.html

  • 相关阅读:
    mark
    ON DUPLICATE KEY UPDATE重复插入时更新
    lnmp上传文件
    websoket
    Nginx 和 Php 优化
    Nginx常见问题
    Keepalived 高可用
    https ; 及https证书
    Nginx动静分离;资源分离;rewrite重写、跳转、伪静态、规则、日志
    nginx负载均衡会话保持;四层负载均衡;端口转发
  • 原文地址:https://www.cnblogs.com/dscs/p/5043993.html
Copyright © 2011-2022 走看看