HTTP状态404 - Servlet welcome不可用

时间:2016-06-23 12:11:25

标签: jsp spring-mvc

我正在尝试创建一个基本的Spring MVC应用程序,它抛出以下错误:

HTTP状态404 - Servlet欢迎不可用

我的项目结构

enter image description here

我的web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>ServletProject</display-name>
  <servlet>
  <servlet-name>welcome</servlet-name>
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
     <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
     <servlet-name>welcome</servlet-name>
     <url-pattern>/</url-pattern>
  </servlet-mapping>

  <welcome-file-list>
     <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>


</web-app>

我的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>
<font size="2px" face="verdana">
  Welcome...
   <a href="myPage.htm"><br> Click here to check the output :-)</a>
</font>

</body>
</body>
</html>

我的WelcomePage.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>
  <font face="verdana" size="2">
     ${welcomeMessage}
  </font>
</body>
</html>

我的welcome-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>

  <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<context:component-scan base-package="com.springMail" />

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>

</beans>

我的HelloWorld.java:

package com.springMail;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HelloWorld {

@RequestMapping("/myPage")
public ModelAndView helloWorld() {

   String message =  "Welcome to Java4s.com Spring MVC 3.2.x Sessions";
   message += "<br>You Did it....!";

   return new ModelAndView("welcomePage", "welcomeMessage", message);
}

}

请帮我解决此错误。

提前致谢。

1 个答案:

答案 0 :(得分:0)

index.jsp 页面中,您有一个指向 myPage.htm 的链接,但您根本没有这样的页面。

如果您想转到 HelloWorld 控制器,请执行 您可以说<a href="${pageContext.request.contextPath}/myPage" />,因为您的 HelloWorld 控制器的RequestMapping为“/ myPage”。