简体   繁体   English

如何将 URL 参数的 null 值传递给 Spring MVC controller?

[英]How to pass a null value for a URL parameter to Spring MVC controller?

Let's say I have a Java Spring MVC controller method like this:假设我有一个 Java Spring MVC controller 方法,如下所示:

@RequestMapping(value="showuser", method={RequestMethod.GET})
public ModelAndView showUser(Model model, @RequestParam Map<String,String> requestParams) throws Exception {

User has three fields (firstname, middlename, lastname) all of which are part of the unique ID for that record.用户具有三个字段(名字、中间名、姓氏),所有这些字段都是该记录的唯一 ID 的一部分。

And middlename can be NULL.中间名可以是 NULL。

I do this to view the user...我这样做是为了查看用户...

http://localhost:8080/myapp/viewuser?firstname=Bob&middlename=Tiberius&lastname=Jones

And when middlename is NULL, my code currently passes no value for middlename, like this...当中间名是middlename时,我的代码目前没有为中间名传递任何值,就像这样......

http://localhost:8080/myapp/viewuser?firstname=Bob&middlename=&lastname=Jones

... but doesn't work because in the requestParams map, the value for middlename gets set to empty string, not NULL. ... 但不起作用,因为在requestParams map 中,中间名的值设置为空字符串,而不是middlename

I need that value to be NULL, not empty string.我需要该值为 NULL,而不是空字符串。

How do I pass NULL as the value for the middlename parameter?如何传递 NULL 作为middlename参数的值?

I searched and found other answers that said to use %00 or %A0 for the value, but neither of these worked.我搜索并发现其他答案说使用%00%A0作为值,但这些都不起作用。 Any ideas?有任何想法吗?

Don't send middlename from frontend不要从前端发送中间名

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM