警告:org.springframework.web.servlet.PageNotFound - 浏览器上的404

时间:2016-02-07 13:25:06

标签: spring servlets

创建mvc表单处理并在控制台上获取错误

警告:org.springframework.web.servlet.PageNotFound - 在名为'appServlet'的DispatcherServlet中找不到带有URI [/ controller /]的HTTP请求的映射

employeeForm.jsp

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"  
        pageEncoding="ISO-8859-1"%>  
    <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>  
      
    <html>  
     <head>  
        
      <title>Spring MVC Form Handling</title>  
     </head>  
     &body>  
    <h2>  
    Employee Data Form</h2>  
    <form:form action="/sdnext/addEmployee" method="POST">        
    <table><tbody>  
    <tr>        <td><form:label path="empId">Employee :</form:label></td>      <td><form:input path="empId"></form:input></td>    </tr>  
    <tr>      <td><form:label path="name">EmployeeName:/form:label></form:label></td>       <td><form:input path="name"></form:input></td>    </tr>  
    <tr>       <td><form:label path="age">Employee Age:</form:label></td>       <td><form:input path="age"></form:input></td>     </tr>  
    <tr>      <td><form:label path="salary">Employee Salary:</form:label></td>     <td><form:input path="salary"></form:input></td>    </tr>  
    <tr>         <td colspan="2"><input type="submit" value="Submit"/>  </td>       </tr>  
    </tbody></table>  
    </form:form>  
    </body>  
    </html>  

EmployeeController

package com.spring.controller;

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.spring.model.Employee;

/**
 * Handles requests for the application home page.
 */
@Controller
public class EmployeeController {
	
	private static final Logger logger = LoggerFactory.getLogger(EmployeeController.class);
	
	/**
	 * Simply selects the home view to render by returning its name.
	 */
	  
	  @RequestMapping(value = "/employeeForm", method = RequestMethod.GET)  
	  public ModelAndView employee() {  
	    return new ModelAndView("employeeForm", "command", new Employee());  
	   }  
	      
	  @RequestMapping(value = "/addEmployee", method = RequestMethod.POST)  
	  public String addEmployee(@ModelAttribute("SpringWeb")Employee employee, ModelMap model) {  
	     model.addAttribute("name", employee.getName());  
	     model.addAttribute("age", employee.getAge());  
	     model.addAttribute("empId", employee.getEmpId());  
	     model.addAttribute("salary", employee.getSalary());  
	     return "employeeDetail";  
	   }  
	}  

员工

package com.spring.model;

public class Employee {


	private int empId;  
	private String name;  
	private Long salary;  
	private int age;
	public int getEmpId() {
		return empId;
	}
	public void setEmpId(int empId) {
		this.empId = empId;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Long getSalary() {
		return salary;
	}
	public void setSalary(Long salary) {
		this.salary = salary;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	} 

}

的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">

	<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/spring/root-context.xml</param-value>
	</context-param>
	
	<!-- Creates the Spring Container shared by all Servlets and Filters -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!-- Processes application requests -->
	<servlet>
		<servlet-name>appServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
		
	<servlet-mapping>
		<servlet-name>appServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

</web-app>

servlet的context.xml中

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:beans="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
		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">

	<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
	
	<!-- Enables the Spring MVC @Controller programming model -->
	<annotation-driven />

	<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
	<resources mapping="/resources/**" location="/resources/" />

	<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
	<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<beans:property name="prefix" value="/WEB-INF/views/" />
		<beans:property name="suffix" value=".jsp" />
	</beans:bean>
	
	<context:component-scan base-package="com.spring.controller" />
	
	
	
</beans:beans>

1 个答案:

答案 0 :(得分:0)

我想强调两点,我发现提供的输入不匹配。

  1. employeeForm.jsp网址为"/sdnext/addEmployee"
  2. EmployeeController的addEmployee方法仅映射"/addEmployee"
  3. 正确关闭<form:label path="name">EmployeeName:/form:label>
  4. 问题是关于"/sdnext"。来自给定的输入uri路径"/sdnext"映射缺失。

    在控制器级别添加RequestMapping注释可能会有所帮助。

    @Controller
    @RequestMapping("/sdnext")
    class EmployeeController{
        ...
    } 
    

    如果在DEBUG模式下启用服务器日志,那么spring将告诉您Web应用程序映射了哪些uri路径。