作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有一个 Controller 方法定义如下 -
@RequestMapping(method = RequestMethod.POST, value="/callMe")
public String myMethod(@ModelAttribute MyClass myObj, Model model) {
//Do something
}
即使我没有传递 ModelAttribute myObj,我怎样才能调用上面的 Controller 方法。
我不想在没有它的情况下创建另一个 Controller 并复制功能。
最佳答案
模型属性已经是可选的。即使您不传递模型属性,也会创建 myObj。所以检查
if(myObj == null){
//Do method1
}else{
//Do method2
}
将不起作用。
试试这个。像这样在 myClass 中创建一个 boolean 值
private Boolean isGotMyObj = false;
在你的 jsp 中(提交模型属性)添加一个像这样的隐藏输入
<input type="hidden" value="1" name="isGotMyObj" />
然后在 Controller 中执行此操作
@RequestMapping(method = RequestMethod.POST, value="/callMe")
public String myMethod(@ModelAttribute MyClass myObj, Model model) {
if (myObj.getIsGotMyObj()){
//Got model attribute
//Method 1
}else{
//Method 2
}
return "callme";
}
关于java - 在 Spring Controller 方法中使 ModelAttribute 可选,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27519927/
我正在尝试用 Swift 编写这段 JavaScript 代码:k_combinations 到目前为止,我在 Swift 中有这个: import Foundation import Cocoa e
我是一名优秀的程序员,十分优秀!