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!

-------------Problems Reply------------

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

In the above code in mensajeAction action class you forwarded to your action response to "success" servelet which is not mapped in your struts-config.xml. As per Struts 1.0 specs it need to be mapped because for every request and response in struts the Request Processor look into struts-config.xml as it is the main Controller configuration.

hope this helps!

The actual answer is because the Struts 1.x framework was designed to run in a Servlet container (such as Apache Tomcat).

All Servlet containers follows the basic principle, that your web application must contain a web.xml file. For Struts to work, it follows the simple configuration like so:

<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
</servlet>

<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

The <url-pattern> says that all URL part ending with the .do must be picked up by the Servlet container and call a servlet whose servlet name is action. This action is your struts ActionServlet. This is the actual processor which delegates responsibility to Struts RequestProcessor and runs your Action class.

This type of architecture is called the Model View Controller pattern and that's what Struts 1.x has been designed upon.

In essence, without a <servlet> and <servlet-mapping> in your web.xml file, mapping to your Struts' ActionServlet, your application won't be executed at all.

Category:java Views:0 Time:2018-07-11
Tags: java struts 1

Related post

Copyright (C) dskims.com, All Rights Reserved.

processed in 9.320 (s). 12 q(s)