我能够从我的student.jsp文件中显示我的表单,当我点击提交按钮时,它应该被重定向到main.jsp文件,其中包含我给出的输入结果在上一页中,即。在形式。但是main.jsp页面没有显示,而是我得到了404.
这是NoticesController.java:
package com.mvc.spring;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class NoticesController {
@RequestMapping(value = "/student", method = RequestMethod.GET)
public String showHello(Model model)
{
return "student";
}
@RequestMapping(value = "/main", method = RequestMethod.POST)
public String form(@ModelAttribute("StudentModel") Student student, Model model)
{
System.out.println("Inside form");
model.addAttribute("Student", student);
return "main";
}
}
这是Student.java:
package com.mvc.spring;
import org.springframework.stereotype.Controller;
public class Student {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
这是我的student.jsp:
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<html>
<head>
<title>This is student.jsp</title>
</head>
<body>
<form action="main.jsp" method="POST">
ID: <input type="text" name="id"><br />
Name: <input type="text" name="name" />
<input type="submit" value="Submit" />
</form>
</body>
</html>
这是我的main.jsp:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Using GET and POST Method to Read Form Data</title>
</head>
<body>
<p>${Student.id}</p>
<p>${Student.name}</p>
</body>
</html>
这是我的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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>WebMVC</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>notices</display-name>
<servlet-name>notices</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>notices</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<description>Spring Tutorial</description>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/spring</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
这是我的通知-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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.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-3.2.xsd">
<context:component-scan base-package="com.mvc.spring"></context:component-scan>
<mvc:annotation-driven></mvc:annotation-driven>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="jspViewResolver">
<property name="prefix" value="/WEB-INF/jsps/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
这是日志:
org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/notices/] in DispatcherServlet with name 'notices'
答案 0 :(得分:3)
我通过以下方式解决了我的问题:
这是Controller类:
package com.mvc.spring;
import java.util.List;
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.mvc.spring.database.Student;
import com.mvc.spring.service.StudentService;
@Controller
public class NoticesController {
@RequestMapping(value = "/student", method = RequestMethod.GET)
public ModelAndView showHello(Model model) {
return new ModelAndView("student", "command", new StudentSazz());
}
@RequestMapping(value = "/main", method = RequestMethod.POST)
public String form(@ModelAttribute("StudentModel") StudentSazz student,
ModelMap model) {
System.out.println("Inside form");
model.addAttribute("name", student.getName());
model.addAttribute("id", student.getId());
model.addAttribute("student", student);
return "main";
}
}
这是student.jsp文件:
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<html>
<head>
<title>This is student.jsp</title>
</head>
<body>
<h2>Student Information</h2>
<form:form method="POST" action="/notices/main">
<table>
<tr>
<td><form:label path="name">Name</form:label></td>
<td><form:input path="name" /></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>
这是main.jsp文件:
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Using GET and POST Method to Read Form Data</title>
</head>
<body>
<h2>Submitted Student Information</h2>
<table>
<tr>
<td>Name</td>
<td>${name}</td>
</tr>
<tr>
<td>ID</td>
<td>${id}</td>
</tr>
</table>
</body>
</html>
基本上,我所做的是使用控制器类中的Student对象来获取所有参数,并在.jsp文件中使用它们只使用$表示法。这很好。
答案 1 :(得分:1)
如果您更改这样的表单操作会怎样:
<form action="/main" method="POST">