Java 我想根据表单中输入的数字更改屏幕转换目的地
我开始使用Spring Boot+Thymeleaf+MySQL进行开发 例如,如果在输入表单中输入“1”(DB ID), 我想过渡到 但无法通过获取表单中输入的值来转换屏幕。 请帮忙 [html]Java 我想根据表单中输入的数字更改屏幕转换目的地,java,html,mysql,spring,thymeleaf,Java,Html,Mysql,Spring,Thymeleaf,我开始使用Spring Boot+Thymeleaf+MySQL进行开发 例如,如果在输入表单中输入“1”(DB ID), 我想过渡到 但无法通过获取表单中输入的值来转换屏幕。 请帮忙 [html] <form method="post" action="/edit"> <h1>Please input the number</h1> <input type="text" name="id" th:value="${id}" /&
<form method="post" action="/edit">
<h1>Please input the number</h1>
<input type="text" name="id" th:value="${id}" />
<input type="submit" value="Send" >
<br>
<a th:href="@{/}">Back enter display</a>
</form>
我确认,如果我直接在浏览器中输入URL,屏幕会转换
Ex)
请告诉我如何修复它。一旦表单到达客户端,它就具有固定的操作属性。正常表单提交不会考虑此问题的输入值 您有两种选择,都使用javascript
- 在输入值更改时更改窗体的action属性,将onchange事件绑定到输入以了解何时获取该值并相应地修改action属性
- 使用preventDefault截取表单提交,并使用修改的action属性从javascript手动提交表单
@RequestParam(“id”)整数id
而不是@PathVariable
。您的控制器代码应如下所示:
@RequestMapping(value = "/edit", method = RequestMethod.POST)
@Transactional(readOnly = false)
public ModelAndView update(@ModelAttribute MyData mydata, @RequestParam("id") Integer id, ModelAndView mav) {
mydata.setId(id);
repository.saveAndFlush(mydata);
return new ModelAndView("redirect:/");
}
谢谢你的回答。我做了如下修改和尝试公共ModelAndView更新(@ModelAttribute MyData MyData,@RequestParam(“id”)整数id,ModelAndView mav){--但是,结果是失败的,错误内容是相同的。我认为html描述是错误的。我特别认为以下内容是错误的:---请告诉我如何修复。请再次检查我的答案,我刚刚更新了它。请看一下
@RequestMapping(value=“/edit”,method=RequestMethod.POST)
line。我删除了{id}部分。
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Wed Oct 09 19:26:41 JST 2019
There was an unexpected error (type=Not Found, status=404).
No message available
@RequestMapping(value = "/edit", method = RequestMethod.POST)
@Transactional(readOnly = false)
public ModelAndView update(@ModelAttribute MyData mydata, @RequestParam("id") Integer id, ModelAndView mav) {
mydata.setId(id);
repository.saveAndFlush(mydata);
return new ModelAndView("redirect:/");
}