user1348997
user1348997

Reputation:

How to declare many JSF managed beans at once

I'm developing a JSF 2.0 app that consumes a SOAP-based web service.

I want to use in the JSFs pages most of the generated client classes for the Web Service - but the client classes are not managed beans (nor CDI beans)... and as there are a lot of client classes I don't think is feasible to add @ManagedBean or @Named annotations to all classes...

Let me give you an example so things might get a bit clearer:

The User class is a generated client class - this class has only two attributes (login and password).

I want to be able to assign values to the attributes of a given user in the JSF page:

<h:inputText value="#{user.name}"/>
<h:inputText value="#{user.password}"/>

And then I want to call my UserService to authenticate the user:

<h:commandButton value="Login" action="#{userService.authenticate}"/>

The only way (AFAIK) to assign a value to the User object from the JSF page is by making the User object a managed bean (or a CDI bean).

As there are more than 100 client classes, I definitely don't want to add @ManagedBean or @Named annotations on all classes (I equally don't want to add message-bean element for each class in the faces-config.xml).

And even if annotating all classes were a feasible option, the solution would have a drawback: the service contract (WSDL) might change at any minute and I would be obligated to regenerate the client classes... I'd loose the annotated classes.

What is the best way to handle this (kind of) issue?

I've looked for a way to declare all classes of a package in the faces-config.xml (example below), but I could find neither a way to do that nor a feasible alternative.

<managed-beans>
    <managed-beans-package>x.y.z.ws.client</managed-beans-package>
    <managed-beans-scope>none</managed-beans-scope>
</managed-beans>

Upvotes: 1

Views: 1240

Answers (1)

BalusC
BalusC

Reputation: 1108712

Just make the User a property of UserService. That's also more conform JSF's MVC ideology. The UserService is the controller and the User is the model.

Thus so,

@ManagedBean
@ViewScoped
public class UserService {

    private User user;

    // ... (don't forget to prepare user in (post)constructor if "new" user)
}

with

<h:inputText value="#{userService.user.name}" />
<h:inputText value="#{userService.user.password}" />

Upvotes: 2

Related Questions