java Spring MVC默认值不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16504401/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring MVC default value not working
提问by Shahid Ghafoor
@RequestMapping(value = "/Fin_AddBankAccount", method = RequestMethod.POST)
public @ResponseBody JsonResponse addCoaCategory(
@RequestParam(value="code", required=true) long code,
@RequestParam(value="startFrom", required=true) long startFrom,
@RequestParam(value="name", required=true, defaultValue="N/A") String name)
{
}
defaultValue="N/A" not working , As I did not provide any text in name field , it store null in database instead of "N/A"?
defaultValue="N/A" 不起作用,因为我没有在 name 字段中提供任何文本,它在数据库中存储 null 而不是“N/A”?
回答by Sanjaya Liyanage
What is the point of setting a default value if you really want that parameter. if you mark it as required true(not needed as it is default) then no need of a default value. If that parameter is not mandatory then mark it as false and give a default value.
如果您真的想要该参数,那么设置默认值有什么意义。如果将其标记为 required true(不需要,因为它是默认值),则不需要默认值。如果该参数不是强制性的,则将其标记为 false 并提供默认值。
回答by Grzegorz ?ur
Documentation of Spring RequestParam.required
Spring 文档 RequestParam.required
Default is true, leading to an exception thrown in case of the parameter missing in the request. Switch this to false if you prefer a null in case of the parameter missing.
默认为true,导致请求中缺少参数时抛出异常。如果您希望在缺少参数的情况下使用 null,请将其切换为 false。
From your question I figured out that you are sending parameter name
with empty value using POST request. According to the Spring documentation you should not send name
parameter in the request in order to use default value. Simply remove name
field from HTML form if it is empty.
从您的问题中,我发现您正在name
使用 POST 请求发送带有空值的参数。根据 Spring 文档,您不应name
在请求中发送参数以使用默认值。name
如果字段为空,只需从 HTML 表单中删除字段。
It seems that default values makes more sense for GET requests.
似乎默认值对 GET 请求更有意义。
回答by Vijay Gawade
make sure you don't pass empty string value Valid Methods: 1. Fin_AddBankAccount?name= O/P: name="N/A"
确保您不传递空字符串值有效方法: 1. Fin_AddBankAccount?name= O/P: name="N/A"
- Fin_AddBankAccount? O/P: name="N/A"
- Fin_AddBankAccount?O/P:名称=“不适用”
Invalid Methods: Fin_AddBankAccount?name="" this will set empty string to variable i.e. name="";
无效方法:Fin_AddBankAccount?name="" 这会将空字符串设置为变量,即 name="";
回答by Jason
In my project
在我的项目中
@RequestParam(value="name", required=true, defaultValue="N/A") String name
This code correctly sets name variable as defaultvalue N/A when requestparam "name" was not provided. My guess is you are not inserting this name variable into the table properly so database is storing null instead of "N/A". Please show us or double check the data access object code. Good luck
当未提供 requestparam "name" 时,此代码正确地将 name 变量设置为默认值 N/A。我的猜测是您没有将此名称变量正确插入表中,因此数据库存储的是 null 而不是“N/A”。请向我们展示或仔细检查数据访问对象代码。祝你好运
Thanks @Tiarê Balbi, in fact you do not need "required=true" because defaultValue="N/A" implicitly sets this variable as required=false anyways.
谢谢@Tiarê Balbi,实际上您不需要“required=true”,因为 defaultValue="N/A" 无论如何都隐式地将此变量设置为 required=false。