我在 Spring Controller 中有一个这样的方法;
@RequestMapping(value = "/contact", method = RequestMethod.GET)
public final ModelAndView contact(@RequestParam(value = "id", required = false) int i) {..}
我将参数 i 传递给另一个方法。在那种方法中,我想检查 int i 中是否有值。如果未设置参数(这是可选的),我想将其设置为某个值。我如何在 Spring 中检查这个?
最佳答案
使用 Integer
而不是 int
。如果未设置,则为空。
例如,如果 0 不是有效的 ID 值,您也可以将类型保留为 int
并使用
@RequestParam(value = "id", defaultValue = "0")
参数不传则等于0。
关于java - Spring 可选参数 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25274697/