假设我像这样指定一个outputText组件:
<h:outputText value="#{ManagedBean.someProperty}"/>
Run Code Online (Sandbox Code Playgroud)
如果我在someProperty调用getter for时打印一条日志消息并加载页面,那么注意每个请求多次调用getter是很容易的(在我的情况下发生了两次或三次):
DEBUG 2010-01-18 23:31:40,104 (ManagedBean.java:13) - Getting some property
DEBUG 2010-01-18 23:31:40,104 (ManagedBean.java:13) - Getting some property
Run Code Online (Sandbox Code Playgroud)
如果someProperty计算的值很昂贵,这可能是一个问题.
我用Google搜索了一下,认为这是一个已知问题.一个解决方法是包括一个检查,看看它是否已经计算过:
private String someProperty;
public String getSomeProperty() {
if (this.someProperty == null) {
this.someProperty = this.calculatePropertyValue();
}
return this.someProperty;
}
Run Code Online (Sandbox Code Playgroud)
这个问题的主要问题是你得到大量的样板代码,更不用说你可能不需要的私有变量了.
这种方法有哪些替代方案?没有那么多不必要的代码,有没有办法实现这一目标?有没有办法阻止JSF以这种方式行事?
感谢您的输入!
我正在使用Eclipse和Glassfish 3.0.虽然我以前做过类似的事情,但对这项技术来说还是很新 非常简单,确实有一个绑定到支持bean的数据表.添加方法并删除我已经介绍过的方法 - 问题在于我调用的更新方法.我似乎无法看到组件中拾取的更改(HtmlInputText)从不介意将数据传递回表.
我的数据表代码如下(和jsf页面)
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<f:loadBundle basename="resources.application" var="msg"/>
<head>
<title><h:outputText value="#{msg.welcomeTitle}" /></title>
</head>
<body>
<h:form id="mainform">
<h:dataTable var="row" border="0" value="#{beanCategory.collection}" binding="#{beanCategory.datatable}">
<f:facet name="header">
<h:outputText value="Categories"/>
</f:facet>
<h:column>
<f:facet name="header">
<h:outputText value="Description"/>
</f:facet>
<h:inputText id="input1" value="#{row.description}" valueChangeListener="#{row.inputChanged}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Id"/>
</f:facet>
<h:outputText id="id" value="#{row.id}"/>
</h:column>
<h:column>
<h:commandButton value="Delete" type="submit" action="#{beanCategory.remove}">
<f:setPropertyActionListener target="#{beanCategory.selectedcategory}" value="#{row}"/>
</h:commandButton>
<h:commandButton value="Save" action="#{beanCategory.update}"
>
<f:setPropertyActionListener
target="#{beanCategory.selectedcategory}" value="#{row}" />
</h:commandButton>
</h:column>
</h:dataTable>
<h:inputText id="text1"></h:inputText> <h:commandButton action="#{beanCategory.addCategory}" value="Add" type="submit" id="submitbutton"> …Run Code Online (Sandbox Code Playgroud)