3
votes

I am a new in Spring and Java EE. There is my very simple application:

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

mvc-dispatcher-servlet.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

       <context:component-scan base-package="ru.javaheap"/>

       <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
              <property name="prefix" value="/WEB-INF/pages/"/>
              <property name="suffix" value=".jsp"/>
       </bean>

       <mvc:resources mapping="/resources/**" location="/resources/"/>

</beans>

Controller:

@Controller
@RequestMapping("/")
public class HelloController {
    @RequestMapping(method = RequestMethod.GET)
    public String printWelcome(ModelMap model) {
        List<AuthorEntity> authors = HibernateUtil.getSession().createCriteria(AuthorEntity.class).list();
            model.addAttribute("listAuthors", authors);
            return "hello";
    }
}

I use glassfish 4.1 and IDEA. If there is no line

<mvc:resources mapping="/resources/**" location="/resources/"/>

then it works fine and opens hello.jps page (without css and js resources). Glassfish log:

[2015-03-04T17:18:04.876+0400] [glassfish 4.1] [WARNING] [] [org.springframework.web.servlet.PageNotFound] [tid: _ThreadID=31 _ThreadName=http-listener-1(1)] [timeMillis: 1425475084876] [levelValue: 900] [[ No mapping found for HTTP request with URI [/untitled_war_exploded/resources/bootstrap/css/bootstrap.min.css] in DispatcherServlet with name 'mvc-dispatcher']]

[2015-03-04T17:18:04.880+0400] [glassfish 4.1] [WARNING] [] [org.springframework.web.servlet.PageNotFound] [tid: _ThreadID=33 _ThreadName=http-listener-1(3)] [timeMillis: 1425475084880] [levelValue: 900] [[ No mapping found for HTTP request with URI [/untitled_war_exploded/resources/blog.css] in DispatcherServlet with name 'mvc-dispatcher']]

[2015-03-04T17:18:04.899+0400] [glassfish 4.1] [WARNING] [] [org.springframework.web.servlet.PageNotFound] [tid: _ThreadID=32 _ThreadName=http-listener-1(2)] [timeMillis: 1425475084899] [levelValue: 900] [[ No mapping found for HTTP request with URI [/untitled_war_exploded/resources/bootstrap/js/bootstrap.min.js] in DispatcherServlet with name 'mvc-dispatcher']]

When I add this line to have a possible to use resources in my page, it doesn't open the page (error 404), but I can open resources (like http://localhost:8080/untitled_war_exploded/resources/blog.css). Glassfish log:

[2015-03-04T17:15:54.595+0400] [glassfish 4.1] [WARNING] [] [org.springframework.web.servlet.PageNotFound] [tid: _ThreadID=31 _ThreadName=http-listener-1(1)] [timeMillis: 1425474954595] [levelValue: 900] [[ No mapping found for HTTP request with URI [/untitled_war_exploded/] in DispatcherServlet with name 'mvc-dispatcher']]

[2015-03-04T17:15:54.745+0400] [glassfish 4.1] [WARNING] [] [org.springframework.web.servlet.PageNotFound] [tid: _ThreadID=34 _ThreadName=http-listener-1(4)] [timeMillis: 1425474954745] [levelValue: 900] [[ No mapping found for HTTP request with URI [/untitled_war_exploded/] in DispatcherServlet with name 'mvc-dispatcher']]

[2015-03-04T17:16:02.191+0400] [glassfish 4.1] [WARNING] [] [org.springframework.web.servlet.PageNotFound] [tid: _ThreadID=33 _ThreadName=http-listener-1(3)] [timeMillis: 1425474962191] [levelValue: 900] [[ No mapping found for HTTP request with URI [/untitled_war_exploded/] in DispatcherServlet with name 'mvc-dispatcher']]

Files tree:

├───.idea
│   ├───artifacts
│   ├───copyright
│   ├───libraries
│   └───scopes
├───lib
├───out
│   └───artifacts
│       └───untitled_war_exploded
│           ├───resources
│           │   └───bootstrap
│           │       ├───css
│           │       ├───fonts
│           │       └───js
│           └───WEB-INF
│               ├───classes
│               │   └───ru
│               │       └───javaheap
│               │           └───entity
│               ├───lib
│               ├───pages
│               └───resources
│                   └───bootstrap
│                       ├───css
│                       ├───fonts
│                       └───js
├───src
│   ├───main
│   │   ├───java
│   │   │   └───ru
│   │   │       └───javaheap
│   │   │           └───entity
│   │   └───resources
│   └───test
│       └───java
├───target
│   ├───classes
│   │   └───ru
│   │       └───javaheap
│   │           └───entity
│   └───generated-sources
│       └───annotations
└───web
    ├───resources
    │   └───bootstrap
    │       ├───css
    │       ├───fonts
    │       └───js
    └───WEB-INF
        └───pages
1

1 Answers

4
votes

The following configuration should work for you, you're basically missing annotation-driven element

   <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xsi:schemaLocation="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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

           <context:component-scan base-package="ru.javaheap"/>

           <mvc:annotation-driven/>

           <mvc:resources mapping="/resources/**" location="/resources/"/>

           <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                  <property name="prefix" value="/WEB-INF/pages/"/>
                  <property name="suffix" value=".jsp"/>
           </bean>
    </beans>