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.

1 Answer 1

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:

Sign up to request clarification or add additional context in comments.

5 Comments

Can anyone please tell me the sql query for storing multiple rows in database in above situation?
That has nothing to do with JSF. Press Ask Question. Hint: INSERT INTO ... VALUES ....
Can we not achieve it by using a managed bean with session scoped?
You can. It has only the disadvantage that opening the page in multiple browser tabs in the same session can have undesired side effects.
The datatable in my answer has input components, so it's editable already.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.