gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 13:48:54 26 4
gpt4 key购买 nike

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

网络.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";
}
}

我使用 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] [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']]

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

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

文件树:

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

最佳答案

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

关于spring-mvc - 当我添加 mvc :resources mapping 时,Java Spring MVC 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28855683/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com