【问题标题】:Issue with Spring Framework static resource set upSpring Framework静态资源设置问题
【发布时间】:2015-01-11 14:53:12
【问题描述】:

我已经使用 XML 文件中的以下设置配置了我的 spring 项目以迎合静态资源:

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

但是当我尝试直接访问静态内容时,比如使用下面的 URL:

http://localhost:8080/main/resources/css/app.css

请求不会在浏览器中显示静态 CSS 内容,而是转到 Controller 中的以下方法:

@RequestMapping(value = "/{countryID}/{stateId}/{cityId}", method = RequestMethod.GET)
    public String city(@PathVariable("countryID") String countryID, @PathVariable("stateId") String stateId, @PathVariable("cityId") String cityId, Locale locale, Model model) {
        System.out.println("input "+stateId);
        System.out.println("input "+countryID);
        System.out.println("input "+cityId);
        model.addAttribute("serverTime", "");
        return "home";
    }

一旦请求导致此方法而不是显示静态 CSS 文件,我会看到由 return call "home" 返回的 JSP 页面。

我已经检查并重新检查,但并没有真正发现任何设置问题。 请看一下,让我看看这里可能出了什么问题。

下面是我的应用程序正在使用的主控制器。

import java.util.Locale;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/")
public class MainController {

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String country(Locale locale, Model model) {
        System.out.println("nothing but home page");
        return "home";
    }

    @RequestMapping(value = "/{countryID}", method = RequestMethod.GET)
    public String country(@PathVariable("countryID") String countryID, Locale locale, Model model) {
        System.out.println("input country "+countryID);
        return "home";
    }   

    @RequestMapping(value = "/{countryID}/{stateId}", method = RequestMethod.GET)
    public String state(@PathVariable("countryID") String countryID, @PathVariable("stateId") String stateId, Locale locale, Model model) {
        System.out.println("input "+stateId);
        System.out.println("input "+countryID);
        model.addAttribute("serverTime", "");
        return "home";
    }

    @RequestMapping(value = "/{countryID}/{stateId}/{cityId}", method = RequestMethod.GET)
    public String city(@PathVariable("countryID") String countryID, @PathVariable("stateId") String stateId, @PathVariable("cityId") String cityId, Locale locale, Model model) {
        System.out.println("input "+stateId);
        System.out.println("input "+countryID);
        System.out.println("input "+cityId);
        model.addAttribute("serverTime", "");
        return "home";
    }

    @RequestMapping(value = "/{countryID}/{stateId}/{cityId}/{destId}", method = RequestMethod.GET)
    public String dest(@PathVariable("countryID") String countryID, @PathVariable("stateId") String stateId, @PathVariable("cityId") String cityId, @PathVariable("destId") String destId, Locale locale, Model model) {
        System.out.println("input "+stateId);
        System.out.println("input "+countryID);
        System.out.println("input "+cityId);
        System.out.println("input "+destId);
        model.addAttribute("serverTime", "");

        return "home";
    }
}

以下是应用程序 XML 文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        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">

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />

<!--    <beans:bean class="org.springframework.web.servlet.view.tiles3.TilesViewResolver" />

    <beans:bean class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
        <beans:property name="definitions">
            <beans:list>
                <beans:value>/WEB-INF/views.xml</beans:value>
            </beans:list>
        </beans:property>
    </beans:bean> -->

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

    <context:component-scan base-package="com.travel.main" />



</beans:beans>

【问题讨论】:

  • 您是否尝试过将标签的顺序切换为
  • 只是累了但是没用。

标签: java spring spring-mvc jakarta-ee model-view-controller


【解决方案1】:

问题是,控制器中的 @RequestMapping 优先于资源映射。要改变这一点,你必须做两件事:

将属性order="0" 添加到您的资源标签中。

将标签的顺序更改为:

<resources mapping="/resources/**" location="/resources/" order="0" />
<annotation-driven />

那么它应该可以工作了。

【讨论】:

  • 嘿弗洛里安。我做了改变,现在工作正常。你结束了我长达 2 周的斗争。非常感谢
【解决方案2】:

似乎http://localhost:8080/main/resources/css/app.css 映射到/{countryID}/{stateId}/{cityId}

  • countryId=resources
  • stateId=css
  • cityId=app.css

似乎只有在没有与 URL 匹配的控制器时才会映射资源,因此您可以尝试向该控制器添加不同的路径:您可以将 @RequestMapping("/") 更改为 @RequestMapping("/mainController") 或添加类似 /city/ 的内容在city方法的匹配器中:city/{countryID}/{stateId}/{cityId}

【讨论】:

  • 感谢您的回复 Pablo。是的,如果我添加城市或任何其他字符串,它会起作用。但我正在研究将现有 PHP 项目移至 spring 的项目,我需要保持 URL 格式相同。 {countryID}/{stateId}/{cityId} 这三个都需要是动态的。
【解决方案3】:

尝试以下选项:

  1. MainController 中删除@RequestMapping("/")
  2. 将视图解析器添加到您的 Spring 配置中。例如,InternalResourceViewResolver
  3. 看看Spring doc
  4. 将资源目录移动到与视图相同的级别。即/WEB-INF/views 和/WEB-INF/resources。

希望对你有帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-27
    • 1970-01-01
    • 2016-08-18
    • 2016-10-01
    • 1970-01-01
    • 2018-11-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多