java - Spring配置bean静态资源映射和url映射?

标签 java spring spring-mvc javabeans

我刚刚开始使用 spring 框架,我的 spring bean 配置有问题。 要加载我的静态资源,例如 css、js 等,我必须添加以下行: <mvc:resources mapping=”/resources/**” location=”/resources/” />在我的 spring bean 配置中:

我的问题是,如果我放置此行,由于某种原因,当我部署应用程序时,我的应用程序的所有 url 都不会映射。当我评论这一行时,所有 url 的应用程序都工作正常,但当然我的静态资源没有映射。

我需要一些关于如何解决这个问题的提示。谢谢 !。 这也是我所有的 spring-config.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:p="http://www.springframework.org/schema/p"  
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.0.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">


    <context:component-scan base-package="com.application" />
    <!-- Mapping the static resources -->
     <mvc:resources mapping="/resources/**" location="/resources/" />

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <!-- Unimportant for my problem -->
    <!-- Bean used for login from userDao(repository) -->
    <bean id="customUserDetailsService" class="com.application.service.CustomUserDetailsService"></bean>

    <!-- Setup the userDao(repository) -->
    <bean id="userDao" class="com.application.model.UserDAO" />

    <!-- Declared datasource bean -->
    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost/licenta" />
        <property name="username" value="root" />
        <property name="password" value="" />
    </bean>

</beans>

最佳答案

您需要添加<mvc:annotation-driven />到你的 spring-config.xml

查看this类似问题

关于java - Spring配置bean静态资源映射和url映射?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22646552/

相关文章:

spring - 如何使用自定义 WebApplicationInitializer 运行测试?

java - 如何在 Spring 中 Autowiring 依赖项/外部 jar 中的组件?

spring - thymeleaf + Spring : How to keep line break?

java - 运行Spring项目时出现空指针异常

java - 如何选择元素进行 react 选择 Selenium 模式

java - Jersey - 尽管非空初始化,资源变量仍为空值

java - 扩展通用 Controller 时 Spring mvc 错误

java - Spring MVC uri 与百分比编码字符的映射

java - Mapreduce 作业到 HBase 抛出 IOException : Pass a Delete or a Put

java - Android写入文件