English 中文(简体)
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 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...

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





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签