简体   繁体   English

JSF,表单中的多bean更新属性

[英]JSF, multiple bean update property in a form

I edited my question to be more precise as I have more time to write it. 由于我有更多时间来编写,因此我将问题编辑得更精确。

I have a JSF form that should modify the value of the different properties of a dog : 我有一个JSF表单,应该修改狗的不同属性的值:

<h:form id="submit">
     <h:outputLabel value="Dog name:"/>
     <h:inputText value="#{User.dog.name}" id="dogName"/>
     <h:outputLabel value="Name :"/>
     <h:inputSecret value="#{User.name}" id="name" />
     <h:commandButton type="submit" value="Submit" />
</h:form>

This is my managed bean User.java : (All the getter and setter are good and valid, as this is a bean constructor is empty). 这是我的托管bean User.java :(所有getter和setter都很好且有效,因为这是一个bean构造函数为空)。 (Initially Dog property is initialized in a validation method, so it has a value and is not null ) (最初Dog属性在验证方法中初始化,因此它具有值not null

public class User {
    public User() {}
    String  name;
    Dog dog;

    (...get, set, ect...)

This is an other bean that I have not set managed as it is only used by User class Dog.java : 这是我没有设置托管的其他bean,因为它仅由User类Dog.java

public class Dog{
    public User() {}
    String  dog_name;

(...)

Offcourse this is a simple exemple for understanding the thing. 当然,这是一个理解事物的简单例子。

When I send the form, User.name property will update but not the User.dog.name property. 当我发送表单时, User.name属性将更新但不会更新User.dog.name属性。

How can both java classes' values be updated ? 如何更新两个java类的值?

After the form is submitted I show the current values, only the User.name has changed : 提交表单后,我显示当前值,只有User.name已更改:

System.out.println(User.name); 的System.out.println(User.name); //value changed after form is submitted System.out.println(User.dog.name); //提交表单后更改值System.out.println(User.dog.name); //value NOT changed after form is submitted //提交表单后不会更改值

I dont know if you understand my problem here, I want to manipulate the Dog class properties within my JSF form althouth I wont modify the Dog bean directly, only the User.Dog ... 我不知道你是否理解我的问题,我想在我的JSF表单中操纵Dog类属性,我不会直接修改Dog bean,只有User.Dog ...

By the way, faces-config is ok : 顺便说一句,faces-config是可以的:

EDIT : I have added a for my User managed bean. 编辑:我为我的用户管理的bean添加了一个。 Although, nothing is changed... 虽然,没有任何改变......

<managed-property>
    <property-name>dog</property-name>
    <property-class>package.Dog</property-class>
    <value>#{Dog}</value>
</managed-property>

You need to preinstantiate the nested beans during construction or initialization of the parent beans. 您需要在构建或初始化父bean期间预先实例化嵌套bean。 JSF won't do that for you. JSF不会为你做那件事。

So instead of: 所以代替:

public class User {
    Dog dog;
}

you need to instantiate it directly: 你需要直接实例化它:

public class User {
    Dog dog = new Dog();
}

or in constructor: 或者在构造函数中:

public class User {
    Dog dog;
    public User() {
        this.dog = new Dog();
    }
}

or if Dog is actually a managed bean, inject it as managed property in User by faces-config.xml : 或者如果Dog实际上是托管bean,则通过faces-config.xml将其作为托管属性注入User

<managed-bean>
    <managed-bean-name>dog</managed-bean-name>
    <managed-bean-class>mypackage.Dog</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
</managed-bean>

<managed-bean>
    <managed-bean-name>user</managed-bean-name>
    <managed-bean-class>mypackage.User</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
    <managed-property>
        <property-name>dog</property-name>
        <value>#{dog}</value>
    </managed-property>
</managed-bean>

In this all I assume that your properties and getters and setters are named according the Javabean naming conventions . 在这一切中,我假设您的属性和getter以及setter是根据Javabean命名约定命名的

This should work. 这应该工作。 I suggest to run the code through a debugger but my first guess would be that User.dog is null . 我建议通过调试器运行代码,但我的第一个猜测是User.dognull Also, I'm a bit wary by the upper case bean name User . 另外,我对大写bean名称User有点警惕。 That should be user (unless you're referring to static fields in the class User which would be a terrible mistake in a JSF environment). 那应该是user (除非你指的是User类中的静态字段,这在JSF环境中会是一个可怕的错误)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM