在jsf2中创建动态输入 [英] Creating dynamic inputs in jsf2

查看:67
本文介绍了在jsf2中创建动态输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在jsf2的数据表中创建动态文本框,该表具有基于单击添加行按钮的文本框.我是jsf编程的新手.有人可以告诉我有关动态生成的基本示例.我已经读过ui:repeatc:foreach,但是我需要一些实际的例子.

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.

推荐答案

为您的示例,

xhtml代码:

<?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托管豆

@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;
}
 }

您可以直接使用上面的代码.而且我建议使用PrimeFaces组件进行高级操作

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

这篇关于在jsf2中创建动态输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆