Archive

Archive for the ‘Struts’ Category

What is Struts….in a nut nutshell.

August 30, 2011 Leave a comment

Thanks to Mark Gorokhov and Mahesh Lavannis for this explanation…

Well Struts is nothing but a mapping layer… think about what happens when one goes to a URL?

Our URL in this case is http://www.foo.com/blah.do?param1=a&param2=b&param3=c.

Now because blah.do is not a request for a regular HTML page, our Webserver does not know how to render it…. but it knows that it is supposed to forward the HTTP GET request to the Application Server.

Our application server gets this request and it in turn looks up in the struts.xml file for a mapping between blah.do and an “action” class. The struts.xml could have lines as below:



<URL>blah.do</URL>
<action>BlahClass</action>
<form>BlahParameterClass</form>
<Forward name="some_return_string">some_view.jsp</Forward>
<Forward name="some_other_return_string">some_other_view.jsp</Forward>


So the application server now instantiates BlahClass; but how do we now pass those parameters to it? Well this is where the <form> comes into play. This tag defines a class that is something as follows:



public class BlahParameterClass {
String param1;
String param2;
String param3;


public String getParam1() {
return param1;
}


public void setParam1(String param1) {
this.param1 = param1;
}


public String getParam2() {
return param2;
}


public void setParam2(String param2) {
this.param2 = param2;
}


public String getParam3() {
return param3;
}


public void setParam3(String param3) {
this.param3 = param3;
}
}


So as we can see, this code is nothing special. It is merely a set of properties with their associated accessors. And since struts is aware of both of these classes, they are beans as well. (Remember a class becomes a bean when the framework is aware of it's existence and works directly with it). OK so when the struts framework instantiates the BlahParameterClass and sets the values of its parameters from the URL. The BlahParameterClass is then passed to BlahClass with the HTTPRequest and HTTPResponse objects. This BlahClass implements the execute () method as mandated by the struts JSR and the application server merely execute's the method. Internally it does whatever it has to and in its return code, it returns a string lets say some_string…. this string is mapped to the forward tag and then the appropriate jsp code / templating engine code (viz Velocity, Template::Toolkit etc) is called. It get's converted to HTML and returned to the Web Server. The web server then returns this information in a HTTP RESPONSE packet back to the client. Hooray you now know the base of struts… This is struts 1.0.

The real struts.xml file is as follows:



<action name="peersForm" path="/peers" scope="request" type="packageName.PeersAction"
validate="false" parameter="state">
<forward name="success" path="/peers.jsp"/>
</action>


  • The path maps to the URL.
  • The name maps to the action tag.
  • The type maps to the form tag.

Now what if instead of returning control to a form, we need to call another class to do its job, well; then we need to create parameters within the original class before forwarding the request to the other class. This is bad to say the least but this is struts 2.0.

Enjoy.....

Categories: Programming, Struts