環境: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