我是 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 资源)。玻璃鱼日志:
[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] [[在 DispatcherServlet 中找不到带有 URI [/untitled_war_exploded/resources/bootstrap/css/bootstrap.min.css] 的 HTTP 请求的映射,名称为“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] [[在名为“mvc-dispatcher”的 DispatcherServlet 中找不到带有 URI [/untitled_war_exploded/resources/blog.css] 的 HTTP 请求的映射]]
[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] [[在 DispatcherServlet 中找不到带有 URI [/untitled_war_exploded/resources/bootstrap/js/bootstrap.min.js] 的 HTTP 请求的映射,名称为“mvc-dispatcher”]]
当我添加此行以在我的页面中使用资源时,它不会打开页面(错误 404),但我可以打开资源(如http://localhost:8080/untitled_war_exploded/resources/blog.css)。玻璃鱼日志:
[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] [[在名为“mvc-dispatcher”的 DispatcherServlet 中找不到带有 URI [/untitled_war_exploded/] 的 HTTP 请求的映射]]
[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] [[在名为“mvc-dispatcher”的 DispatcherServlet 中找不到带有 URI [/untitled_war_exploded/] 的 HTTP 请求的映射]]
[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] [[在名为“mvc-dispatcher”的 DispatcherServlet 中找不到带有 URI [/untitled_war_exploded/] 的 HTTP 请求的映射]]
文件树:
├───.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