gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 22:35:36 25 4
gpt4 key购买 nike

我已经使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com