0

我在尝试从 jsp 文件 (WEB-INF/jsp/index.jsp) 访问 css 和 js 文件时遇到问题。我通读了 Stack Overflow 上其他人面临的一些类似问题。

在此处输入图像描述

我将这行代码添加到我的 SpringMVCProj-servlet.xml 中,它使事情变得更加复杂。

<mvc:resources mapping="/WebContent/**" location="/WebContent/" />

SpringMVCProj-servlet.xml

<context:component-scan base-package="com.myapp" />
<mvc:resources mapping="/WebContent/**" location="/WebContent/" />

<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
</bean>

Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>Spring MVC Application</display-name>

  <welcome-file-list>
<!--     <welcome-file>/WEB-INF/jsp/index.jsp</welcome-file>
 -->    
 <welcome-file>/WEB-INF/jsp/index.jsp</welcome-file>
  </welcome-file-list>

   <servlet>
      <servlet-name>SpringMVCProj</servlet-name>
      <servlet-class>
         org.springframework.web.servlet.DispatcherServlet
      </servlet-class>
      <load-on-startup>1</load-on-startup>
   </servlet>

   <servlet-mapping>
      <servlet-name>SpringMVCProj</servlet-name>
      <url-pattern>/</url-pattern>
   </servlet-mapping>

<context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>/WEB-INF/SpringMVCProj-servlet.xml</param-value>
</context-param>

<listener>
   <listener-class>
      org.springframework.web.context.ContextLoaderListener
   </listener-class>
</listener>


</web-app>

这是在我添加代码行之前。

    <mvc:resources mapping="/WebContent/**" location="/WebContent/" />

在此处输入图像描述

这之后...

在此处输入图像描述

4

1 回答 1

0

在我将 SpringMVCProj-servlet.xml 更改为此之后,它起作用了。

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="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.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop.xsd">

    <mvc:annotation-driven />
    <mvc:resources mapping="/**" location="/" />

    <context:component-scan base-package="com.myapp" />
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>


</beans>
于 2013-03-25T16:48:27.237 回答