Seam javaBean using JSF binding attribute at <a4j:repeat>?
jack.cdyuan.gmail.com Jun 12, 2009 5:16 AMmy javabean
import java.util.List;
import java.util.Set;
import javax.faces.component.html.HtmlInputText;
import javax.faces.component.html.HtmlSelectOneMenu;
import javax.faces.context.FacesContext;
import org.ajax4jsf.component.UIRepeat;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Begin;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.web.RequestParameter;
import org.jboss.seam.faces.FacesMessages;
import org.jboss.seam.framework.EntityHome;
import com.baijisoft.travel.entity.CommonCode;
@Name("commonCodeHome")
@Scope(ScopeType.CONVERSATION)
public class commonCodeHome extends EntityHome<CommonCode>
{
@RequestParameter Long commonCodeId;
private UIRepeat repeater;
private HtmlInputText nameRef;
private HtmlSelectOneMenu typeRef;
@In FacesMessages facesMessages;
public void change(){
nameRef.processValidators(FacesContext.getCurrentInstance());
nameRef.processUpdates(FacesContext.getCurrentInstance());
typeRef.processValidators(FacesContext.getCurrentInstance());
typeRef.processUpdates(FacesContext.getCurrentInstance());
}
@Override
public Object getId()
{
if (commonCodeId == null)
{
return super.getId();
}
else
{
return commonCodeId;
}
}
public void checkname() {
if(this.getEntityManager().contains(this.instance))
this.getEntityManager().clear();
List exists = this.getEntityManager().createQuery("select cc from CommonCode cc where cc.name=:name and cc.type=:type")
.setParameter("name", this.instance.getName()).setParameter("type", this.instance.getType())
.getResultList();
if (exists.size()>0) {
facesMessages.addFromResourceBundle("myuser_username_exist", this.getInstance().getName());
if (this.instance.getId()==null)
this.instance.setName("");
else {
this.instance.setName(this.getEntityManager().find(CommonCode.class, this.instance.getId()).getName());
}
}
}
@Override @Begin
public void create() {
super.create();
}
public void addSub() {
Set<CommonCode> subs = this.instance.getSubs();
subs.add(new CommonCode());
this.instance.setSubs(subs);
}
public void removeSub() {
Set<CommonCode> subs = this.instance.getSubs();
subs.remove(this.repeater.getRowData());
this.instance.setSubs(subs);
}
public UIRepeat getRepeater() {
return repeater;
}
public void setRepeater(UIRepeat repeater) {
this.repeater = repeater;
}
public HtmlInputText getNameRef() {
return nameRef;
}
public void setNameRef(HtmlInputText nameRef) {
this.nameRef = nameRef;
}
public HtmlSelectOneMenu getTypeRef() {
return typeRef;
}
public void setTypeRef(HtmlSelectOneMenu typeRef) {
this.typeRef = typeRef;
}
}
my xhtml
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:s="http://jboss.com/products/seam/taglib"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:rich="http://richfaces.org/rich"
xmlns:a4j="http://richfaces.org/a4j"
template="/layout/template.xhtml">
<ui:define name="body">
<h:form id="ccForm">
<rich:panel>
<f:facet name="header">#{tagVar.comnon_code_title}</f:facet>
<s:decorate id="nameField" template="/layout/edit.xhtml">
<ui:define name="label">#{tagVar.common_code_name}</ui:define>
<a4j:region>
<h:inputText id="name" required="true" value="#{commonCodeHome.instance.name}" style="width:155px">
<f:validateLength minimum="2" maximum="50"/>
<a4j:support actionListener="#{myUserHome.checkname}" event="onchange" reRender="name"/>
</h:inputText>
</a4j:region>
</s:decorate>
<s:decorate id="typeField" template="/layout/edit.xhtml">
<ui:define name="label">#{tagVar.common_code_type}</ui:define>
<h:selectOneMenu value="#{commonCodeHome.instance.type}" id="type">
<f:selectItem itemLabel="province" itemValue="province"/>
<f:selectItem itemLabel="city" itemValue="city"/>
</h:selectOneMenu>
</s:decorate>
<fieldset class="fieldset">
<legend class="legend">#{tagVar.common_code_sub}</legend>
<a4j:outputPanel id="table">
<table>
<tr>
<td align="center"><h:outputText value="#{tagVar.common_code_name}" /></td>
<td align="center"><h:outputText value="#{tagVar.common_code_type}" /></td>
<td align="center"><a4j:commandButton value="#{tagVar.new}" immediate="true" action="#{commonCodeHome.addSub}" reRender="table"
onclick="this.disabled=true" oncomplete="this.disabled=false"/></td>
<td></td>
</tr>
<a4j:repeat id="repeat" value="#{commonCodeHome.instance.subs}" var="sub" binding="#{commonCodeHome.repeater}">
<tr>
<td align="center">
<h:inputText id="subname" value="#{sub.name}" binding="#{commonCodeHome.nameRef}">
<f:validateLength maximum="20"></f:validateLength>
<a4j:support immediate="true" action="#{commonCodeHome.change}"
event="onchange"/>
</h:inputText>
</td>
<td>
<h:selectOneMenu value="#{sub.type}" id="subtype" binding="#{commonCodeHome.typeRef}">
<f:selectItem itemLabel="province" itemValue="province"/>
<f:selectItem itemLabel="city" itemValue="city"/>
<a4j:support immediate="true" action="#{commonCodeHome.change}"
event="onchange"/>
</h:selectOneMenu>
</td>
<td>
<a4j:commandButton value="#{tagVar.delete}" immediate="true" action="#{commonCodeHome.removeSub}" reRender="table"
onclick="this.disabled=true" oncomplete="this.disabled=false"/>
</td>
<td>
<rich:message for="subname"/>
<rich:message for="subtype"/>
</td>
</tr>
</a4j:repeat>
</table>
</a4j:outputPanel>
</fieldset>
<div style="clear:both"/>
</rich:panel>
<div class="actionButtons">
<h:commandButton id="save"
value="#{tagVar.submit_save}"
action="#{commonCodeHome.doPersist}"
rendered="#{!commonCodeHome.managed}"/>
<h:commandButton id="update"
value="#{tagVar.submit_update}"
action="#{commonCodeHome.doUpdate}"
rendered="#{commonCodeHome.managed}"/>
<s:button id="delete"
value="#{tagVar.submit_delete}"
action="#{commonCodeHome.doRemove}"
propagation="end"
immediate="true"
view="/my/commonCodeList.xhtml"
rendered="#{commonCodeHome.managed}" />
<s:button propagation="end"
id="cancel"
value="#{tagVar.submit_cancel}"
view="/my/commonCodeList.xhtml"/>
</div>
</h:form>
</ui:define>
</ui:composition>
When I accessed that xhtml page, I got exceptions:
Exception during request processing:
Caused by javax.servlet.ServletException with message: "/my/commonCode.xhtml @48,122 binding="#{commonCodeHome.repeater}": Target Unreachable, identifier 'commonCodeHome' resolved to null"
What did I do wrong?
Please help me!Thinks!