java - 请求未从 jsp 页面转发到调度程序 servlet

标签 java jsp spring-mvc servlets

您好,我正在从教程网站学习 Spring MVC,我遇到这样的情况:当我从 jsp 页面提交 html 时,它应该得到处理并返回成功页面,但是当我提交表单时,它给了我 404 错误。所以请有人帮助我如何解决我的问题,下面是我的完整代码。

最初当我以 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>

这是我的前端 Controller 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. 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 对象。您正在选读 String 学生。

  3. AdmissionForm.jsp

<h3>Name : ${student.name}</h3> <h3>Place : ${student.place}</h3> 当您在 Controller 中设置字符串时,这将不起作用。

希望这会有所帮助。

关于java - 请求未从 jsp 页面转发到调度程序 servlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39355631/

相关文章:

Java - 图标未对齐的 JLabel(向右)

java - 在 Spring 中为 Controller 操作生成 URL

java - Spring MVC 在 WEB-INF/views/package 中找不到合适的 jsp

javascript - Java Spring 项目中的 Angular2

java - 模型类名与 spring 验证注解同名

java - 在 DAO 中使用准备好的语句检索数据时出现问题

java - 如何从 JSP 中的文件类型获取完整路径?

mysql - JSP + Spring + MySQL 提交时填充表

java - DispatcherServler 不工作

java - 有没有更简洁的方法可以将许多对象添加到数组列表中?