【发布时间】:2017-04-12 20:37:47
【问题描述】:
我正在 spring mvc 中开发一个简单的应用程序,而不使用 spring 表单标签,目前我的工作是通过使用下面的代码完成的,但为了理解目的,我问了这个问题。
我有两个支持 bean 类,我已绑定到一个 html <form> 标签,这在 spring 表单标签中是不可能的。
豆 1
public class Interim {
private int interimId;
private BigDecimal amount;
private int interimCategory;
// setter n getter
}
豆2
public class Bcr {
private int bcrId;
private BigDecimal cashAmount;
private int interimCategory;
}
html 表单
<form action="/interim" method="get">
<input type="text" name="amount" />
<input type="text" name="cashAmount" />
<input type="text" name="interimCategory" />
<button type="submit" name="Month" > month </button>
</from>
弹簧控制器
@Controller
public TestController {
@RequestMapping(value = "/interim", method = RequestMethod.GET)
public String interimInit(ModelMap map) {
map.addAttribute("interim",new Interim());
map.addAttribute("bcr",new Bcr());
return "interim";
}
/// on form submit
@RequestMapping(value = "/interim", method = RequestMethod.GET, params = "Month")
public String getMonthlyInterim(@ModelAttribute("bcr") Bcr b,ModelMap
modelMap,@ModelAttribute("interim") Interim in) {
}
当我在两个 bean 中提交表单 spring mvc set interimCategory 时,
- 如何告诉spring不要设置Bcr bean的interimCategory?
- 这是在spring创建html表单的好方法吗?
【问题讨论】:
-
您在表单上使用 POST 方法,但是,您的控制器上只有 GET 方法
-
操作已编辑,谢谢 ;-)
-
您在表单上的操作是“/test”,但您没有 RequestMethod 为“/test”的方法
-
出于某种原因,我粘贴了所有原始代码,但我自己编写了表单
-
你不能有 2 个具有相同路径和相同方法 (GET) 的方法
标签: java spring forms spring-mvc