首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >对于setter和getter有不同的值。

对于setter和getter有不同的值。
EN

Stack Overflow用户
提问于 2015-01-21 16:23:08
回答 1查看 189关注 0票数 0

在jsf(mojarra2.1)-application中,我从下面的sessionbean调用一个布尔值。一切都很好,直到我用session.invalidate进行注销和新的登录。在新登录后,getter和setter的detailView值是不同的。这意味着:

  • 调用setter方法,并将detailView的值设置为true。
  • 之后,调用getter方法,并在detailView的客户端接收值“false”!!

您也可以通过这些输出线看到这一点:

代码语言:javascript
运行
复制
System.out.println("getdetailView:" + detailView + ": " + sessionId )

代码语言:javascript
运行
复制
System.out.println("setdetailView:" + detailView + ": " + sessionId );

当我比较这两个方法调用的sessionIds时,我们可以看到这两个方法都有相同的sessionId。

代码语言:javascript
运行
复制
@ManagedBean(name = "sessionBean")
@SessionScoped
public class SessionBean implements Serializable {
…

private boolean detailView = false;
public boolean isDetailView() {

    FacesContext context = FacesContext.getCurrentInstance();
    HttpSession session = (HttpSession) context.getExternalContext().getSession(false);
    String sessionId = session.getId();
    System.out.println("getdetailView:" + detailView + ": " + sessionId );

    Return detailView;
}

public void setDetailView(final boolean detailView) {
    FacesContext context = FacesContext.getCurrentInstance();
    HttpSession session = (HttpSession) context.getExternalContext().getSession(false);
    String sessionId = session.getId();
    System.out.println("setdetailView:" + detailView + ": " + sessionId );
    this.detailView = detailView;
}

在jsf中,该值被称为如下所示:

代码语言:javascript
运行
复制
<p:inputText rendered="#{sessionBean.detailView} value="bla"/>

看起来,getter和setter被绑定到不同的数据源,但这怎么可能呢?

任何帮助都是非常感谢的。提前谢谢。

EN

回答 1

Stack Overflow用户

发布于 2015-01-21 16:35:26

我在后台bean中没有看到任何用于detailView的getter,您必须有一个getter:

代码语言:javascript
运行
复制
public boolean getDetailView() {
    //any required code.
    Return detailView;
}

注意到,您的后台bean被@SessionScoped注释了,所以如果会话结束后无法访问字段,那么它是逻辑的,如果您希望它在应用程序期间是活动的,那么用@ApplicationScoped注释它。

--我在jst代码中看到了一个错误,纠正它:<p:inputText rendered="#{sessionBean.detailView}" value="bla"/>,但是我不确定是否可以将EL表达式用于inputText的render属性,另一件事是尝试将代码放入<form>标记中

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28072034

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档