首页 > 解决方案 > Spring MVC 在下一页传递空值

问题描述

我是 SpringMVC JSP 的初学者,我正在创建一个使用 POST 方法传递值的简单项目。

第 1 页:hello.jsp

<body>
<h1>Record Form</h1>
    <form name="test" id="test" action="test.jsp" method="post">
        <p>Name: <input type = "text" name = "name" /></p>
        <p>Address: <input type = "text" name = "address" /></p>
        <p>Remarks: <input type = "text" name = "remarks" /></p>
        <p><input type="submit" value="Save" /> <input type="reset" value="Reset" /></p>
    </form>
</body>

第二页:test.jsp

<body>
<h1>Result</h1>
    <p>name: ${record.name}</p>
    <p>address: ${record.address}</p>
    <p>remarks: ${record.remarks}</p>
    <a href="hello.jsp">Submit another message</a>
</body>

记录.java

import org.springframework.stereotype.Component;

@Component
public class Record {

    private String name;
    private String address;
    private String remarks;
 //setters getters..

HelloController.java

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import static org.springframework.web.bind.annotation.RequestMethod.POST;

import org.springframework.beans.factory.annotation.Autowired;


@Controller
public class HelloController {

    @Autowired


    @RequestMapping(value = "/")
    public String hello(Record record) {
        return "hello";
    }

    @RequestMapping(value = "/test", method = POST)
    public String test(@RequestParam("name") String name, @RequestParam("address") String address, @RequestParam("remarks") String remarks, Model model) {
        Record record = new Record();
        record.setName(name);
        record.setAddress(address);
        record.setRemarks(remarks);
        model.addAttribute("record", record);
        return "/test";
    }
}

我的问题是,当我单击提交时,没有传递任何值。我一直在检查我的代码,但我看不出有什么问题。谁能帮帮我?

在此处输入图像描述

将代码更改为<form action="test">仅时,会发生此错误。<form action="/test">另外,如果我根据搜索的内容更改为此。没有任何效果。见下图。 在此处输入图像描述

我已经在 web.xml 中有这个

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value></param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

对于我的 pom.xml,我已经添加了这个

 <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.2.3.RELEASE</version>
        </dependency>
    </dependencies>

标签: javaspringrequest-mapping

解决方案


基本上,你需要改变这个:

<form name="test" id="test" action="test.jsp" method="post">

对此:

<form name="test" id="test" action="test" method="post">

/test否则将不会调用映射的控制器方法并test.jsp直接呈现,record模型变量为空。


推荐阅读