尝试使用Spring MVC制作简单的Web应用程序。我正在尝试提交员工的信息,它应该向我显示提交信息的消息。但是,它显示404错误如下:
HTTP Status 404 - /EmployeeInfoSubmitPg/addEmployee
type Status report
message /EmployeeInfoSubmitPg/addEmployee
description The requested resource is not available.
Apache Tomcat/7.0.67
1)这是EmployeeController.java: -
package com.SpringMVC.Employees;
import org.springframework.stereotype.Controller;
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;
@Controller
public class EmployeesController {
@RequestMapping(value = "/employee", method= RequestMethod.GET)
public ModelAndView employee() {
return new ModelAndView("employee", "command", new Employee());
}
@RequestMapping(value = "/addEmployee", method= RequestMethod.POST)
public String addEmployee(@ModelAttribute("SpringMVC_EmployeesInfoSubmitPg")Employee employee, ModelMap model) {
model.addAttribute("name", employee.getName());
model.addAttribute("age", employee.getAge());
model.addAttribute("id", employee.getId());
return "result";
}
}
2)在Employee.java中,我只是定义了名称,年龄和id的私有字段,并为私有字段定义了getter和setter方法。
3)这是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>SpringMVC form of Employees Info.</display-name>
<servlet>
<servlet-name>EmployeeInfoSubmitPg</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>EmployeeInfoSubmitPg</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
4)这是EmployeeInfoSubmitPg-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: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">
<context:component-scan base-package="com.SpringMVC.Employees" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
5)这是employee.jsp: -
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<title>SpringMVC form of Employees Info.</title>
</head>
<body>
<h2>Employee Information</h2>
<form:form method="POST" action="/EmployeeInfoSubmitPg/addEmployee">
<table>
<tr>
<td><form:label path="name">Name</form:label></td>
<td><form:input path="name" /></td>
</tr>
<tr>
<td><form:label path="age">Age</form:label></td>
<td><form:input path="age" /></td>
</tr>
<tr>
<td><form:label path="id">id</form:label></td>
<td><form:input path="id" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Submit" /></td>
</tr>
</table>
</form:form>
</body>
</html>
6)这是result.jsp: -
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<title>SpringMVC form of Employees Info.</title>
</head>
<body>
<h2>Submitted Employee Information</h2>
<table>
<tr>
<td>Name</td>
<td>${name}</td>
</tr>
<tr>
<td>Age</td>
<td>${age}</td>
</tr>
<tr>
<td>ID</td>
<td>${id}</td>
</tr>
</table>
</body>
</html>
当我输入http://localhost:90/SpringMVC_EmployeeInfoPg/employee时,它正好运行一半 在此之后它显示HTTP状态404错误 - / EmployeeInfoSubmitPg / addEmployee
但是,现在Eclipse在employee.jsp上显示错误,
但是,我在employee.jsp
另外,我忘了提到我从中得到了这个例子 http://www.tutorialspoint.com/spring/spring_mvc_form_handling_example.htm
答案 0 :(得分:0)
form:form method =&#34; POST&#34;行动=&#34; ./ addEmployee&#34;&GT; ,改变这样的形式