我在jsf。
中的HtmlPaneGrid中创建动态inputText时遇到问题1)我有一个用户选择否的DropDown。主题 2)基于dropDown中的Selected值,我必须在PanelGrid中创建inputText。 3)所以在dropDown的ajax更改事件中,我创建HtmlInputText并将其添加到htmlPanelGrid并更新HtmlPanelGrid 4)此HtmlPanelGrid绑定到名为Subject的辅助bean。
当我选择任何一个值时(比如说:3),最初在htmlPanelGrid中没有显示任何文本框。 当我选择另一个值(例如:2)时,Previous值为3(因此显示3个TextBox) 当我选择另一个值(例如:4)时,Previous值为2(因此显示2个TextBox)
当我选择2,2文本框时应该显示 3表示应显示3个文本框,依此类推。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"htth://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
xmlns:c="http://java.sun.com/jsp/jstl/core">
<h:head></h:head>
<ui:composition template="/templates/common/layout.xhtml">
<ui:define name="content">
<h:body>
<h:form>
<p:panel header="Add Subject">
<p:growl id="subjGrowl" showDetail="true"></p:growl>
<f:view>
<p:panelGrid>
<f:facet name="header">
<p:row>
<p:column>Degree</p:column>
<p:column>Department</p:column>
<p:column>Academic Session From</p:column>
<p:column>Academic Session To</p:column>
<p:column>Semester</p:column>
<p:column>Number Of Subjects</p:column>
</p:row>
</f:facet>
<p:row>
<p:column>
<p:selectOneMenu id="degree"
value="#{subject.entDegree.degreeId}">
<f:selectItem itemLabel="Please Select" itemValue="-1"></f:selectItem>
<f:selectItems value="#{subject.degreeMap}"></f:selectItems>
<p:ajax update="department"
listener="#{subject.handleDegreeChange}"></p:ajax>
</p:selectOneMenu>
</p:column>
<p:column>
<p:selectOneMenu id="department"
value="#{subject.entDepartment.deptId}">
<f:selectItem itemLabel="Please Select" itemValue="-1"></f:selectItem>
<f:selectItems value="#{subject.departmentMap}"></f:selectItems>
</p:selectOneMenu>
</p:column>
<p:column>
<p:inputText value="#{subject.entSubject.academicSessionFrom}"
required="true"
requiredMessage="Academic Session From Required" maxlength="4">
<f:convertNumber integerOnly="true"></f:convertNumber>
</p:inputText>
</p:column>
<p:column>
<p:inputText value="#{subject.entSubject.academicSessionTo}"
required="true" requiredMessage="Academic Session To Required"
maxlength="4">
<f:convertNumber integerOnly="true"></f:convertNumber>
</p:inputText>
</p:column>
<p:column>
<p:inputText value="#{subject.entSubject.semester}"
required="true" requiredMessage="Semester Required"
maxlength="44"></p:inputText>
</p:column>
<p:column>
**<p:selectOneMenu id="noOfSubj" value="#{subject.noOfSubj}">
<f:selectItems value="#{subject.noOfSubjMap}"></f:selectItems>
<p:ajax update="subjPanelGridId" listener="#{subject.updateSubjGrid}"></p:ajax>
</p:selectOneMenu>**
</p:column>
</p:row>
</p:panelGrid>
**<h:panelGrid id="subjPanelGridId"
binding="#{subject.subjPanelGrid}">
</h:panelGrid>**
<f:facet name="footer">
<p:commandButton value="Save" action="#{subject.seeSubjList}"
update="subjGrowl"></p:commandButton>
</f:facet>
</f:view>
</p:panel>
</h:form>
</h:body>
</ui:define>
</ui:composition>
</html>
支持Bean: 请参阅标记为粗体或**
的代码package com.oxford.web.college;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.el.ValueExpression;
import javax.faces.application.Application;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.component.html.HtmlInputText;
import javax.faces.component.html.HtmlPanelGrid;
import javax.faces.context.FacesContext;
import com.oxford.biz.college.SesnCollegeRemote;
import com.oxford.biz.collegesubjects.SesnSubjectRemote;
import com.oxford.eea.ejb.exception.SystemException;
import com.oxford.entity.campus.EntCampus;
import com.oxford.entity.college.EntDegree;
import com.oxford.entity.college.EntDepartment;
import com.oxford.entity.collegesubjects.EntSubject;
import com.oxford.web.util.DisplayMessage;
@ManagedBean
@SessionScoped
public class Subject implements Serializable {
private static final long serialVersionUID = 1L;
Logger logger = Logger.getLogger(Subject.class.getName());
@ManagedProperty("#{error}")
private ResourceBundle error;
@ManagedProperty("#{message}")
private ResourceBundle message;
@ManagedProperty(value = "#{collegeDepartment}")
private CollegeDepartment collegeDepartment;
private EntCampus entCampus;
private EntDegree entDegree;
private EntDepartment entDepartment;
private EntSubject entSubject;
private static Map<String, Object> degreeMap;
private static Map<String, Object> departmentMap;
private static Map<Integer, Integer> noOfSubjMap;
private Integer noOfSubj;
@EJB
private SesnSubjectRemote sesnSubjectRemote;
@EJB
private SesnCollegeRemote sesnCollegeRemote;
private List<EntDepartment> listOfDepartment;
private List<EntDegree> listOfDegree;
private HtmlPanelGrid subjPanelGrid;
private Collection gridCollection;
String errorMsg = null;
String successMsg = null;
static {
degreeMap = new LinkedHashMap<String, Object>();
}
public Subject() {
entDegree = new EntDegree();
entDepartment = new EntDepartment();
entSubject = new EntSubject();
noOfSubjMap = new LinkedHashMap<Integer, Integer>();
for(Integer i=1;i<12;i++){
noOfSubjMap.put(i, i);
}
}
@PostConstruct
public void init() {
loadDegree();
}
public static ValueExpression createValueExpression(String valueExpression,
Class<?> valueType) {
FacesContext context = FacesContext.getCurrentInstance();
return context
.getApplication()
.getExpressionFactory()
.createValueExpression(context.getELContext(), valueExpression,
valueType);
}
public void loadDegree() {
try {
listOfDegree = sesnCollegeRemote.getDegreeList(getEntCampus()
.getCampusId());
for (EntDegree entDeg : listOfDegree) {
degreeMap.put(entDeg.getDegreeName(), entDeg.getDegreeId());
}
} catch (SystemException sysex) {
errorMsg = error.getString(sysex.getMessage());
logger.log(Level.SEVERE, errorMsg);
DisplayMessage.errorMessageUI(errorMsg);
} catch (Exception ex) {
}
}
public void seeSubjList(){
List subjects = subjPanelGrid.getChildren();
ArrayList<String> subjList = new ArrayList<String>();
boolean flag = false;
String val = null;
outer:
for(int i=0;i<subjects.size();i++){
HtmlInputText htmlText = (HtmlInputText)subjects.get(i);
val = (String)htmlText.getValue();
if(subjList!=null && subjList.size()>0){
for(int j=0;j<subjList.size();j++){
String subject = subjList.get(j);
if(subject.equalsIgnoreCase(val)){
flag = true;
break outer;
}
}
if(flag==false){
subjList.add(val);
}
}else{
subjList.add(val);
}
}
System.out.println(subjList);
if(flag){
errorMsg = "The Subject '"+val+"' Should not repeat more than once";
DisplayMessage.errorMessageUI(errorMsg);
}
}
public void handleDegreeChange() {
try {
departmentMap = new LinkedHashMap<String, Object>();
listOfDepartment = sesnSubjectRemote.getDepartmentList(
entDegree.getDegreeId(), getEntCampus().getCampusId());
for (EntDepartment entDept : listOfDepartment) {
departmentMap.put(entDept.getDeptName(), entDept.getDeptId());
}
} catch (SystemException sysex) {
errorMsg = error.getString(sysex.getMessage());
logger.log(Level.SEVERE, errorMsg);
DisplayMessage.errorMessageUI(errorMsg);
} catch (Exception ex) {
}
}
**public void updateSubjGrid(){
System.out.println("Inside UpdateSubjGrid method");
Application app = FacesContext.getCurrentInstance().getApplication();
gridCollection = new ArrayList();
for(int i=0;i<getNoOfSubj();i++){
HtmlInputText subjectText = (HtmlInputText) app.createComponent(HtmlInputText.COMPONENT_TYPE);
subjPanelGrid = (HtmlPanelGrid) app.createComponent(HtmlPanelGrid.COMPONENT_TYPE);
subjPanelGrid.setId("subjGridId-"+i);
subjectText.setId("subj-" + i);
subjectText.setValueExpression(
"subjName",
createValueExpression("#{subject.entSubject.subjName}",
String.class));
gridCollection.add(subjectText);
}
subjPanelGrid.getChildren().addAll(gridCollection);
System.out.println("cc::"+subjPanelGrid.getChildCount());
}**
public Map<String, Object> getDegreeMap() {
return degreeMap;
}
public Map<String, Object> getDepartmentMap() {
return departmentMap;
}
public EntDepartment getEntDepartment() {
return entDepartment;
}
public void setEntDepartment(EntDepartment entDepartment) {
this.entDepartment = entDepartment;
}
public EntDegree getEntDegree() {
return entDegree;
}
public void setEntDegree(EntDegree entDegree) {
this.entDegree = entDegree;
}
public void setCollegeDepartment(CollegeDepartment collegeDepartment) {
this.collegeDepartment = collegeDepartment;
}
public EntCampus getEntCampus() {
if (collegeDepartment != null) {
entCampus = collegeDepartment.getEntCampus();
}
return entCampus;
}
public ResourceBundle getError() {
return error;
}
public void setError(ResourceBundle error) {
this.error = error;
}
public ResourceBundle getMessage() {
return message;
}
public void setMessage(ResourceBundle message) {
this.message = message;
}
public EntSubject getEntSubject() {
return entSubject;
}
public void setEntSubject(EntSubject entSubject) {
this.entSubject = entSubject;
}
public HtmlPanelGrid getSubjPanelGrid() {
return subjPanelGrid;
}
public void setSubjPanelGrid(HtmlPanelGrid subjPanelGrid) {
this.subjPanelGrid = subjPanelGrid;
}
public Integer getNoOfSubj() {
return noOfSubj;
}
public void setNoOfSubj(Integer noOfSubj) {
this.noOfSubj = noOfSubj;
}
public Map<Integer, Integer> getNoOfSubjMap() {
return noOfSubjMap;
}
}