java - 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>
解决方案
基本上,你需要改变这个:
<form name="test" id="test" action="test.jsp" method="post">
对此:
<form name="test" id="test" action="test" method="post">
/test
否则将不会调用映射的控制器方法并test.jsp
直接呈现,record
模型变量为空。
推荐阅读
- javascript - 如何在 JavaScript 中将默认导出和命名导出合并到一个语句中?
- typescript - 在字段中键入 graphql 联合类型
- javascript - 使用php访问远程服务器中的文件显示错误
- node.js - 如何从 REST API 调用中保存 PDF
- list - 如何将类列表的成员转换为列表
在一行代码中? - javascript - “node_modules/express/lib/express.js”中的包试图导入 Node 标准库模块“events”
- containers - Inform7 无法重新进入容器
- python - Vim 不使用 ":!command" 替换终端输出
- python-3.x - Python 3.8.3 中的 ModuleNotFoundError
- recursion - 函数是递归的还是带索引的?