在 Spring 请求处理程序中处理请求中传入的空值

发布于 2024-12-11 04:14:22 字数 1056 浏览 0 评论 0 原文

在春季 3:
我的Bean:

public class UserFormBean {

    private String userEmail;
    private String userMobNo;

    public String getUserEmail() {
        return userEmail;
    }
    public void setUserEmail(String userEmail) {
        this.userEmail = userEmail;
    }
    public String getUserMobNo() {
        return userMobNo;
    }
    public void setUserMobNo(String userMobNo) {
        this.userMobNo = userMobNo;
    }

}

以及我在控制器中的请求处理程序:

@RequestMapping(value = "/userData", method = RequestMethod.GET)
String userData(UserFormBean bean){
    //code to handle incoming data
}

如果用户在请求时未在 'userEmail''userMobNo' 属性中设置任何值,则 spring 默认设置 <这些属性中的 code>null ,当我通过 String str = bean.getUserEmail(); 在请求处理程序中获取这些属性的值时,它会返回双引号中的 null喜欢“空”

当这些 null 值包含 null 时,有什么方法可以将它们转换为 "" (blank) 因为我创建了一个额外的处理程序来处理这些 "" (blank)代码>“空”值。或者有更好的想法来解决这个问题。

谢谢

In Spring 3:
My Bean:

public class UserFormBean {

    private String userEmail;
    private String userMobNo;

    public String getUserEmail() {
        return userEmail;
    }
    public void setUserEmail(String userEmail) {
        this.userEmail = userEmail;
    }
    public String getUserMobNo() {
        return userMobNo;
    }
    public void setUserMobNo(String userMobNo) {
        this.userMobNo = userMobNo;
    }

}

And my request handler in Controller:

@RequestMapping(value = "/userData", method = RequestMethod.GET)
String userData(UserFormBean bean){
    //code to handle incoming data
}

if user dosen't set any value in 'userEmail' and 'userMobNo' attributes at request time, spring by default sets null in these attributes and when i am getting value of these attributes in my request handler by String str = bean.getUserEmail(); it returns me null in double quote like "null".

Is there any way to convert these null values as "" (blank) when they contain null because i made an extra handler to handle these "null" values. Or some better idea to resolve this issue.

Thanks

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

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

发布评论

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

评论(3

慕烟庭风 2024-12-18 04:14:22

我认为最简单的方法是将属性初始化为空字符串:

private String userEmail = "";
private String userMobNo = "";

I think the easiest way would be to initialize the properties to the empty String:

private String userEmail = "";
private String userMobNo = "";
千柳 2024-12-18 04:14:22

我怀疑 setter 是用“\”null\””调用的新值。试试这个:

public void setUserEmail(final String newValue)
{
  if (StringUtils.isNotBlank(newValue) &&
      "\"null\"".equals(newValue))
  {
    userEmail = newValue;
  }
  else
  {
    userEmail = StringUtils.EMPTY;
  }
}

I suspect the setter is called with "\"null\"" for the new value. Try this:

public void setUserEmail(final String newValue)
{
  if (StringUtils.isNotBlank(newValue) &&
      "\"null\"".equals(newValue))
  {
    userEmail = newValue;
  }
  else
  {
    userEmail = StringUtils.EMPTY;
  }
}
吹泡泡o 2024-12-18 04:14:22

您可以定义命名“Web 方法”的标准,例如:

@RequestMapping(value = "/userData", method = RequestMethod.GET)
String webUserData(UserFormBean bean){
    //code
}

当所有方法都以 web* 开头时,您可以编写一个方面来根据需要初始化所有参数。

如果您需要有关创建和定义方面的帮助,请直接询问。

更新:创建和定义方面

我不会为您编写代码,因为通过阅读文档来学习对您来说要好得多 - 您将了解很多额外的细节。

也就是说,创建切面的步骤是:

  1. 启用 AspectJ 支持 - 此步骤在整个应用程序中仅执行一次
  2. 声明一个切面 - 创建一个具有 @Aspect 注释的类
  3. 在方面内声明一个切入点 - 指定代码中的兴趣点,方面将围绕该点执行一些操作,在您的情况下为 web* 方法
  4. 声明建议 在你的方面 - @Before、@AfterReturning、@AfterThrowing、@After、@Around,在这里您将编写特定的方面代码

在您的方面代码中,您将必须访问 当前方法参数 并执行设置空字符串值的逻辑字符串。

You could define a standard for naming your "web methods", for example:

@RequestMapping(value = "/userData", method = RequestMethod.GET)
String webUserData(UserFormBean bean){
    //code
}

When all your methods start with web*, then you can write an aspect that will initalize all arguments as you would like.

If you need help about creating and defining an aspect, just ask.

UPDATE: creating and defining an aspect

I will not write the code for you, because it's much better for you to learn by reading the documentation - you will get to know a lot of extra details.

That said, the steps to create an aspect are:

  1. Enable AspectJ support - this step is done only once in your whole app
  2. Declare an aspect - create a class that has an @Aspect annotation
  3. Declare a pointcut inside your aspect - specify the point of interest in your code, around which the aspect will do some stuff, in your case, the web* methods
  4. Declare an advice inside your aspect - @Before, @AfterReturning, @AfterThrowing, @After, @Around, here you will write your specific aspect code

In your aspect code you will have to access the current method arguments and do your logic for setting the empty string values for Strings.

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