环境:spring + springMvc + mybatis + maven
关于在springMVC环境访问web-inf目录下文件,其一有在springMVC xml文件下加
[Java] 纯文本查看 复制代码
1
2
|
<!-- 对静态资源文件的访问 不支持访问WEB-INF目录 -->
<mvc: default -servlet-handler />
|
如果还是不能访问,继续在配置文件中追加
[Java] 纯文本查看 复制代码
1
|
<mvc:resources mapping= "/res/**" location= "/WEB-INF/res/" />
|
mapping:映射
location:本地资源路径,注意必须是webapp根目录下的路径。
两个*,它表示映射resources/下所有的URL,包括子路径(即接多个/)
这样我们就可以直接访问该文件夹下的静态内容了。
如在jsp页面中映入jquery文件
[Java] 纯文本查看 复制代码
1
|
<script type= "text/javascript" src= "${pageContext.request.contextPath}/res/js/jquery/jquery-1.8.3.js" ></script>
|
在新搭建的项目中,建立一个jsp页面在导入jquery插件时
浏览器调试提示:GET http://localhost:8080/storm/js/jquery-2.1.1.js 404 (Not Found)
看到这个一直围绕着路径的问题去了,各种虐。
[Java] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
|
<%@ 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/>
<mvc:default-servlet-handler/>
[Java] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
<?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
[url=http: //www.springframework.org/schema/beans/spring-beans-3.1.xsd]http://www.springframework.org/schema/beans/spring-beans-3.1.xsd[/url]
[url=http: //www.springframework.org/schema/context]http://www.springframework.org/schema/context[/url]
[url=http: //www.springframework.org/schema/context/spring-context-3.1.xsd]http://www.springframework.org/s ... ing-context-3.1.xsd[/url]
[url=http: //www.springframework.org/schema/mvc]http://www.springframework.org/schema/mvc[/url]
[url=http: //www.springframework.org/schema/mvc/spring-mvc-4.0.xsd]http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd[/url]">
<!-- 自动扫描该包,使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>
|