User Registration Action Class and DAO code
In this section we will explain how to write code for action class and code for performing database operations (saving data into database).
Developing Action Class
The Action Class UserRegisterAction.java process the user registration request. It saves the user information into database with the help of "SpringHibernateDao" bean.
Here is the full code of UserRegisterAction.java:
package roseindia.web.struts.action; import roseindia.services.ServiceFinder; import roseindia.web.struts.form.UserRegisterForm; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionMessage; import org.apache.struts.action.ActionMessages; public class UserRegisterAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { UserRegisterForm objForm = (UserRegisterForm) form; //Utilities util=new Utilities(); // Retrieve the DAO Reference roseindia.dao.SpringHibernateDAO springHibernateDAO = (roseindia.dao.SpringHibernateDAO) ServiceFinder .getContext(request).getBean("SpringHibernateDao"); //By Default show the Add/Edit Page String forwardToPage = "input"; String strError = ""; try { String strParent = "0"; boolean ValidUsernameStatus = springHibernateDAO .checkValidUserName(objForm.getUserid()); //In case of form submit Add/Update the data if (objForm.getActionUpdateData().equals("update")) { //In case of Add, Add the data into database if (objForm.getAction().equals("add")) { if (ValidUsernameStatus == false) { roseindia.dao.hibernate.Login pojoObj = new roseindia.dao.hibernate.Login(); pojoObj.setLoginid(objForm.getUserid()); pojoObj.setPassword(objForm.getPassword()); pojoObj.setAddress(objForm.getAddress()); pojoObj.setPhno(Integer.parseInt(objForm.getPhno())); pojoObj.setId(objForm.getId()); pojoObj.setEmail(objForm.getEmail()); //Add the data springHibernateDAO.addUser(pojoObj); forwardToPage = "success"; } else { // Create object of ActionMesssages ActionMessages errors = new ActionMessages(); errors.add("invalidUsername", new ActionMessage( "error.invalidUsername.invalid")); saveErrors(request, errors); return mapping.findForward("input"); } }//User updates the data, update the user details if (objForm.getAction().equals("update")) { //System.out.println("Update the Data"); roseindia.dao.hibernate.Login pojoObj = springHibernateDAO .loadUser(objForm.getId().toString()); pojoObj.setLoginid(objForm.getUserid()); pojoObj.setPassword(objForm.getPassword()); pojoObj.setAddress(objForm.getAddress()); pojoObj.setPhno(Integer.parseInt(objForm.getPhno())); pojoObj.setId(objForm.getId()); pojoObj.setEmail(objForm.getEmail()); //Update the data springHibernateDAO.updateUser(pojoObj); forwardToPage = "updated"; } } //In case of Edit retrieve the data from datbase and set the values in the form obj if (objForm.getAction().equals("Edit")) { HttpSession session = request.getSession(); String id = (String)session.getAttribute("ID"); //Retrieve the data from database roseindia.dao.hibernate.Login pojoObj = springHibernateDAO.loadUser(id); objForm.setId(pojoObj.getId()); objForm.setUserid(pojoObj.getLoginid()); objForm.setPassword(pojoObj.getPassword()); objForm.setAddress(pojoObj.getAddress()); objForm.setEmail(pojoObj.getEmail()); objForm.setPhno(String.valueOf(pojoObj.getPhno())); //for the edit form forwardToPage = "input"; //Set the action to update objForm.setAction("update"); } } catch (Exception e) { forwardToPage = "input"; strError = e.getMessage(); System.out.println("===> Error:" + strError); } //Display the registration form to the user return mapping.findForward(forwardToPage); } } |
Save the above code into the file UserRegisterAction.java in the project\WEB-INF\src\java\roseindia\web\struts\action directory.
Understanding the Action Class
a) Getting the SpringHibernateDao Reference
Following code gets the reference of SpringHibernate.
//Retrieve the DAO Reference
roseindia.dao.SpringHibernateDAO springHibernateDAO = (roseindia.dao.SpringHibernateDAO) ServiceFinder
.getContext(request).getBean("SpringHibernateDao");
b) Checking for the New user Registration
Following code checks for the user restoration information. This is necessay as we using the same action class (UserRegistrationAction.java) for modification of user profile.
if (objForm.getAction().equals("add")) {
//The Save the user information into database
}
c) Creation and populating the POJO object
We are using Hibernate so, to persist the User information into database we will first create object of Login class and populate it with requied data.
roseindia.dao.hibernate.Login pojoObj = new roseindia.dao.hibernate.Login();
pojoObj.setLoginid(objForm.getUserid());
pojoObj.setPassword(objForm.getPassword());
pojoObj.setAddress(objForm.getAddress());
pojoObj.setPhno(Integer.parseInt(objForm.getPhno()));
pojoObj.setId(objForm.getId());
pojoObj.setEmail(objForm.getEmail());
c) Persisting the POJO object
Following code persists the data into database.
//Update the data
springHibernateDAO.updateUser(pojoObj);
Configurations to be made into struts-config.xml
a) Add the form bean entry:
<form-bean name="UserRegisterForm"
type="roseindia.web.struts.form.UserRegisterForm">
</form-bean>
b) Action mapping entry:
<action
path="/userregister"
name="UserRegisterForm"
scope="request"
validate="true"
input="/pages/user/userRegister.jsp"
type="roseindia.web.struts.action.UserRegisterAction">
<forward name="success" path="/pages/user/registersuccess.jsp"/>
<forward name="input" path="/pages/user/userRegister.jsp"/>
<forward name="updated" path="/pages/user/updatesuccess.jsp"/>
</action>
Now the registration form is ready to test. You can compile and run the application to test the Registration Form.