问题
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