1

我已经通过 Spring Tool Suite 模板创建了 Spring 3 MVC 项目,并且在那里集成了 Spring 安全性。除了访问静态内容之外,一切正常。

当我只创建 MVC 应用程序并将我的静态内容放入/src/webapp/WEB-INF/resources/并放入<resources mapping="/resources/**" location="src/main/webapp/WEB-INF/resources" />我的applicationContext.xml.applicationContext.xml写到我的 web.xml 来完成这项工作?

我的applicationContext.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:context="http://www.springframework.org/schema/context"
xmlns:security="http://www.springframework.org/schema/security"
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/security http://www.springframework.org/schema/security/spring-security-3.1.xsd">

<context:annotation-config />

<context:component-scan base-package="cz.cvut.fit.genepi.controllers" />

<import resource="classpath:applicationContext-security.xml" />

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

奇怪的是,当使用上面的代码时,映射视图控制器工作正常,但是当我使用它时,我得到了这个错误The prefix mvc:resources is not bound

<?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:context="http://www.springframework.org/schema/context"
xmlns:security="http://www.springframework.org/schema/security"
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/security http://www.springframework.org/schema/security/spring-security-3.1.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

<context:annotation-config />
    <context:component-scan base-package="cz.cvut.fit.genepi.controllers" />
<import resource="classpath:applicationContext-security.xml" />
<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/" />
    <property name="suffix" value=".jsp" />
</bean>

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

     </beans>

解决方案的结构:

在此处输入图像描述

4

1 回答 1

3

代码应该在您的应用上下文定义(applicationContext.xml)中,并且位置相对于部署根目录:

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

你需要在配置文件的顶部

 xmlns:mvc="http://www.springframework.org/schema/mvc"

接着

 xsi:schemaLocation = http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd

我认为可能有些混乱,正常的 wdirectory 结构是这样的:

src/main/java 
src/main/resources
src/main/webapp
src/main/webapp/WEB-INF
src/main/webapp/WEB-INF/jsps
src/main/webapp/css 

css 目录只是一个示例,我实际上有 javascipt 和图像目录,有些人更喜欢一个名为“静态资产”的目录。但是称它为资源是相当混乱的。src/main/resource/ 目录实际上包含整个项目的配置文件(我将 appContext.xml 和 log.properties 文件放在那里),它在部署时被复制到 WEB-INF,不应该用于映射静态资源。例如,在我的示例中,dactualyl 会像这样映射:

<mvc:resources location="/css/" mapping="/css/**"/>
于 2013-03-20T15:17:47.650 回答