Spring MVC:如何指示是否需要路径变量? [英] Spring MVC: how to indicate whether a path variable is required or not?
问题描述
我正在做一个Spring网站.对于控制器方法,我可以使用RequestParam指示是否需要该参数.例如:
I am doing a Spring web. For a controller method, I am able to use RequestParam to indicate whether a parameter it is required or not. For example:
@RequestMapping({"customer"})
public String surveys(HttpServletRequest request,
@RequestParam(value="id", required = false) Long id,
Map<String, Object> map)
我想使用如下所示的PathVariable:
I would like to use PathVariable such as the following:
@RequestMapping({"customer/{id}"})
public String surveys(HttpServletRequest request,
@PathVariable("id") Long id,
Map<String, Object> map)
如何指示是否需要路径变量?我需要将其设置为可选,因为在创建新对象时,在保存之前没有可用的关联ID.
How can I indicate whether a path variable is required or not? I need to make it optional because when creating a new object, there is no associated ID available until it is saved.
感谢帮助!
推荐答案
无法将其设为可选,但是您可以创建两个方法,其中一个具有@RequestMapping({"customer"})
注释,另一个具有@RequestMapping({"customer/{id}"})
注释,然后采取相应措施在每个.
There's no way to make it optional, but you can create two methods with one having the @RequestMapping({"customer"})
annotation and the other having @RequestMapping({"customer/{id}"})
and then act accordingly in each.
这篇关于Spring MVC:如何指示是否需要路径变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!