How to save the data in Java Server faces??

Posted on 2005-05-14
Last Modified: 2013-11-24
Hi All,

How to save the data in Java Server faces??

Question by:sudhakar_koundinya
    LVL 21

    Expert Comment

    How do you mean "save the data".   Do you want to save the data to a database or are you just having problems saving the data on the session.
    LVL 14

    Author Comment

    in both ways

    LVL 21

    Accepted Solution


    Lets start with saving the data to the session.  When you set up your bean in the faces-config.xml, you specify the scope of the bean:
      page     - The bean stays in memory ONLY for the JSP page where it is referenced
      request -  The bean stays in memory for multple JSP pages on the same request
      session -  The bean stays in memory for the duratio of the user's session.

    When a JSP accesses a bean, it examines the configured scope and stores it in the appropriate memory.  My recommendation for your first JSF application, is to store everything on the session initially.  So set the <managed-bean-scope> in the faces-config.xml to 'session'.
    The managed bean is referenced inside your form in the custom inputfield tags:
      <h:inputText value="#{dataBean.dataValue}"/>
    refers to the dataValue Property  in the dataBean and sets its value appropriately

    As for saving, when your beans to the database, it is usually as a result of a user click on the OK or SAVE button.  In your JSP you use the custom tag on the SAVE button.  Something like this:
      <h:commandButton value="Save" action="#{dataBean.saveData}"/>
       <!--The SAVE button invokes this 'controler' bean when it is clicked-->

    In the faces-config.xml you have this bean configuation:

    Then the dataControler class handles the save event:
    public class dataBean

       public String getValue() {...}
       public void setValue(String val) {...}

       public String saveData()
            String[] values={this.getValue()};
            try {
              int rows=TransactionMgr.getInstance().executeQuery("Insert into MyTable values(?)", values);
              return "dataSaved"
            } catch (SQLException e)
                  log.error("Save transaction failed!", e);
                  return "dataSaveFailed"

    In the faces-config.xml, you have a navigation rule that controls the action taken when the SAVE button is clicked:
      <from-view-id>/enterData.jsp</from-view-id>                   <!--JSP page where data entry form is displayed-->
         <from-outcome>dataSaved</from-outcome>                 <!--Action invoked when saveData() method succeeds-->
         <to-view-id>/WEB-INF/results/result1.jsp</to-view-id>   <!--Page to navigate to when save is complete!-->
         <from-outcome>dataSaveFailed</from-outcome>            <!--Action invoked when saveData() method fails!-->
         <to-view-id>/WEB-INF/results/DBError.jsp</to-view-id>   <!--Page to navigate to when save is complete!-->


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
    I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
    Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
    This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now