请求不会从jsp页面转发到调度程序servlet

时间:2016-09-06 18:30:56

标签: java jsp spring-mvc servlets

您好我正在从教程网站学习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>

这是我提交表格页面时遇到的错误 enter image description here

enter image description here

1 个答案:

答案 0 :(得分:1)

您的共享代码中几乎没有错误。此外,您的帖子需要更正,因为它混合了代码和您的评论。 我想指出一些更正。

  1. AdmissionForm.jsp jsp中<form action="submitAdmissionForm.html" method="post">应该足够了。

  2. StudentAdmissionController.java @RequestMapping(value="/submitAdmissionForm.html",method=RequestMethod.POST) public ModelAndView submitAdmissionForm(@ModelAttribute("student") Student student){您应该只接受Student对象。你正在学习弦乐学生。

  3. AdmissionForm.jsp

  4. <h3>Name : ${student.name}</h3> <h3>Place : ${student.place}</h3> 这在您在控制器中设置String时无效。

    希望这会有所帮助。