Spring MVC InternalResourceViewResolver和静态资源

时间:2014-11-09 14:08:54

标签: spring-mvc

当我单独使用InternalResourceViewResolver时,我的视图将被正确解析。当我将注释驱动添加到我的配置文件时,我的视图已解决,但我的资源却没有。这让我发疯了......

src
    main
        java
        resources
            css
            js
                ajaxHandler.js
        webapp
            WEB-INF
                spring
                appServlet
                    servlet-context.xml
                views
                    index.jsp
                    internalview.jsp
                web.xml

这是我的web.xml:

<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <display-name>spring.introduction</display-name>

    <servlet>
        <servlet-name>ApplicationServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>ApplicationServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>WEB-INF/views/index.jsp</welcome-file>
    </welcome-file-list>

</web-app>

servlet-context.xml:

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

    <context:component-scan base-package="com.tsystems.sample" />

    <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>

    <mvc:resources location="/js/**" mapping="/resources/js/" />
    <!--mvc:default-servlet-handler/-->
    <mvc:annotation-driven/>

</beans:beans>

基本流程: Index.jsp有一个表单,它将“sender:index”作为POST传递给indexController。这应该归结为以下方法:

@RequestMapping(value = "/Forward", method = RequestMethod.POST)
public ModelAndView forward(@RequestParam(value = "sender", required = true) String sender, Model model) {

    m_logger.info(String.format("Captured sender attribute: " + sender));

    ModelAndView mav = new ModelAndView("internalview");
    mav.addObject("sender", sender);

    return mav;
}

到目前为止,信息消息显示在服务器日志中并显示内部视图。在我的internalview.jsp中,我尝试按如下方式加载js:

<script type="text/javascript" src="<c:url value="/js/ajaxHandler.js"/>"></script>

最后收到一个很好的404错误,以及server.log中的以下消息:

[org.springframework.web.servlet.PageNotFound] (default task-20) No mapping found for HTTP request with URI [/spring.introduction/js/ajaxHandler.js] in DispatcherServlet with name 'ApplicationServlet'

如果我从配置文件中删除注释驱动,即使我的视图变为404 NOT FOUND。如果我删除注释驱动的AND mvc:资源它可以工作,但当然没有加载.js。

如何解决此问题? (也有类似的问题,但在尝试了那些答案之后,当我接受这个问题可能与其他人重复时,他们都没有这样做,我仍在开放,因为他们的答案都没有解决问题< /强>)

1 个答案:

答案 0 :(得分:4)

JavaScript和CSS等Web资源通常应放在src/main/webapp目录下。因此,在您的情况下(基于您的mvc:resources映射),您应该在resources中创建src/main/webapp目录,并从{{1}移动jscss目录那里。

src/main/resources