当我添加mvc:resources mapping时,Java Spring MVC不起作用

时间:2015-03-04 13:28:01

标签: spring-mvc java-ee glassfish-4.1

我是Spring和Java EE的新手。有一个非常简单的应用程序:

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

我使用glassfish 4.1和IDEA。如果没有行

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

然后它工作正常并打开hello.jps页面(没有css和js资源)。 Glassfish日志:

  

[2015-03-04T17:18:04.876 + 0400] [glassfish 4.1] [警告] []   [org.springframework.web.servlet.PageNotFound] [tid:_ThreadID = 31   _ThreadName = http-listener-1(1)] [timeMillis:1425475084876] [levelValue:900] [[找不到带URI的HTTP请求的映射   [/untitled_war_exploded/resources/bootstrap/css/bootstrap.min.css] in   DispatcherServlet,名称为“mvc-dispatcher”]]

     

[2015-03-04T17:18:04.880 + 0400] [glassfish 4.1] [警告] []   [org.springframework.web.servlet.PageNotFound] [tid:_ThreadID = 33   _ThreadName = http-listener-1(3)] [timeMillis:1425475084880] [levelValue:900] [[找不到带HTTP的HTTP请求的映射   DispatcherServlet中的[/untit_war_exploded/resources/blog.css]   名称'mvc-dispatcher']]

     

[2015-03-04T17:18:04.899 + 0400] [glassfish 4.1] [警告] []   [org.springframework.web.servlet.PageNotFound] [tid:_ThreadID = 32   _ThreadName = http-listener-1(2)] [timeMillis:1425475084899] [levelValue:900] [[找不到带URI的HTTP请求的映射   [/untitled_war_exploded/resources/bootstrap/js/bootstrap.min.js] in   DispatcherServlet,名称为“mvc-dispatcher”]]

当我添加此行以便可以在我的页面中使用资源时,它不会打开页面(错误404),但我可以打开资源(如http://localhost:8080/untitled_war_exploded/resources/blog.css)。 Glassfish日志:

  

[2015-03-04T17:15:54.595 + 0400] [glassfish 4.1] [警告] []   [org.springframework.web.servlet.PageNotFound] [tid:_ThreadID = 31   _ThreadName = http-listener-1(1)] [timeMillis:1425474954595] [levelValue:900] [[找不到带HTTP的HTTP请求的映射   带有名称的DispatcherServlet中的[/ untitled_war_exploded /]   'MVC-调度']]

     

[2015-03-04T17:15:54.745 + 0400] [glassfish 4.1] [警告] []   [org.springframework.web.servlet.PageNotFound] [tid:_ThreadID = 34   _ThreadName = http-listener-1(4)] [timeMillis:1425474954745] [levelValue:900] [[找不到带URI的HTTP请求的映射   带有名称的DispatcherServlet中的[/ untitled_war_exploded /]   'MVC-调度']]

     

[2015-03-04T17:16:02.191 + 0400] [glassfish 4.1] [警告] []   [org.springframework.web.servlet.PageNotFound] [tid:_ThreadID = 33   _ThreadName = http-listener-1(3)] [timeMillis:1425474962191] [levelValue:900] [[找不到带URI的HTTP请求的映射   带有名称的DispatcherServlet中的[/ untitled_war_exploded /]   'MVC-调度']]

文件树:

├───.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 个答案:

答案 0 :(得分:4)

以下配置适合您,您基本上缺少annotation-driven元素

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