Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Opinion of an expert

 
Roel De Nijs
Sheriff
Posts: 10392
133
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now an expert is among us, i have to take the advantage to ask for his opinion about this. So Mr. Carnell (and others), please feel free to give your opinion...

We are just started with a new web application and it will be developed with the Jakarta Struts Framework. One of our issues at this moment is the following: we have some beans (InfoA, InfoB, InfoC,...) that will be filled with info from a database. Each bean has some data members and methods (a1, getA1, setA1, a2, ...). Because each bean contains different information, we have for each bean a jsp-file (infoA.jsp, infoB.jsp, ...). We also have an info.jsp page containing a navigation-section (with a menu.jsp containing some links to infoA, infoB, infoC,...) and a body-section (must contain the right jsp-file after clicking on a link). Functionalities: reading and updating the bean-information.

Because i have the most experience with JSF (and it is just my second JSF-project after a very small private website), I gave my collegue following advice:
  • make for each bean an ActionForm (FormA, FormB, FormC) and implement it as follows:

  • class FormX extends ActionForm {
    private InfoX info;

    public FormX() { info = new InfoX(); }
    public void setXXX(value) { info.setXXX(value); }
    public datatype getXXX() { return info.getXXX(); }
    public InfoX getInfoX() { return info; }
    public void setInfoX(InfoX i) { info = new InfoX(i); }
    }
  • in struts-config.xml

  • - for each ActionForm:
    <form-bean name="formX" type="be.test.FormX" />
    - for each DispatchAction
    <action path="/infoX" type="be.test.ActionX" name="formX" scope="request" validate="false" parameter="action" input="/jsp/info.jsp">
    <forward name="SUCCESS" path="/jsp/info.jsp" />
    <forward name="SUCCESS.SAVE" path="/jsp/save.jsp" />
    </action>
    - for each bean a global-forward
    <forward name="INFO.X" path="/kaart.do?actie=initInfoX" redirect="false" />
  • make also for each ActionForm a DispatchAction and implement it as follows:

  • class ActionX extends DispatchAction {
    public ActionForward initInfoX(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
    InfoX info = new InfoX();
    // fill object with values from database
    request.getSession().setAttribute("INFOX", info);
    request.getSession().setAttribute("PAGE", "infoX.jsp");
    return mapping.findForward("SUCCESS");
    }
    public ActionForward saveInfoX(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
    FormX formX = (FormX)form;
    InfoX info = new InfoX(formX.getInfoX());
    // save values from object into database
    return mapping.findForward("SUCCESS.SAVE");
    }
    }
  • the menu.jsp :

  • for each jsp-page make a link: <html:link forward="initInfoX">See info x</html:link>
  • design each jsp-page as follows:

  • <bean efine id="info" name="INFOX" scope="session" />

    <html:form action="/infoX">
    <html:hidden name="action" value="saveInfoX" />
    <html:text name="info" property="x1" />
    <html:text name="info" property="x2" />
    ...
    <html:submit>Save</html:submit>
    </html:form>

    I hope it is a little bit clear and understandable. Is this the way how to do it or where could it be made more effective.

    All tips, hints, suggestions, pros and cons are welcome.
     
    somkiat puisungnoen
    Ranch Hand
    Posts: 1312
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It's very good. And Do you use Struts Validation Framework or not and Why/How ?
     
    Roel De Nijs
    Sheriff
    Posts: 10392
    133
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    We don't use the Validation framework because this project is just in the first phase and that requires just reading, no updating. So input of the user is bounded to some clicks on links, images (eg. zooming in on a map,...), so he can't do anything wrong in my opinion.
    But in second or third phase user can give information and when it's validated, it will be saved in a database and then we'll use the validation framework, because it has a lot of pros: e.g. a lot of built-in validation-rules (so you can use your time to impliment business specific rules), it's quite easy to implement (did it once with our struts-training and once for the private website i made), ...
     
    John Carnell
    Author
    Ranch Hand
    Posts: 71
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Roel,

    I don't consider myself an expert. I am constantly humbled by my inability to get even the simple things right.

    Overall the advice you gave looks good. I only had a couple of questions/comments:

    1. For each action, you should probably make the forward private to the
    action rather then a global forward. Global forwards are more for
    when you have multiple actions all directing to the same place.

    2. If you are doing mostly navigation with basic data entry you might
    want to consider using DynaActionForms with the Validator framework.

    If you do this approach, you will significantly reduce the amount of code you need to write. Later on if you need to write more concrete ActionForm's and plug them into the code with minimal effort.

    Thanks,
    John
     
    Roel De Nijs
    Sheriff
    Posts: 10392
    133
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    John,

    thanks for your reply.

    First, i want to clarify some things: with a private forward you do mean defining an forward between <action> and </action> or is a private forward something else ?

    Secondly, when i assume a private forward is something between <action> and </action> i don't know how i can use a html:rewrite with a private forward. I tought a global-forward must be used when a forward is used without a certain form/action. And this is the case of <html:link forward="...">Click here</html:link> because struts framework can't know in which action-mapping he has to look for the forward. (This was explanation of our consultant)

    And if i want to switch to an DynaActionForm, which changes do i have to make on this example (one i know for sure and that's changing the extend in DynaActionForm instead of ActionForm ):
    class FormA extends ActionForm {
    private InfoA info;

    public FormA() { info = new InfoA(); }
    public void setA1(value) { info.setA1(value); }
    public datatype getA1() { return info.getA1(); }
    public void setA2(value) { info.setA2(value); }
    public datatype getA2() { return info.getA2(); }
    public InfoA getInfoA() { return info; }
    public void setInfoA(InfoA a) { info = new InfoA(a); }
    }

    Thanks.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic