开发者

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 :

<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). (Initially Dog property is initialized in a validation method, so it has a value and is 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 :

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.

How can both java classes' values be updated ?

After the form is submitted I show the current values, only the User.name has changed :

System.out.println(User.name); //value changed after form is submitted System.out.println(User.dog.name); //value NOT changed after form is submitted

I dont know if you un开发者_StackOverflowderstand 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...

By the way, faces-config is ok :

EDIT : I have added a for my User managed 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. JSF won't do that for you.

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:

<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.


This should work. I suggest to run the code through a debugger but my first guess would be that User.dog is null. Also, I'm a bit wary by the upper case bean name 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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜