Search code examples
javastruts-1

Why do I need to open the blah.do before it is taken by the actual program?


I'm learning struts in school. I have a VERY basic hello world but there is one thing I don't seem to get right.

I have the strut form, the strut action, the main jsp(working off of the welcomeStruts.jsp), and the struts-config.xml.. which are the four files I'm working with.

Form is this:

public class mensaje extends org.apache.struts.action.ActionForm {
    private String message;
    public String getMessage() {
    return message;
    }
    public void setMessage (String message) {
        this.message = message;
    }
}

Action is this:

public class mensajeAction extends org.apache.struts.action.Action {
     @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
       HttpServletRequest request, HttpServletResponse response)
       throws Exception {
        mensaje hellwForm = (mensaje) form;
        hellwForm.setMessage("Hello World");
        return mapping.findForward("success");
      }
}

The call from the jsp is like this:

<html:form action="/mensajeAct">
    <bean:write name="mensaje" property="message"/>
</html:form>

And lastly, this is what I have in the struts-config

    <form-beans>
        <form-bean name="mensaje" type="Struts.Form.mensaje"/>
    </form-beans>
    <global-forwards>
        <forward name="welcome"  path="/Welcome.do"/>
    </global-forwards>
    <action-mappings>
        <action input="/Welcome" name="mensaje" path="/mensajeAct" scope="session" type="Struts.Action.mensajeAction" validate="false"/>
        <action path="/Welcome" forward="/welcomeStruts.jsp"/>
    </action-mappings>

NOW... if I just go to my browser (localhost:8080/helloworld), it will NOT add the "Hello World" part of the action. HOWEVER, if I first go in my browser to the .do (localhost:8080/helloworld/mensajeAct.do), and then go back to the main page (localhost:8080/helloworld) it now works. And at this point after I have visited the .do once, I can make changes in netbeans, hit play, and it takes the changes.

So, question is, am I doing something wrong in struts, or could it be GlassFish playing tricks on me? I tried IE and Firefox and it behaves the same in both.

Thanks much!


Solution

  • Your *.do should map with the path in <action-mappings>. So mensajeAct.do in the url localhost:8080/helloworld/mensajeAct.do maps with path="/mensajeAct"(<action input="/Welcome" name="mensaje" path="/mensajeAct" scope="session" type="Struts.Action.mensajeAction" validate="false"/>) and it takes you to the relevant Action class(mensajeAction in your case)

    Steps:

    1. The RequestProcessor looks up for xml block with mensajeAct.do
    2. It finds the following xml block from struts-config.xml <action input="/Welcome" name="mensaje" path="/mensajeAct" scope="session" type="Struts.Action.mensajeAction" validate="false"/>
    3. It instantiates the Action class(mensajeAction) given in your type attribute
    4. After doing necessary steps in Action class, it searches for <forward name="success" path="blah.jsp"/> in your <action-mappings> and forwards to blah.jsp