2016-09-06 59 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

是的,我已经用你的答案修改了我的代码,它解决了我的问题。谢谢你的快速回复 –

相关问题