Creating dynamic inputs in jsf2

大城市里の小女人 提交于 2019-12-08 11:48:18

问题


i want to create dynamic textbox in jsf2 in datatable having textboxes based on clicking add row button. Iam a newbie for jsf programming.Can someone tell me a basic example for dynamic for generation . I have read ui:repeatand c:foreach but i need some practical example.


回答1:


For your sample,

xhtml code:

<?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:f="http://java.sun.com/jsf/core">
<h:head>
</h:head>
<h:body>
    <h:form  id="form1">
        <h:commandButton value="Add new Row" actionListener="#{myBean.addNewEmployee}"/>
        <h:dataTable value="#{myBean.employeeList}"  var="emp">
            <h:column>
                <f:facet name="header">
                    Employee Id
                </f:facet>
                #{emp.empId}
            </h:column>
            <h:column>
                <h:inputText value="#{emp.empName}"/>
            </h:column>
        </h:dataTable>
    </h:form>
</h:body>
</html>

jsf manged bean

@ManagedBean
@ViewScoped
public class MyBean implements Serializable {

List<Employee> employeeList;

public List<Employee> getEmployeeList() {
    return employeeList;
}

public void setEmployeeList(List<Employee> employeeList) {
    this.employeeList = employeeList;
}

public void addNewEmployee(ActionEvent event) {
    employeeList.add(new Employee(employeeList.size(), null));
}

@PostConstruct
public void init() {
    employeeList = new ArrayList<Employee>();
    employeeList.add(new Employee(1, "Emp1"));
    employeeList.add(new Employee(2, "Emp2"));
}

public MyBean() {
}
}

Employee Class(You can use your entity class)

public class Employee {

Integer empId;
String empName;

public Integer getEmpId() {
    return empId;
}

public void setEmpId(Integer empId) {
    this.empId = empId;
}

public String getEmpName() {
    return empName;
}

public void setEmpName(String empName) {
    this.empName = empName;
}

public Employee(Integer empId, String empName) {
    this.empId = empId;
    this.empName = empName;
}

@Override
public boolean equals(Object obj) {
    if (obj == null) {
        return false;
    }
    if (getClass() != obj.getClass()) {
        return false;
    }
    final Employee other = (Employee) obj;
    if (this.empId != other.empId && (this.empId == null || !this.empId.equals(other.empId))) {
        return false;
    }
    return true;
}

@Override
public int hashCode() {
    int hash = 5;
    hash = 41 * hash + (this.empId != null ? this.empId.hashCode() : 0);
    return hash;
}
 }

You can directly use above code. And also I suggest to use PrimeFaces component for advanced manipulations



来源:https://stackoverflow.com/questions/16623563/creating-dynamic-inputs-in-jsf2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!