• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 226
  • Last Modified:

How to save the data in Java Server faces??

Hi All,

How to save the data in Java Server faces??

Thanks
Sudhakar
0
sudhakar_koundinya
Asked:
sudhakar_koundinya
  • 2
1 Solution
 
MogalManicCommented:
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.
0
 
sudhakar_koundinyaAuthor Commented:
in both ways

Regards
Sudhakar
0
 
MogalManicCommented:
OK,

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:
<managed-bean>
  <managed-bean-name>dataControler</managed-bean-name>
  <managed-bean-class>com.MyCompanyName.BeanControlers.dataBean</managed-bean-class>
  <managed-bean-scope>request</managed-bean-scope>
</managed-bean>

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:
<navigation-rule>
  <from-view-id>/enterData.jsp</from-view-id>                   <!--JSP page where data entry form is displayed-->
  <navigation-case>
     <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!-->
   </navigation-case>
  <navigation-case>
     <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!-->
   </navigation-case>
</navigation-rule>

0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now