tags:

views:

122

answers:

1

I want to modify my jsp page after submission with some additional readonly fields. I have used iterators and if conditions. I am working in struts2. How to reset my save page after saving?

public String savePerson(){
        Session hSession = ActionManager.getHibernateSession();
        profileObj.setProfileId(pid);
        Date doBirth = DateFunctions.toDate(dob);
        profileObj.setDob(doBirth);

        try{

            pid = ProfileDAO.getInstance().addProfils(hSession, profileObj);
            addressObj.setProfileId(pid);
            AddressDAO.getInstance().addAddress(hSession, addressObj);
            setMessage("Succesfully saved.");

            if (pid > 0){
                hSession.clear();
                resetProfile(pid);
//i want to include my jsp page with hidden fields additionally.
                }

        } catch (HibernateException hExc) {
            setMessage("Unable to save.");
            return ERROR;
        } finally {
            ActionManager.closeHibernateSession();
        }
        return SUCCESS;
        }
+1  A: 

So a few things. First to answer your question. Generally, after a save you do a redirect to different page to prevent the POST from being processed twice should the user click "Refresh" on the page after they save. In your case, I would redirect to a different method, perhaps show() where you load the Profile from id and then display the JSP page. The redirect URL would be something like "show?pid=${profileObj.profileId}.

You have a model object called profile. You don't need separate member variables in your action to "catch" the request parameters. You can just name your field in the JSP page profileObj.profileId and assuming you have a setProfileObj(Profile profileObj) in your action, Struts2 will create your Profile object for you and populate the right properties based on matching request Parameters. It will even do this for dates! (You can customize how Struts2 converts dates by creating and registering your own date converter).

So you POST these parameters and Struts creates your model with the values populated. You use your DAO an save Profile and then redirect like I mentioned in the first paragraph.

You also have some mixing of concerns in your Action. If you are going to use the DAO pattern, then you should really have all of your persistence code (Hibernate Sesssion, etc) encapsulated there.

Dusty Pearce