How to declare many JSF managed beans at once

拥有回忆 提交于 2019-12-24 10:44:38

问题


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>

回答1:


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}" />


来源:https://stackoverflow.com/questions/10677926/how-to-declare-many-jsf-managed-beans-at-once

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!