英文:
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="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.
And middlename can be 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 ...
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.
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论