gpt4 book ai didi

java - Spring MVC 在下一页传递 null 值

转载 作者:行者123 更新时间:2023-11-30 01:43:13 26 4
gpt4 key购买 nike

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

第一页: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";
}
}

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

enter image description here

将代码更改为 <form action="test"> 时只是,出现这个错误。另外如果我改成这个<form action="/test">根据我的搜索。什么都不起作用。见下图。 enter image description here

我已经在 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>

最佳答案

基本上,您需要更改此:

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

对此:

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

否则,映射在 /test 的 Controller 方法将不会被调用,并且 test.jsp 会直接呈现,其中 record 模型变量为空。

关于java - Spring MVC 在下一页传递 null 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59209339/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com