Spring Framework静态资源设置问题

时间:2014-11-14 08:55:38

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

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

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

但是当我尝试直接访问静态内容时,请使用以下网址:

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

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

@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";
    }

一旦请求导致这个方法而不是显示staic CSS文件,我看到返回调用“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>

3 个答案:

答案 0 :(得分:2)

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

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

将代码的顺序更改为:

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

然后它应该工作。

答案 1 :(得分:1)

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

  • countryId =资源
  • STATEID = CSS
  • cityId = app.css

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

答案 2 :(得分:0)

尝试以下选项:

  1. @RequestMapping("/")
  2. 中删除MainController
  3. 在Spring配置中添加View Resolver。例如,InternalResourceViewResolver
  4. 查看Spring doc
  5. 将资源目录移动到与视图相同的级别。即/ WEB-INF / views和/ WEB-INF / resources。
  6. 我希望有所帮助!