1

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.

Adnan
  • 4,517
  • 15
  • 44
  • 54

1 Answers1

3

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:

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Can anyone please tell me the sql query for storing multiple rows in database in above situation? – Adnan May 19 '11 at 01:27
  • 1
    That has nothing to do with JSF. Press `Ask Question`. Hint: `INSERT INTO ... VALUES ...`. – BalusC May 19 '11 at 01:59
  • Can we not achieve it by using a managed bean with session scoped? – Adnan May 19 '11 at 04:57
  • You can. It has only the disadvantage that opening the page in multiple browser tabs in the same session can have undesired side effects. – BalusC May 19 '11 at 11:42
  • The datatable in my answer has input components, so it's editable already. – BalusC May 20 '11 at 11:30