如何将空值作为URL参数传递给Spring MVC控制器?

huangapple go评论65阅读模式
英文:

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

问题

让我们假设我有一个像这样的Java Spring MVC控制器方法:

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

用户有三个字段(firstname,middlename,lastname),它们都是该记录的唯一标识的一部分。

并且middlename可以为NULL。

我用以下方式查看用户...

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

当middlename为NULL时,我的代码当前不传递middlename的任何值,像这样...

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

...但它不起作用,因为在requestParams映射中,middlename的值被设置为空字符串,而不是NULL。

我需要该值为NULL,而不是空字符串。

如何将NULL作为middlename参数的值传递?

我搜索并找到其他答案,说要使用%00%A0作为值,但这两者都不起作用。有什么建议吗?

英文:

Let's say I have a Java Spring MVC controller method like this:

@RequestMapping(value=&quot;showuser&quot;, method={RequestMethod.GET})
public ModelAndView showUser(Model model, @RequestParam Map&lt;String,String&gt; requestParams) throws Exception {

User has three fields (firstname, middlename, lastname) all of which are part of the unique ID for that record.

And middlename can be NULL.

I do this to view the user ...

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

And when middlename is NULL, my code currently passes no value for middlename, like this ...

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

... but doesn't work because in the requestParams map, the value for middlename gets set to empty string, not NULL.

I need that value to be NULL, not empty string.

How do I pass NULL as the value for the middlename parameter?

I searched and found other answers that said to use %00 or %A0 for the value, but neither of these worked. Any ideas?

答案1

得分: 3

不要从前端发送中间名。

英文:

Don't send middlename from frontend

huangapple
  • 本文由 发表于 2020年9月23日 01:17:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/64014675.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定