【发布时间】:2015-07-09 08:45:01
【问题描述】:
我已经创建了一个简单的 spring MVC 应用程序,而没有在 Eclipse EE IDE 中使用任何构建工具,如 maven 或 ant
该应用程序仅包含一个控制器类、2 个 XML(web.xml 和 spring-servlet.xml)和一个 jsp 页面(hellopage.jsp)
我正在使用 tomcat 和 6.0 eclipse galileo。
在我的spring-servlet.xml 文件中,我提到<servlet-name> 为“弹簧”,<servlet-class> 为org.springframework.web.servlet.DispatcherServlet,<url-pattern> 为*.html,而在我的web.xml 文件中,欢迎文件是index.jsp有一个链接 (<a href="hello.html">click</a>) 到 hello.html。
我的控制器类的请求映射为 ("/hello"),当在浏览器中部署我的应用程序的 war 文件后,我点击 URL localhost:8080/projectname,index.jsp 页面弹出一个链接 "click",但是单击此链接后,我收到一个 404 错误,其中提到“servlet spring 不可用”表示我在spring-servlet.xml 文件中提到的调度程序 servlet,任何人都可以帮忙?
代码如下:
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>SpringMVC</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
HelloWorldController.java:
package com.samar.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HelloWorldController {
@RequestMapping("/hello")
public ModelAndView helloworld()
{
String message ="Hello spring MVC...!!";
return new ModelAndView("hellopage","message",message);
}
}
spring-servlet.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation ="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.samar.controllers"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
index.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="/hello.html">Click</a>
</body>
</html>
hellopage.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Message is ${message}
</body>
</html>
【问题讨论】:
-
如果您发布代码,每个人(包括您自己)都可以更轻松地进行调试
-
欢迎来到 StackOverflow!请务必阅读我们的How to Ask 页面,以帮助您提出一个很好的问题。如果您对您的问题付出一些努力,您更有可能从社区中得到一个好的答案。
-
仅查看您发布的 html 可能是找不到 hello.html 文件,因为您需要为其提供完全限定的资源位置,即像这样 href="${ pageContext.request.contextPath}/hello.html"
标签: java spring jsp spring-mvc servlets