如何一次声明许多JSF托管的bean [英] How to declare many JSF managed beans at once

查看:82
本文介绍了如何一次声明许多JSF托管的bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个使用基于SOAP的Web服务的JSF 2.0应用程序.

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

我想在JSF页面中使用大多数为Web Service生成的客户端类-但是客户端类不是托管bean(也不是CDI bean)...而且我有很多客户端类,认为向所有类添加@ManagedBean@Named注释是可行的...

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:

User类是生成的客户端类-该类只有两个属性(登录名和密码).

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

我希望能够在JSF页面中将值分配给给定用户的属性:

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

然后我要呼叫我的UserService来验证用户身份:

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

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

从JSF页面向User对象分配值的唯一方法(AFAIK)是使User对象成为托管bean(或CDI bean).

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

因为有超过100个客户端类,所以我绝对不想在所有类上添加@ManagedBean@Named批注(我同样不想在脸上为每个类添加message-bean元素-config.xml).

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

即使注释所有类都是可行的选择,该解决方案也会有一个缺点:服务协定(WSDL)可能随时更改,并且我有义务重新生成客户端类...我会放弃带注释的类.

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?

我一直在寻找一种在faces-config.xml中声明包的所有类的方法(下面的示例),但是我既找不到做到这一点的方法,也找不到可行的选择.

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>

推荐答案

只需将User设置为UserService的属性.这也更符合JSF的MVC意识形态. UserService是控制器,User是模型.

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.

因此,

@ManagedBean
@ViewScoped
public class UserService {

    private User user;

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

使用

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

这篇关于如何一次声明许多JSF托管的bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆