我收到错误404,请求的资源不可用

时间:2019-04-24 08:03:19

标签: spring-mvc

  • 我收到了错误的HTTP状态404-/FirstSpringMVCProject/welcome.html

我的控制台是

  1. 请帮帮我。

我对spring和spring mvc应用程序很陌生。

`Apr 24, 2019 1:02:06 PM org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: Completed initialization in 2771 ms
Apr 24, 2019 1:02:06 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping for GET /FirstSpringMVCProject/

  • 这是我的web.xml文件

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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>FirstSpringMVCProject</display-name>
  
  <servlet>
  	<servlet-name>spring-dispatcher</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  	<servlet-name>spring-dispatcher</servlet-name>
  	<url-pattern></url-pattern>
  </servlet-mapping>
</web-app>

  • 这是我的调度程序文件

spring-dispatcher-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: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">  
   
  	<bean id="HandlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
  	
  	<bean name="/welcome.html" class="com.amol.hellocontroller.HelloController"/>
  	
  	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  		<property name="prefix">
  			<value>/WEB-INF/</value>
  		</property>
  		<property name="suffix">
  			<value>.jsp</value>
  		</property>
  	</bean>
</beans>

  • 这是控制器类

HelloController.java

package com.amol.hellocontroller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class HelloController extends AbstractController {

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView modelAndView = new ModelAndView("HelloPage");
		modelAndView.addObject("welcomeMessage", "Hi User, wlocme to the first Spring mvc appliction");
		
		return modelAndView;
	}

}

  • 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>

	<h1>first spring mvc application</h1>
	<h2>${welcomeMessage}</h2>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

您在配置中缺少url模式。尝试按照以下步骤检查是否可行。

<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>FirstSpringMVCProject</display-name>

  <servlet>
    <servlet-name>spring-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>spring-dispatcher</servlet-name>
    <url-pattern>/FirstSpringMVCProject/*</url-pattern>
  </servlet-mapping>
</web-app>

根据您的错误日志,

  

警告:GET / FirstSpringMVCProject /

没有映射

您的应用程序上下文根是/FirstSpringMVCProject,因此您需要定义与上下文根匹配的url模式。