我想有条件地输出一些Facelets代码.
为此,JSTL标签似乎工作正常:
<c:if test="${lpc.verbose}">
...
</c:if>
Run Code Online (Sandbox Code Playgroud)
但是,我不确定这是否是最佳做法?还有另一种方法来实现我的目标吗?
这似乎不对.我正在清理我的代码,我只是注意到了这一点.每个ajax请求都会触发构造函数和@PostConstruct我的@ViewScopedbean.即使是简单的数据库分页也会触发它.
我知道这@ViewScoped比@RequestScoped任何请求都要重建并且不应该重建.只有在通过GET重新加载完整页面之后.
我再次看到@PostConstruct每次都在触发,即使没有使用绑定属性.看到这段代码: -
<?xml version='1.0' encoding='UTF-8' ?>
<!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:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:c="http://java.sun.com/jsp/jstl/core">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h:form>
<c:forEach var="item" items="#{TestBean.listItems}">
<h:outputText value="#{item}"/>
</c:forEach>
<h:commandButton value="Click" actionListener="#{TestBean.actionListener}"/>
</h:form>
</h:body>
</html>
Run Code Online (Sandbox Code Playgroud)
这是JSF中最简单的bean: -
package managedBeans;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
@ManagedBean(name="TestBean")
@ViewScoped
public class TestBean implements Serializable {
private List<String> listItems;
public List<String> getListItems() {
return listItems;
}
public void setListItems(List<String> listItems) {
this.listItems = listItems;
} …Run Code Online (Sandbox Code Playgroud) 我对<ui:repeat>标签有一个奇怪的问题.即使对于我非常简单的示例,嵌套重复组件中的值绑定也无法按预期工作.
我有一个像这样的简单小脸:
<h:body>
<h:form>
<ui:repeat value="#{sandbox.rows}" var="row">
<ui:repeat value="#{row.columns}" var="column">
<h:outputText value="#{column.value}" />
<h:selectBooleanCheckbox value="#{column.value}" />
</ui:repeat>
<br/>
</ui:repeat>
<h:commandButton action="#{sandbox.refresh}" value="Refresh" />
</h:form>
</h:body>
Run Code Online (Sandbox Code Playgroud)
和沙盒类:
@Component
@Scope("request")
public class Sandbox {
public static class Row {
private List<Column> columns = Arrays.asList(new Column(), new Column());
public List<Column> getColumns() {
return columns;
}
}
public static class Column {
private boolean value;
public void setValue(boolean value) {
this.value = value;
}
public boolean getValue() {
return this.value;
}
} …Run Code Online (Sandbox Code Playgroud)