File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes Opinion of an expert Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Opinion of an expert" Watch "Opinion of an expert" New topic

Opinion of an expert

Roel De Nijs

Joined: Jul 19, 2004
Posts: 8368

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" />
    - for each bean a global-forward
    <forward name="INFO.X" path="/" 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" />

    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.

    SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
    OCAJP 7
    somkiat puisungnoen
    Ranch Hand

    Joined: Jul 04, 2003
    Posts: 1312
    It's very good. And Do you use Struts Validation Framework or not and Why/How ?

    Java Developer, Thailand
    Roel De Nijs

    Joined: Jul 19, 2004
    Posts: 8368

    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
    Ranch Hand

    Joined: Sep 27, 2002
    Posts: 71
    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.


    John Carnell<br />Principal Architect<br /> <br />Netchange, LLC<br />1161 HillCrest Heights<br />Green Bay, WI 54313<br /> <br /><br /> <br /> <br />Author of <a href="" target="_blank" rel="nofollow">Pro Jakarta Struts, Second Edition</a>
    Roel De Nijs

    Joined: Jul 19, 2004
    Posts: 8368


    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); }

    I agree. Here's the link:
    subject: Opinion of an expert
    It's not a secret anymore!