【问题标题】:Creating dynamic inputs in jsf2在 jsf2 中创建动态输入
【发布时间】:2013-05-18 11:07:04
【问题描述】:

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

【问题讨论】:

    标签: jsf-2 richfaces


    【解决方案1】:

    对于您的示例,

    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() {
    }
    }
    

    员工类(你可以使用你的实体类)

    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 组件进行高级操作

    【讨论】:

    • 这会在按钮点击时创建另一行输入文本
    • 是的,它正在添加带有 inputText 的新行
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-12
    • 2012-11-19
    • 1970-01-01
    • 2013-02-27
    • 2014-08-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多