Adding multiple rows in database table from one JSF page in one go

﹥>﹥吖頭↗ 提交于 2020-01-15 12:09:13

问题


Can we add multiple rows in the database table from one JSF page in one go?

Actually i want to make an attendance page, i want to take attendance of all employees in one JSF page in one go that is when user will press save button then all employees attendance will get save in database.

Is it possible to achieve this using managed bean and JSF page? Kindly give me some idea that how it can be achieved?

I've done insert,view,update and delete for single row.


回答1:


Yes, just use a <h:dataTable>. Imagine that you've a bean like this

@ManagedBean
@ViewScoped
public class EmployeeManager {

    private List<Employee> employees;

    @EJB
    private EmployeeService employeeService;

    @PostConstruct
    public void init() {
        employees = new ArrayList<Employee>();
        employees.add(new Employee());
        employees.add(new Employee());
        employees.add(new Employee());
        // ...
        // Do whatever you find necessary. Maybe offer an `Add row` button?
    }

    public void save() {
        employeeService.save(employees);
        // ...
    }

    // ...
}

then you can present it as follows

<h:form>
    <h:dataTable value="#{employeeManager.employees}" var="employee">
        <h:column><h:inputText value="#{employee.firstname}" /></h:column>
        <h:column><h:inputText value="#{employee.lastname}" /></h:column>
        <h:column><h:inputText value="#{employee.email}" /></h:column>
    </h:dataTable>
    <h:commandButton value="Save" action="#{employeeManager.save}" />
</h:form>

See also:

  • The benefits and pitfalls of @ViewScoped - contains a CRUD example


来源:https://stackoverflow.com/questions/6049847/adding-multiple-rows-in-database-table-from-one-jsf-page-in-one-go

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