java - Spring 框架静态资源设置问题

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

我已经使用 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 文件,我就会看到返回调用“home”返回的 JSP 页面。

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

下面是我的应用程序正在使用的主 Controller 。

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>

最佳答案

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

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

将标签的顺序更改为:

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

然后它应该可以工作。

关于java - Spring 框架静态资源设置问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26926193/

相关文章:

java - 如何将带有字节操作的代码从 Java 翻译为 Kotlin?

java - @Autowired 和线程的陷阱

java - JPA/hibernate : call stored procedure with input and output parameters

Spring选择多个标签和绑定(bind)

mysql - 在尝试使用 spring mvc Angular JS 保存数据时在数据库中存储空值

java - 我必须循环接受用户输入,直到输入 "done"

java - 我们从哪里开始使用单元测试?

java - 如何自定义Junit测试用例调用功能

javascript - 无法使用 angularjs 将请求从 Controller 渲染到另一个 html

java - Spring : create object with id from another table