SpringMVC在JSP中加载CSS等静态资源问题
大家好,我的问题是这样的。
自己搭建了SpringMVC + Spring + Hibernate 框架。
结果在JSP中引用CSS等静态文件的时候遇到了404的问题。
以下是问题的截图。以及配置文件的信息。
还有就是我查了很多资料。SpringMVC加载静态资源的三种配置我都已经试过了但是都不好使。
比如这个链接的内容 三种方式都不好使。
问题情况如下:
项目结构:
SpringMVC配置文件:
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<!-- 注解 -->
<mvc:annotation-driven />
<mvc:default-servlet-handler/>
<!-- <mvc:resources mapping="/js/**" location="/WEB-INF/js/" cache-period="31556926"/>
<mvc:resources mapping="/css/**" location="/WEB-INF/css222/" cache-period="31556926"/>
<mvc:resources mapping="/images/**" location="/WEB-INF/imgs/" cache-period="31556926"/> -->
<!-- 配置自动扫描的包 -->
<context:component-scan base-package="com.whr">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
<!--<context:include-filter type="annotation"
expression="org.springframework.web.bind.annotation.ControllerAdvice" /> -->
</context:component-scan>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
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">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 配置 Spring IOC 容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置 Springmvc 的 DispatcherServlet -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 配置编码方式过滤器 ,注意一点,要配置所有的过滤器,最前面 -->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/</url-pattern>
</filter-mapping>
<!-- 配置log4j -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>6000</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.util.Log4jConfigListener
</listener-class>
</listener>
<filter>
<filter-name>openSession</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSession</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta name="viewport" content="width=device-width,initial-scale=1">
<base href="<%=basePath%>"></base>
<link rel="stylesheet" href="css/bootstrap.min.css">
<title>My JSP 'work_info.jsp' starting page</title>
</head>
<body>
<h1>WELCOME TO WORKSPACE</h1>
<img src="WEB-INF/imgs/watch.png" />
</body>
</html>
Controller:
//就只粘贴了有用的部分
package com.whr.Controller;
import java.sql.Date;
import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.whr.Entity.Work;
import com.whr.Service.WorkService;
@Controller
@RequestMapping("/work")
public class WorkController {
@Autowired
private WorkService workService;
private static Logger logger = Logger.getLogger(WorkController.class);
@RequestMapping("/index")
public String workIndex()
{
return "testIndex";
}
@RequestMapping("/index2")
public String workIndex2()
{
return "work";
}
大概情况就是这样的了。
弄了两天了,方法试了好多,简直焦头烂额。各位大神帮帮忙看看吧,不胜感激!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
静态资源不要放在WEB-INF目录下,WEB-INF目录的所有文件是没法直接访问的,除了jsp可以放在WEB-INF下之外,其他img/css/js都移到WebRoot下吧。
大哥你把资源都放在WEB-INF下了,放到WebRoot 下吧,另外再把 mvc:resources 配置取消注释,但是记得换路径
js,css放到web-inf外面,jsp放在里面就可以了。没必要为这个问题烦恼,还两天....
楼上正解,静态资源你放到WEB-INF中当然访问不到啦,拿粗来吧,可以看看我的demo https://git.oschina.net/gradle/ssmbootst...
资源可以放在web-inf,但要用mvc:resouces来配置路径
谁说不能放在web-inf下面的,只是要spring配置文件中配置一下就可以了,
<mvc:resources mapping="/styles/**" location="/styles/" />