【发布时间】:2015-06-03 20:22:54
【问题描述】:
我写网站,我必须使用 html 页面。当我使用 jsp 页面编写时,我得到了很好的结果,但是我不使用 HTML 页面,因为我得到了错误:
信息:服务器在 4792 毫秒内启动
警告:在名称为“HelloWeb”的 DispatcherServlet 中找不到带有 URI [/TestSpringMVC/] 的 HTTP 请求的映射
警告:在名称为“HelloWeb”的 DispatcherServlet 中找不到带有 URI [/TestSpringMVC/index.html] 的 HTTP 请求的映射
这是我的欢迎 html 页面:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="home">click</a>
</body>
</html>
这是我的 home.html 页面:
<body>
<h1>this is home page</h1>
</body>
这是我的 web.xml
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Archetype Created Web Application</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/HelloWeb-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>HelloWeb</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>HelloWeb</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
HelloWeb-servlet.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
<context:component-scan base-package="main.test.spring" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/page/html/"/>
<property name="suffix" value=".html"/>
</bean>
我的控制器:
@Controller
public class HelloController{
@RequestMapping(value = "/home", method = RequestMethod.GET)
public String printHellow() {
return "page/html/home";
}
}
附言我使用Tomcat 7
【问题讨论】:
-
首先从浏览器
http://localhost:8080/TestSpringMVC/home尝试,看看它是否命中了控制器。 -
当我去
http://localhost:8080/TestSpringMVC/home我有HTTP Status 404 description The requested resource is not available. -
你的tomcat服务器运行在8080端口只有ri8?接下来将
return "page/html/home";更改为return "home";
标签: java html spring spring-mvc