【问题标题】:Java Spring MVC doesnt work when I add mvc:resources mapping当我添加 mvc:resources 映射时,Java Spring MVC 不起作用
【发布时间】:2015-03-04 13:28:01
【问题描述】:

我是 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-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
@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。如果没有行

&lt;mvc:resources mapping="/resources/**" location="/resources/"/&gt;

然后它工作正常并打开 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] 在 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] [[ 未找到带有 URI 的 HTTP 请求的映射 [/untitled_war_exploded/resources/blog.css] 在 DispatcherServlet 中 名称'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] 在 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] [[ 没有找到带有 URI 的 HTTP 请求的映射 DispatcherServlet 中的 [/untitled_war_exploded/] 名称 'mvc-dispatcher']]

[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-dispatcher']]

[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-dispatcher']]

文件树:

├───.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

【问题讨论】:

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


    【解决方案1】:

    以下配置应该适合你,你基本上缺少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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-15
      • 1970-01-01
      • 1970-01-01
      • 2017-04-07
      • 2016-01-31
      • 2014-06-12
      • 1970-01-01
      • 2020-06-26
      相关资源
      最近更新 更多