0 0

spring3mvc 访问css或图片时无法显示:使用mvc:resources5

1、项目目录结构


2、web.xml文件内容
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

	<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/spring/root-context.xml</param-value>
	</context-param>

	<!-- Creates the Spring Container shared by all Servlets and Filters -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!-- Processes application requests -->
	<servlet>
		<servlet-name>appServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>appServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>


3、servlet-context.xml文件内容
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:beans="http://www.springframework.org/schema/beans"
	xsi:schemaLocation="
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

	<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
	
	<!-- Enables the Spring MVC @Controller programming model -->
	<annotation-driven />

	<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
	<resources mapping="/resources/**" location="/resources" />

	<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
	<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<beans:property name="prefix" value="/WEB-INF/views/" />
		<beans:property name="suffix" value=".jsp" />
	</beans:bean>
	
	<!-- Imports user-defined @Controller beans that process client requests -->
	<beans:import resource="controllers.xml" />
	
</beans:beans>


4、controller的内容
package com.zb.itams.web.spring;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.zb.itams.hibernate.persistents.Organization;
import com.zb.itams.service.OrganizationService;

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {

	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

	/**
	 * Simply selects the home view to render by returning its name.
	 */
	@RequestMapping(value="/", method=RequestMethod.GET)
	public String home() {
		logger.info("Welcome home!");
		return "home";
	}
	
}


5、页面的内容(部分内容)
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!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=UTF-8">
	<title>ITAMS首页</title>
	<link href="/resources/css/default.css" rel="stylesheet" type="text/css" media="screen" />
</head>
<body>
....
			<div class="entry"> <img src="/resources/images/img07.jpg" alt="" width="122" height="122" class="left" />
			</div>
....
</body>
</html>

运行程序,页面的css格式无法使用,图片“img07.jpg”也无法显示。

6、日志显示信息如下:
[2010-11-25 17:01:38 671][DEBUG][http-8080-3]org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping[getHandlerInternal] - 221: Mapping [/] to HandlerExecutionChain with handler [com.zb.itams.web.spring.HomeController@10a2cf4] and 2 interceptors
[2010-11-25 17:01:38 718][DEBUG][http-8080-3]org.springframework.web.servlet.DispatcherServlet[doDispatch] - 769: Last-Modified value for [/itams/] is: -1
[2010-11-25 17:01:38 890][DEBUG][http-8080-3]org.springframework.web.bind.annotation.support.HandlerMethodInvoker[invokeHandlerMethod] - 173: Invoking request handler method: public java.lang.String com.zb.itams.web.spring.HomeController.home(java.util.Map)
[2010-11-25 17:01:38 906][DEBUG][http-8080-3]org.springframework.beans.factory.support.DefaultListableBeanFactory[invokeInitMethods] - 1461: Invoking afterPropertiesSet() on bean with name 'home'
[2010-11-25 17:01:38 906][DEBUG][http-8080-3]org.springframework.web.servlet.DispatcherServlet[render] - 1045: Rendering view [org.springframework.web.servlet.view.JstlView: name 'home'; URL [/WEB-INF/views/home.jsp]] in DispatcherServlet with name 'appServlet'
[2010-11-25 17:01:38 953][DEBUG][http-8080-3]org.springframework.web.servlet.view.JstlView[renderMergedOutputModel] - 236: Forwarding to resource [/WEB-INF/views/home.jsp] in InternalResourceView 'home'
[2010-11-25 17:01:39 046][DEBUG][http-8080-3]org.springframework.web.servlet.DispatcherServlet[processRequest] - 674: Successfully completed request



上网找了一下午,都是说spring3.04新注解怎么好等等,没有找到什么原因造成的。

问题补充:通过查看spring的mvc-showcase,做了相应的修改:
1、将resources文件移动到WebContent下。
2、servlet-context.xml中
<resources mapping="/resources/**" location="/resources" />

修改为:
<resources mapping="/resources/**" location="/resources/" />

3、页面的修改
1)加上
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

2)引用资源的地方使用:
<link href="<c:url value="/resources/css/default.css" />" rel="stylesheet" type="text/css" />

<img src="<c:url value="/resources/images/img07.jpg" />" alt="" width="122" height="122" class="left" />


就能得到相应的资源了,但是:
1、不使用c:url,而直接使用“/resources/images/img07.jpg”无法得到资源。
2、css文件中调用的图片资源还是无法查看。

2010年11月25日 16:55
  • 大小: 40.7 KB

2个答案 按时间排序 按投票排序

0 0

不使用c:url,而直接使用“/resources/images/img07.jpg”无法得到资源。

用 “ resources/images/img07.jpg ” 就可以访问到了~

2013年5月10日 00:55
0 0

估计你重定向的位置不对

 <resources mapping="/resources/**" location="/resources" />

改成
 <resources mapping="/css/**" location="/css" />

看看

2010年11月26日 10:28

相关推荐

Global site tag (gtag.js) - Google Analytics