当所选选项具有空值时将嵌套对象设置为 null

发布于 2024-08-28 14:22:39 字数 1724 浏览 5 评论 0原文

我有一个类为用户建模,另一个类为他的国家建模。像这样的事情:

public class User{
    private Country country;
    //other attributes and getter/setters
}

public class Country{
    private Integer id;
    private String name;
    //other attributes and getter/setters
}

我有一个 Spring 表单,其中有一个组合框,以便用户可以选择他的国家/地区,或者可以选择未定义的选项来指示他不想提供此信息。所以我有这样的事情:

<form:select path="country">
    <form:option value="">-Select one-</form:option>
    <form:options items="${countries}" itemLabel="name" itemValue="id"/>
</form:select>

在我的控制器中,我得到带有用户信息的自动填充对象,并且当选择“-选择一个-”选项时,我希望将国家/地区设置为空。因此,我使用这样的自定义编辑器设置了一个 initBinder:

@InitBinder
protected void initBinder(WebDataBinder binder) throws ServletException {
    binder.registerCustomEditor(Country.class, "country", new CustomCountryEditor());
}

我的编辑器执行如下操作:

public class CustomCountryEditor(){
    @Override
    public String getAsText() {
        //I return the Id of the country 
    }

    @Override
    public void setAsText(String str) {
        //I search in the database for a country with id = new Integer(str)
        //and set country to that value
        //or I set country to null in case str == null
    }
}

当我提交表单时,它会起作用,因为当我选择国家/地区时,它会设置为 null “-选择一个-”选项或所选国家/地区的实例。问题是,当我加载表单时,我有一种像下面这样的方法来加载用户信息。

@ModelAttribute("user")
public User getUser(){
   //loads user from database
}

我从 getUser() 获得的对象将国家/地区设置为特定国家/地区(不是空值),但在组合框中未选择任何选项。我已经调试了该应用程序,并且 CustomCountryEditor 在设置和获取文本时工作得很好,尽管 getAsText 方法是为列表“国家”中的每个项目调用的,而不仅仅是“国家”字段。

有什么想法吗? 当我在组合框中没有选择国家/地区选项时,是否有更好的方法将国家/地区对象设置为空?

谢谢

I have a Class which models a User and another which models his country. Something like this:

public class User{
    private Country country;
    //other attributes and getter/setters
}

public class Country{
    private Integer id;
    private String name;
    //other attributes and getter/setters
}

I have a Spring form where I have a combobox so the user can select his country or can select the undefined option to indicate he doen't want to provide this information. So I have something like this:

<form:select path="country">
    <form:option value="">-Select one-</form:option>
    <form:options items="${countries}" itemLabel="name" itemValue="id"/>
</form:select>

In my controller I get the autopopulated object with the user information and I want to have country set to null when the "-Select one-" option has been selected. So I have set a initBinder with a custom editor like this:

@InitBinder
protected void initBinder(WebDataBinder binder) throws ServletException {
    binder.registerCustomEditor(Country.class, "country", new CustomCountryEditor());
}

and my editor do something like this:

public class CustomCountryEditor(){
    @Override
    public String getAsText() {
        //I return the Id of the country 
    }

    @Override
    public void setAsText(String str) {
        //I search in the database for a country with id = new Integer(str)
        //and set country to that value
        //or I set country to null in case str == null
    }
}

When I submit the form it works because when I have country set to null when I have selected
"-Select one-" option or the instance of the country selected. The problem is that when I load the form I have a method like the following one to load the user information.

@ModelAttribute("user")
public User getUser(){
   //loads user from database
}

The object I get from getUser() has country set to a specific country (not a null value), but in the combobox is not selected any option. I've debugged the application and the CustomCountryEditor works good when setting and getting the text, thoughgetAsText method is called for every item in the list "countries" not only for the "country" field.

Any idea?
Is there a better way to set null the country object when I select no country option in the combobox?

Thanks

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

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

发布评论

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

评论(2

在你怀里撒娇 2024-09-04 14:22:39

问题是我正在从 Spring Security 主体对象中检索对象 User 。我已经更改了 getUser 方法,因此我从 Spring Security 获取 Id 并在数据库中搜索,现在一切正常,尽管我不明白为什么会发生这种情况,因为我可以在调试中看到该国家/地区已由 Spring 加载到用户中安全。

The problem was that I was retrieving the object User from Spring Security principal object. I've changed my getUser method so I get Id from Spring Security and the I search in database and now everything works, though I don't understand why this happend cause I can see in debug that the country was loaded in the user by Spring Security.

身边 2024-09-04 14:22:39

您是否检查了用户的数据库表,在保存用户时设置了国家/地区的值...是否为空,是否有某些值?

Did you check the database table for User what value for country is being set when you save the user ... is it null of is there some value?

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