[英]Request is not forwarded from jsp page to dispatcher servlet
您好,我是從一個教程站點學習Spring MVC
,我遇到的情況是,當我從jsp
頁面提交html時,應該對其進行處理並返回成功頁面,但是當我提交表單時,它給了404 error
。 因此,請一些機構幫我解決下面的問題,這是我完整的代碼。
最初,當我以http:// localhost:3399 / FristSpringMVCProject / admissionForm.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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>FristSpringMVCProject</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>
這是我的Dispatcher Servlet spring-dispatcher-servlet.xml
-------------------------------------------------- ---------
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
<context:component-scan base-package="com.gontuseries.hellocontroller" />
<mvc:annotation-driven/>
<!-- <bean id="HandlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
<bean name="/welcome.html" class="com.gontuseries.hellocontroller.HelloController"></bean> -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
這是我的前端控制器StudentAdmissionController.java
-------------------------------------------------- ---------
package com.gontuseries.hellocontroller;
import org.springframework.stereotype.Controller;
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 StudentAdmissionController {
@RequestMapping(value="/admissionForm.html",method=RequestMethod.GET)
public ModelAndView getAdmissionForm(){
System.out.println("inside getAdmissionForm");
ModelAndView model=new ModelAndView("AdmissionForm");
return model;
}
@RequestMapping(value="/submitAdmissionForm.html",method=RequestMethod.POST)
public ModelAndView submitAdmissionForm(@ModelAttribute("student") String student){
ModelAndView model=new ModelAndView("AdmissionSuccess");
model.addObject("message","Thanks for registering with us");
model.addObject("student",student);
return model;
}
}
這是我的學生Bean
Student.java
------------------------------------
package com.gontuseries.hellocontroller;
public class Student {
private String name;
private String place;
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public String getPlace() {
return place;
}
public void setPlace(String place) {
this.place = place;
}
}
這是我的AdmissionForm.jsp
----------------------------
<html>
<head>
</head>
<body>
<h1>Please fill following details to complete registration</h1>
<form action="/FirstSpringMVCProject/submitAdmissionForm.html" method="post">
<p>Student's Name : <input type="text" name="name"/></p>
<p>Place : <input type="text" name="place"/></p>
<input type="submit" value="Submit Details"/>
</form>
</body>
</html>
This is my AdmissionSuccess.jsp
-------------------------------
<html>
<head>
</head>
<body>
<h1>Your request have been processed successfully</h1>
<h2>${message}</h2>
<h2>with following details...</h2>
<h3>Name : ${student.name}</h3>
<h3>Place : ${student.place}</h3>
</body>
</html>
您的共享代碼中幾乎沒有錯誤。 另外,您的帖子還需要更正,因為它會將代碼和您的注釋混在一起。 我想指出一些更正。
在jsp中,AdmissionForm.jsp <form action="submitAdmissionForm.html" method="post">
就足夠了。
StudentAdmissionController.java @RequestMapping(value="/submitAdmissionForm.html",method=RequestMethod.POST) public ModelAndView submitAdmissionForm(@ModelAttribute("student") Student student){
您應該只使用Student對象。 您正在接受String Student。
AdmissionForm.jsp
<h3>Name : ${student.name}</h3> <h3>Place : ${student.place}</h3>
當您在控制器中設置String時,這將不起作用。
希望這會有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.