Hi, I think I have some problem on understanding certain concept in
struts, and wondering if anyone can help me.  Basically I have a jsp
page, an action and an actionform, which load the data from the
database, and display on the jsp.  There is a part which use the
<display:*> tags, which display a table.  I have trouble keeping the
data inside the form to persist, and if I click on the sorting link on
the UserGroup table, I lost the request, and therefore the
UserDetailForm instance is lost, and the page report error because it
cannot find any data (as you can see, I stop the page from loading the
data again and resetting my input in the form).  I am very unsure if my
structure is correct, please point as many bad things as possible, this
would be a great help.  I have included a series of code here for
reference.  Although my code does not work properly, but I feel that
there are things that could be of great help to some people (e.g.
displaying hashmap easily), and I would like to share these and possible
help others.  Thanks

################################ UserDetail.jsp
##############################################

<!�Xsome tag lib ��
<html:form action="/UserDetail">
<html:hidden property = "IUserId" />
Username: <html:text property = "SUserName"/><BR>
Created Date: <bean:write name="UserDetailForm" property="DCreatedDate"
scope="request"/> <BR>
Email: <html:text property = "SEmail"/><BR>
Password: <html:password property = "SPassword"/><BR>
�K  some other fields
<html:hidden property="forward" value="error"/>
<html:submit onclick="set('update');">UPDATE</html:submit>
<html:submit onclick="set('delete');">DELETE</html:submit>
�K.some other forwards
<hr>GROUPS<hr>
<display:table width="60%" name="UserDetailForm" property="hmUserGroups"
scope="request">
  <display:column property="key" title="Name" sort="true"/>
  <display:column property="value" title="value" />
</display:table>
</html:form>

</body>
<Script language="JavaScript">
function set(target) {
     document.forms[0].forward.value=target;
}
</Script>
</html>

################################ UserDetailForm
##############################################

public class UserDetailAction extends Action{    
    public ActionForward perform(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) {
        //load Detail form data
        UserDetailForm udf = (UserDetailForm)form;
        HashMap hmUserGroups = new HashMap();

        DaoManager daoManager = (DaoManager)
servlet.getServletContext().getAttribute(Constants.DATABASE_KEY  );
        UserFacade fd = new UserFacade();
        fd.Init(daoManager);
        
        //create a user object for storing current user data
        User user = new User();
        try {
            user = fd.getUser(Integer.parseInt(udf.getIUserId()));  
            hmUserGroups = fd.getUserGroups(user.getIUserId());
            udf.setHmUserGroups(hmUserGroups);
        }
        catch (Exception e) {
            System.out.println(e);
        }

        //only reset all the user detail if UserId =null from the
UserDetail_b.jsp (User Detail is not loaded yet)
        //or if forward is reset
        String forwardRequest="";
        if(request.getParameter("forward")!=null)
            forwardRequest = request.getParameter("forward");
        else
            forwardRequest = "requestNull";
        
        if(udf.getIUserId() == null ||
forwardRequest.equals("requestNull") || forwardRequest.equals("reset"))
{
            udf.setDCreatedDate(Util.Date2String(user.getDCreatedDate(),
"1970-01-01"));
            udf.setSEmail(user.getSEmail());
            udf.setSPassword(user.getSPassword());
            udf.setSUserName(user.getSUserName());
        }

        request.setAttribute("currentUser", user);              
        //check if forward is null (first time load)
        if(request.getParameter("forward")!=null)
            return
(mapping.findForward(request.getParameter("forward")));
        else
            //first time load, load all data
            return (mapping.findForward("reset"));
    }
}

################################ UserDetailForm
##############################################
public class UserDetailForm extends ActionForm{
    
    private String sEmail;
    private String sUserName;
    private String sPassword;
    private String dCreatedDate;
    private HashMap hmUserGroups;

    public String getDCreatedDate() {
        return dCreatedDate;
     }
    public void setDCreatedDate(String dCreatedDate) {
        this.dCreatedDate = dCreatedDate;
    }
    
    public java.lang.String getSEmail() {
        return sEmail;
    }
    
    public void setSEmail(java.lang.String sEmail) {
        this.sEmail = sEmail;
    }
    
    public java.lang.String getSPassword() {
        return sPassword;
    }
    
    setSPassword(java.lang.String sPassword) {
        this.sPassword = sPassword;
    }
    
    public java.lang.String getSUserName() {
        return sUserName;
    }
    
    public void setSUserName(java.lang.String sUserName) {
        this.sUserName = sUserName;
    }
    
    public String getIDeletedUser() {
        return convertUserId(iDeletedUser);
    }
    
    /** Setter for property iDeletedUser.
     * @param iDeletedUser New value of property iDeletedUser.
     *
     */
    public void setIDeletedUser(String iDeletedUser) {
        this.iDeletedUser = iDeletedUser;
    }
    
    public HashMap getHmUserGroups() {
        return hmUserGroups;
    }
    
    public void setHmUserGroups(HashMap hmUserGroups) {
        this.hmUserGroups = hmUserGroups;
    }
    
    public String convertUserId(String sUserId) {
        if(sUserId.equals("0"))
            return "<Not Available>";
        else {
            HashMap hm =
(HashMap)servlet.getServletContext().getAttribute("hmUser");
            if(!hm.containsKey(new Integer(Integer.parseInt(sUserId))))
                return "<Invalid User ID>";
            else
                return (String)hm.get(new
Integer(Integer.parseInt(sUserId)));
        }
    }
}

################################################### struts-config.xml
##############################################################
<form-bean name="UserDetailForm"
type="com.apps.crm.web.form.UserDetailForm" />

  <action path="/UserDetail"
type="com.apps.crm.web.actions.UserDetailAction" name="UserDetailForm"
validate="false" input="UserDetail" scope="request" unknown="false">
  <forward name="cancel" redirect="false" path="/UserList.do" />
  <forward name="update" path="/UserUpdate.do" redirect="false" />
  <forward name="delete" path="/UserDelete.do" redirect="false" />
  <forward name="reset" path="/UserDetail.jsp" redirect="false" />
  </action>



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to