【问题标题】:Accessing the static content in Spring 3 MVC application with integrated Spring security使用集成的 Spring 安全性访问 Spring 3 MVC 应用程序中的静态内容
【发布时间】:2013-03-21 17:51:58
【问题描述】:

我通过 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>

解决方案的结构:

【问题讨论】:

  • 该代码不应该在web.xml 中,我认为它应该在您的servlet 上下文中

标签: java spring spring-mvc spring-security


【解决方案1】:

代码应该在您的应用上下文定义(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 目录,有些人更喜欢一个叫做“static-assets”的目录。但是称它为资源是相当混乱的。 src/main/resource/ 目录实际上包含整个项目的配置文件(我将 appContext.xml 和 log.properties 文件放在那里),它在部署时被复制到 WEB-INF,不应该用于映射静态资源。 例如,在我的示例中,dactualyl 会像这样映射:

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

【讨论】:

  • 我不能,因为我已经尝试过了,但它不起作用。我得到错误:The prefix mvc:resources is not bound
  • 现在我意识到,添加&lt;mvc:resources mapping="/resources/**" location="/resources/" /&gt; 后,我无法使用控制器访问页面。我收到警告:WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/fit/] in DispatcherServlet with name 'mvc-dispatcher'
  • @Dworza 你的资源目录应该向上移动一个......它的 webapp 目录是部署的根目录。
  • 我认为,由于安全性,它不起作用:-/ 因为在其他项目中这工作得很好......
  • @Dworza 也需要在配置中,在资源映射之前
猜你喜欢
  • 2014-05-14
  • 1970-01-01
  • 2016-01-30
  • 2018-05-17
  • 2013-03-03
  • 2013-06-12
  • 2021-04-01
  • 2019-07-05
  • 1970-01-01
相关资源
最近更新 更多