我通过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>
解决方案的结构:
答案 0 :(得分: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和image directries,有些人更喜欢只有一个名为“static-assets”的人。但称其为资源相当混乱。 src / main / resource /目录实际上包含整个项目的配置文件(我将appContext.xml放在那里和log.properties文件中),它在部署时被复制到WEB-INF,不应该用于映射静态资源。 例如,在我的例子中,woul dactualyl被映射为:
<mvc:resources location="/css/" mapping="/css/**"/>