java – SpringMVC和静态资源
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – SpringMVC和静态资源,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4545字,纯文字阅读大概需要7分钟。
内容图文
![java – SpringMVC和静态资源](/upload/InfoBanner/zyjiaocheng/701/3b2774ef2be049429a17cc63791359a7.jpg)
我是java和spring的新手.我正在尝试制作你好世界的应用程序,而不是得到我做错了什么.
这是我的目录结构:
test_app
-pom.xml
-src
--main
---java
----com.example.web
-----IndexController.java
---webapp
----static
-----img
------example.jpg
----WEB-INF
-----web.xml
-----dispatcher-servlet.xml
-----jsp
------index.jsp
和来源:
web.xml中
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Movie Reminder WebApp</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
</web-app>
调度员servlet.xml中:
<?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.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.example.web"/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
IndexController.java
package com.example.web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class IndexController {
@RequestMapping(value = "/")
public ModelAndView index() {
return new ModelAndView("index");
}
}
的index.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Hello Spring!</title>
</head>
<body>
<img src="<c:url value="/static/img/example.jpg" />" />
</body>
</html>
当我部署我的应用程序并请求时,我在图像请求上获得404
http://localhost:8081/ --- http 200 ok
http://localhost:8081/static/img/example.jpg - http 404 not found
当我将mvc:resources添加到dispatcher-servlet.xml时
<context:annotation-config/>
<context:component-scan base-package="com.example.web"/>
<mvc:resources mapping="/static/**" location="/static/" />
并重新编译我得到404 /请求
http://localhost:8081/ --- http 404 not foundok
http://localhost:8081/static/img/example.jpg - http 200 ok
请帮我弄清楚我做错了什么
解决方法:
只需将这两行添加到dispatcher-servlet.xml即可
<mvc:resources mapping="/static/**" location="/static/" />
<mvc:default-servlet-handler />
以下是default-servlet-handler的文档说:
Configures a handler for serving static resources by forwarding to the
Servlet container’s default Servlet. Use of this handler allows
using a “/” mapping with theDispatcherServlet
while still utilizing
the Servlet container to serve static resources. This handler will
forward all requests to the default Servlet. Therefore it is
important that it remains last in the order of all other URL
HandlerMappings. That will be the case if you use the
“annotation-driven” element or alternatively if you are setting up
your customized HandlerMapping instance be sure to set its “order”
property to a value lower than that of theDefaultServletHttpRequestHandler
, which isInteger.MAX_VALUE
.
内容总结
以上是互联网集市为您收集整理的java – SpringMVC和静态资源全部内容,希望文章能够帮你解决java – SpringMVC和静态资源所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。