spring-mvc - SpringMVC在JSP中加载CSS等静态资源问题

查看:131
本文介绍了spring-mvc - 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-INF目录下,WEB-INF目录的所有文件是没法直接访问的,除了jsp可以放在WEB-INF下之外,其他img/css/js都移到WebRoot下吧。

这篇关于spring-mvc - SpringMVC在JSP中加载CSS等静态资源问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆