Spring MVC InternalResourceViewResolver and static resources

When I only use InternalResourceViewResolver my views are resolved correctly. When I add annotation to my config file my views are resolved but my resources are missing. It drives me crazy...

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

      

Here is my 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>

      

Main thread: Index.jsp has a form that passes "sender: index" as a POST to indexController. this should drop down to the next method:

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

      

This works so far, an informational message appears in the server log and an internal view appears. In my innerview.jsp I am trying to load js like this:

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

      

ends up with a good 404 error and the message below in 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'

      

If I remove the annotation from the config file even my view becomes 404 NOT FOUND. If I remove the AND annotated mvc: resources it works, but of course the .js doesn't load.

How can I solve this problem? (There are the same questions like this, but after polling the answers for them none of them worked, as long as I agree that the question may be duplicated by another, I am still opening because none of their answers solve the problem )

+3


source to share


1 answer


Web resources like JavaScript and CSS should usually be placed in a directory src/main/webapp

. So in your case (based on your mapping mvc:resources

) you have to create a directory resources

in src/main/webapp

and move directories to js

and css

from src/main/resources

there.



src/main/webapp/resources/js
src/main/webapp/resources/css

      

+4


source







All Articles