为什么我的ViewScoped bean无法在h:commandButton中幸存? [英] Why is my ViewScoped bean not surviving h:commandButton?

查看:91
本文介绍了为什么我的ViewScoped bean无法在h:commandButton中幸存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JBoss AS 7.1.0.Final上进行部署.

Deploying on JBoss AS 7.1.0.Final.

我有一个非常简单的测试应用程序.它一直按预期运行,直到第二天(著名的最后一句话),并且不再做最基本的事情,即设置输入组件的值并在操作组件中使用它.我已经把这东西简化为基本知识,无法弄清楚到底是怎么回事.

I have a very simple test app. It was working as expected until the other day (famous last words) and is no longer doing the most basic thing, namely setting the value of the input component and using it in the action component. I have stripped this thing down to the basics and can not figure out what is going on.

index.xhtml在这里

index.xhtml is here

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:ui="http://java.sun.com/jsf/facelets"
  xmlns:h="http://java.sun.com/jsf/html">

<h:head>
  <title>contacts</title>
</h:head>
<h:form>
    <h:outputLabel value="Message:" />
    <h:inputText value="#{contactView.siteCode}" />
    <h:commandButton  action="#{contactView.save}" value="Save" />

</h:form>

</html>

ViewScoped bean在这里

ViewScoped bean is here

@Named
@ViewScoped
public class ContactView implements Serializable {

    public ContactView() {
    }

    private String siteCode;

    public String getSiteCode() {
        System.out.println("getSiteCode: "+ siteCode);
        return siteCode;
    }

    public void setSiteCode(String siteCode) {
        System.out.println("setSiteCode: "+ siteCode);
        this.siteCode = siteCode;
    }

    public String save(){
        System.out.println("Saving sitecode: " + siteCode);
        return "index.jsf";
    }


}

我做错了什么?当我点击保存按钮时,我会在输出中得到这个

What am I doing wrong? When I click on the save button I get this in the output

10:50:37,663 INFO  [stdout] (http--0.0.0.0-8080-2) setSiteCode: 22
10:50:37,663 INFO  [stdout] (http--0.0.0.0-8080-2) Saving sitecode: null
10:50:37,663 INFO  [stdout] (http--0.0.0.0-8080-2) getSiteCode: null

推荐答案

这是因为Bean是由CDI @Named而不是JSF @ManagedBean管理的.软件包javax.faces.bean的JSF范围注释仅适用于JSF管理的bean.在CDI托管bean上,您需要使用javax.enterprise.context中的CDI注释.但是,CDI没有视图范围的概念.最接近的是@ConversationScoped,但是管理起来更复杂.如果您未在CDI托管Bean上指定范围,则它将默认为请求范围.

That's because the bean is managed by CDI @Named, not by JSF @ManagedBean. JSF scope annotations of the package javax.faces.bean only works on beans managed by JSF. On a CDI managed bean, you need to use CDI annotations from javax.enterprise.context instead. However, CDI doesn't have a concept of the view scope. Closest is @ConversationScoped, but this is more complex to manage. When you don't specify a scope on a CDI managed bean, it will default to the request scope.

每当要使用@ViewScoped时,请确保您的bean由JSF管理.

Make sure that your bean is managed by JSF whenever you want to use @ViewScoped.

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

@ManagedBean
@ViewScoped
public class ContactView implements Serializable {
    // ...
}

此外,您还需要确保只要想保留视图范围,操作方法都将返回nullvoid.

Further, you also need to make sure that your action methods return null or void whenever you want to retain the view scope.

这篇关于为什么我的ViewScoped bean无法在h:commandButton中幸存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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