Spring MVC 中可选的 POST 参数?

发布于 2024-10-30 08:06:43 字数 258 浏览 1 评论 0原文

以下代码:

@RequestMapping(method = RequestMethod.POST)
public ModelAndView editItem(String name, String description)

但是,有时没有传入描述(这是一个比真实示例更简单的示例),我想将描述设为可选,也许可以通过填写默认值(如果没有传入)来填充。

我有 知道怎么做吗?

多谢!

贾森

I have the following code:

@RequestMapping(method = RequestMethod.POST)
public ModelAndView editItem(String name, String description)

However, sometime description is not passed in (this is a simplified example than the real one), and i would like to make description optional, perhaps by filling in a default value if none is passed in.

Anyone have any idea how to do that?

thanks a lot!

Jason

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怪我鬧 2024-11-06 08:06:43

如果您使用的是 Spring MVC 3.0 或更高版本,则只需设置 @RequestParamdefaultValue 参数:

public ModelAndView editItem(@RequestParam(value = "description", defaultValue = "new value") String description)

在 Spring MVC 2.5 中,我建议将 value 标记为 required = false< /code> 并手动检查其值是否为 null:

public ModelAndView editItem(@RequestParam(value = "description", required = false) String description) {
    if (description == null) {
        description = "new value";
    }
    ...
}

另请参阅 @RequestParam注解的对应文档


JDK 8 和 JDK 的更新 Spring 4.1+:现在你可以像这样使用java.util.Optional

public ModelAndView editItem(@RequestParam("description") Optional<String> description) {

    item.setDescription(description.getOrElse("default value"));

    // or only if it's present:
    description.ifPresent(value -> item.setDescription(description));
    ...
}

If you are using Spring MVC 3.0 or higher then just set defaultValue parameter of @RequestParam:

public ModelAndView editItem(@RequestParam(value = "description", defaultValue = "new value") String description)

In Spring MVC 2.5, I suggest to mark value as required = false and check their value against null manually:

public ModelAndView editItem(@RequestParam(value = "description", required = false) String description) {
    if (description == null) {
        description = "new value";
    }
    ...
}

See also corresponding documentation about @RequestParam annotation.


UPDATE for JDK 8 & Spring 4.1+: now you could use java.util.Optional like this:

public ModelAndView editItem(@RequestParam("description") Optional<String> description) {

    item.setDescription(description.getOrElse("default value"));

    // or only if it's present:
    description.ifPresent(value -> item.setDescription(description));
    ...
}
可爱暴击 2024-11-06 08:06:43

不要使用 @RequestParam 作为可选参数,而是采用 org.springframework.web.context.request.WebRequest 类型的参数。例如,

@RequestMapping(method = RequestMethod.POST)
public ModelAndView editItem(
  @RequestParam("name")String name,
  org.springframework.web.context.request.WebRequest webRequest)
{
  String description = webRequest.getParameter("description");

  if (description  != null)
  {
     // optional parameter is present
  }
  else
  {
    // optional parameter is not there.
  }
}

注意:请参阅下文(默认值和必需),了解在不使用 WebRequest 参数的情况下解决此问题的方法。

Instead of using @RequestParam for the optional parameters, take a parameter of type org.springframework.web.context.request.WebRequest. For example,

@RequestMapping(method = RequestMethod.POST)
public ModelAndView editItem(
  @RequestParam("name")String name,
  org.springframework.web.context.request.WebRequest webRequest)
{
  String description = webRequest.getParameter("description");

  if (description  != null)
  {
     // optional parameter is present
  }
  else
  {
    // optional parameter is not there.
  }
}

Note: See below (defaultValue and required) for a way to solve this without using a WebRequest parameter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文