2016-09-06 92 views
0

你好我正在學習Spring MVC從教程網站,我有這種情況,當我從jsp頁面提交我的html應該得到處理並返回成功頁面,但是當我'提交我的表格它給我404 error。所以有些機構請幫我解決我的問題,下面是我的完整代碼。請求不是從jsp頁面轉發到調度程序servlet

最初,當我給請求作爲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> 

這是我的分發程序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; 
    } 
} 

這是我的學生BeanStudent.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> 

這時候我提交表單頁面,我發現了錯誤 enter image description here

enter image description here

+1

我建議在'studentAdmissionForm'方法的'@ModelAttribute('student')student'後添加'BindingResult result'。 – 2016-09-06 18:39:51

回答

1

,我們在您共享的代碼少犯錯誤。此外,您的帖子需要更正,因爲它會混淆代碼和評論。 我想指出一些更正。

  1. AdmissionForm.jsp <form action="submitAdmissionForm.html" method="post">應該足夠在jsp中。

  2. StudentAdmissionController.java @RequestMapping(value="/submitAdmissionForm.html",method=RequestMethod.POST) public ModelAndView submitAdmissionForm(@ModelAttribute("student") Student student){您應該只拿到Student對象。你正在學習絃樂學生。

  3. AdmissionForm.jsp

<h3>Name : ${student.name}</h3> <h3>Place : ${student.place}</h3> 因爲你是在控制器設置字符串這將無法正常工作。

希望這會有所幫助。

+0

非常感謝你解決了我的問題 –

+1

很高興聽到這個消息。你能接受答案嗎? –

+0

是的,我已經用你的答案修改了我的代碼,它解決了我的問題。謝謝你的快速回復 –

相關問題