【问题标题】:JSF websocket update viewScopeJSF websocket 更新 viewScope
【发布时间】:2019-01-19 06:55:37
【问题描述】:

我有一个名为 sampleBean 的 bean,其范围为 viewScope。
这个 bean 从数据库 (MySQL) 加载一些数据。
我的问题是用户之间共享的一些记录。
现在也许 [USER A] 删除了该共享记录,我想更新其他用户的视图。

我无法将范围更改为 ApplicationScope,因为所有记录都共享给所有用户。

如何解决这个问题?
注意:我读了post,但不明白如何解决这个问题。
注意:我通过 JavaEE 8 (webProfile) 使用 Liberty 18.0.0.4

【问题讨论】:

  • 将范围更改为@ApplicationScoped 不会解决任何问题。正如您链接到的帖子所讨论的那样,您可以使用f:websocket 来完成您的上述要求。此标记需要 JSF 2.3。要使其与 Liberty 一起使用,请参阅 ibm.com/support/knowledgecenter/en/SSEQTP_liberty/…。此外,如果您可以向我们提供涵盖您的案例的 minimal reproducible example(可能带有仅提供随机数据的支持 bean),我们可以为您提供特定案例的示例。
  • 我解决了问题,在 JSF 中使用了错误的大小写“@Observer”。
  • @mah454:这个问题在当前状态下对其他人无用,因为不清楚代码(minimal reproducible example 风格)是什么以及应该进行什么更改。你能改进它并创建一个好的对应答案?
  • @Kukeltje,我回答了我的问题,请投票:D
  • 抱歉,不,否决票。见评论。答案不是正确的方法(甚至有点错误),并且问题中没有相应的代码,因此其他人很难看出哪里出了问题

标签: jsf jsf-2.3


【解决方案1】:

我通过这个简单的代码解决了问题。 (我为你分享了这段代码)

public class Information {
    private String name ;
    private String family ; 

    // constructor 
    // Getter & Setter 
    // override equal and hashCode

}

这是一个简单的服务。 (我在这个类上模拟了数据库)

@Stateless
public class InformationService {

    private static final List<Information> db = new ArrayList<>();

    @Inject
    @Push(channel = "infoChannel")
    PushContext push;

    @PostConstruct
    public void init() {
        Information userA = new Information("John", "Vankate");
        Information userB = new Information("Julius", "Sampao");
        db.add(userA);
        db.add(userB);
    }

    public void remove(Information info) {
        db.remove(info);
        push.send("deleteInfo");
    }

    public List<Information> findAll() {
        return db;
    }
}

和简单的 JaxRs 资源:

@Path("/info")
@RequestScoped
public class InformationResources {

    @EJB
    private InformationService informationService;


    @Path("/delete")
    @POST
    @Consumes("application/json")
    public String send(Information information) {
        informationService.remove(information);
        return "Receive : " + information;
    }
} 

现在启动 JSF:

@Named
@ViewScoped
public class InformationBean implements Serializable {

    private Information info ;
    private List<Information> informationList ;

    @EJB
    private InformationService informationService ;


    @PostConstruct
    public void init() {
        informationList = informationService.findAll();
        info = new Information() ;
    }

    public void deleteInformation() {
        informationService.remove(info);
    }

    public Information getInfo() {
        return info;
    }

    public void setInfo(Information info) {
        this.info = info;
    }

    public List<Information> getInformationList() {
        return informationList;
    }

    public void setInformationList(List<Information> informationList) {
        this.informationList = informationList;
    }
}

和xhtml:

<h:body>
    <p:dataTable value="#{informationBean.informationList}" var="info" id="infoTable">
        <p:column rowHeader="name">
            <h:outputText value="#{info.name}"/>
        </p:column>
        <p:column rowHeader="family">
            <h:outputText value="#{info.family}"/>
        </p:column>
        <p:column rowHeader="action">
            <h:form>
                <p:commandButton value="Delete" action="#{informationBean.deleteInformation}">
                    <f:setPropertyActionListener value="#{info}" target="#{informationBean.info}"/>
                </p:commandButton>
            </h:form>
        </p:column>
    </p:dataTable>
    <hr/>
    <f:websocket channel="infoChannel">
        <p:ajax event="deleteInfo" update="infoTable"/>
    </f:websocket>
</h:body>

我已经想过,PushContext 必须在 JSF bean 上实现,现在我明白可以在 service业务逻辑 层实现。
现在您可以从 JaxRs (Rest API) 中删除信息并从 p:dataTable 中删除记录,而无需刷新页面。
注意:此示例使用 @ViewScoped

【讨论】:

  • 在后端服务中添加前端技术(Push)是不好的做法。您发布的链接包含正确操作的信息并且它有效(我一直使用它)而且由于您没有发布您在我们无法提供帮助之前尝试过的内容并且有效地这个 Q/A 有点没用
  • 如果从 web 服务(Rest 或 soap)收到请求,如何更新视图?
  • 响应应该来自 A 服务。 tbat 服务的实现应该对调用或依赖服务隐藏。对于调用者的回调,请使用观察者;ike 在链接中发布
  • @Kukeltje "@Observer" 不适用于 viewScoped bean。如何解决这个问题?
  • 如何不使用 viewscoped bean?请创建一个minimal reproducible example,你真的需要'push'相关的bean作为viewoped bean吗?
猜你喜欢
  • 1970-01-01
  • 2011-07-26
  • 1970-01-01
  • 1970-01-01
  • 2020-02-20
  • 1970-01-01
  • 2012-10-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多