<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Free practice test , mock test, driving test, interview questions &#187; struts2</title>
	<atom:link href="http://www.skill-guru.com/blog/tag/struts2/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.skill-guru.com/blog</link>
	<description>Find free mock and practice test, create and sell tests</description>
	<lastBuildDate>Mon, 06 Sep 2010 19:14:28 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Action classes in Struts</title>
		<link>http://www.skill-guru.com/blog/2009/11/16/action-classes-in-struts/</link>
		<comments>http://www.skill-guru.com/blog/2009/11/16/action-classes-in-struts/#comments</comments>
		<pubDate>Mon, 16 Nov 2009 13:05:48 +0000</pubDate>
		<dc:creator>smitha</dc:creator>
				<category><![CDATA[Programming / tutorials]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[struts2]]></category>

		<guid isPermaLink="false">http://www.skill-guru.com/blog/?p=1045</guid>
		<description><![CDATA[In this post we will explore the features of Struts 2 action class and its capabilities.
Struts2 Actions are the most important and main part of struts2 Framework. They work as Model of MVC framework. Following article gives you more deep idea about Action classes  role, configuration etc.
Action class Role:
1. Acts as Model encapsulates the business Logic: [...]]]></description>
			<content:encoded><![CDATA[<p>In this post we will explore the features of Struts 2 action class and its capabilities.</p>
<p>Struts2 Actions are the most important and main part of struts2 Framework. They work as Model of MVC framework. Following article gives you more deep idea about Action classes  role, configuration etc.</p>
<p><strong>Action class Role:</strong></p>
<p>1. Acts as Model encapsulates the business Logic: The main responsibility of Action class is to include business logic. The business logic will be encapsulated in execute method of the Action class. <span id="more-1045"></span><br />
2. The class helps the controller in result routing: The action class returns String, which helps to determine the view to be loaded. Example if struts.xml has following action definition<br />
&lt;action name=&#8221;Calculate&#8221;&gt;<br />
&lt;result name=&#8221;SUCCESS&#8221;&gt;/page1.jsp&lt;/result&gt;<br />
&lt;result name=&#8221;ERROR&#8221;&gt;/page2.jsp&lt;/result&gt;<br />
&lt;/action&gt;<br />
If the execute() method (of example.Calulator class) returns String “SUCCESS” then the page1.jsp will be chosen by controller to render and if execute() method returns “ERROR”, then page2.jsp will be chosen.<br />
This behaviour is bit different than Struts 1. Struts 1 action classes used specify the routing information using ActionForward objects. This new approach of struts2 has made struts action classes  much simpler and clean.<br />
3. Action class acts as data carrier in Struts 2. This is a different behaviour and major difference from struts 1. Struts 1 Action classes were singleton, where one single instance of action class serves all the requests. The ActionBean carries data.<br />
The struts2 action classes get instantiated per request. The ActionBeans have disappeared. The Action classes itself acts as data carrier. This approach again makes the action classes much cleaner and reusable.</p>
<p>Another major difference is the Action class execute method signature. The exculte method depends upon the servlet API. But the struts 2 action classes are simpler like any other POJO.  The unit testing of Action classes have become easier as they dont rely on the HttpRequest and HttpResponse objects.</p>
<p>Next section will describe how the Action classes and be configured and their packaging.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.skill-guru.com/blog/2009/11/16/action-classes-in-struts/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Struts 2 &#8211; Hands on Tutorial</title>
		<link>http://www.skill-guru.com/blog/2009/11/15/struts-2-hands-on-tutorial/</link>
		<comments>http://www.skill-guru.com/blog/2009/11/15/struts-2-hands-on-tutorial/#comments</comments>
		<pubDate>Sun, 15 Nov 2009 15:33:31 +0000</pubDate>
		<dc:creator>smitha</dc:creator>
				<category><![CDATA[Programming / tutorials]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[struts2]]></category>

		<guid isPermaLink="false">http://www.skill-guru.com/blog/?p=1041</guid>
		<description><![CDATA[Struts 2 Practical Tutorial:
Following tutorial demonstrates you a simple  struts2 application and explains you how things work together in struts2. The tutorial demonstrates sruts2 using a calculator application which calculates different operation results and displays them.
The tutorial uses Eclipse3  IDE -  or above.
Step 1:  Download struts2
Download struts2-blank-xxx.war from  :   http://struts.apache.org/download.cgi
Step 2:  create a project in eclipse 
Create a [...]]]></description>
			<content:encoded><![CDATA[<p><strong><span style="text-decoration: underline;">Struts 2 Practical Tutorial:</span></strong></p>
<p>Following tutorial demonstrates you a simple  struts2 application and explains you how things work together in struts2. The tutorial demonstrates sruts2 using a calculator application which calculates different operation results and displays them.</p>
<p>The tutorial uses Eclipse3  IDE -  or above.</p>
<p><span style="text-decoration: underline;">Step 1:  Download struts2</span></p>
<p>Download struts2-blank-xxx.war from  :   <a href="http://struts.apache.org/download.cgi">http://struts.apache.org/download.cgi</a></p>
<p><span style="text-decoration: underline;">Step 2:  create a project in eclipse </span></p>
<p>Create a new Web project in eclipse. (To create webProject click File Menu on top&#8211;&gt; select New &#8211;&gt; Select Web Project ).<br />
Once you create the application will have src folder and a WebRoot folder. <span id="more-1041"></span></p>
<p><span style="text-decoration: underline;">Step 3:  copy jars</span><br />
Copy jars from downloaded struts 2-blank/EB-INF/lib to WebRoot/WEB-INF/lib directory. You can see the copied jars in the references jars once you refresh the project after copying.</p>
<p><span style="text-decoration: underline;">Step 4:  web.xml setup</span></p>
<p>Struts2 is based on MVC (Model – View – Controller) architecture. All requests must go through a central controller. This controller decides what action should be taken and then generates the view and sends back to the client.  The controller in struts 2 is filter called ‘FilterDispatcher’ . The Controller reads struts.xml file to map a url request to an Action. We will discuss the struts.xml later.</p>
<p>To make all the calls go through he controller, we have to configure it in web application’s deployment descriptor. i.e web.xml file.<br />
We need to configure this in the web.xml as below:</p>
<blockquote><p>&lt;?xml version=&#8221;1.0&#8243; encoding=&#8221;UTF-8&#8243;?&gt;<br />
&lt;web-app id=&#8221;WebApp_9&#8243; version=&#8221;2.4&#8243; xmlns=&#8221;<a href="http://java.sun.com/xml/ns/j2ee">http://java.sun.com/xml/ns/j2ee</a>&#8221; xmlns:xsi=&#8221;<a href="http://www.w3.org/2001/XMLSchema-instance">http://www.w3.org/2001/XMLSchema-instance</a>&#8221; xsi:schemaLocation=&#8221;<a href="http://java.sun.com/xml/ns/j2ee">http://java.sun.com/xml/ns/j2ee</a> <a href="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd</a>&#8220;&gt;</p>
<p>&lt;display-name&gt;Test&lt;/display-name&gt;</p>
<p>&lt;filter&gt;<br />
&lt;filter-name&gt;struts2&lt;/filter-name&gt;<br />
&lt;filter-class&gt;org.apache.struts2.dispatcher.FilterDispatcher&lt;/filter-class&gt;<br />
&lt;/filter&gt;</p>
<p>&lt;filter-mapping&gt;<br />
&lt;filter-name&gt;struts2&lt;/filter-name&gt;<br />
&lt;url-pattern&gt;/*&lt;/url-pattern&gt;<br />
&lt;/filter-mapping&gt;</p>
<p>&lt;welcome-file-list&gt;<br />
&lt;welcome-file&gt;index.html&lt;/welcome-file&gt;<br />
&lt;/welcome-file-list&gt;</p>
<p>&lt;/web-app&gt;</p></blockquote>
<p>This FilterDispatcher acts as controller here. All the requests to any url of this application get redirected to this filter.</p>
<p>Now we have setup struts 2 framework. We now start developing calculator components.</p>
<p><span style="text-decoration: underline;">Step 5:  The Action Class</span></p>
<p>Action class has execute() method, where the action for the request takes place. This method gets called when there is any request came for this action. All the request data gets copied to the action class using getter methods before calling execute method. In the example, we have param1 and param2 parameters, which will be copied to the action class using the setter methods exposed. In action class’s execute method, one can concentrate on business logic leaving beind codes like retrieving request parameters, validation etc.<br />
Once the business logic is done the Action classes execute method returns a String. This String is used by controller to determine which view to be displayed. The view will be picked up. The data needed by the view (jsp page) will be retrieved from the action class using setter methods<br />
The action class is a simple bean, which calculates the results of the input values. Create a package example and create a class Calculator with following contents:</p>
<blockquote><p>package example;</p>
<p>public class Calculator {<br />
private int param1;<br />
private int param2;<br />
private int result1;<br />
private int result2;<br />
private int result3;<br />
private int result4;</p>
<p>public String execute() {<br />
if(param1&gt;0 &amp;&amp; param2 &gt;0){<br />
result1 = param1 + param2;</p>
<p>if(param2&gt; param1)<br />
result2 = param2 &#8211; param1;<br />
else<br />
result2 = param1 &#8211; param2;</p>
<p>result3 = param1 * param2;</p>
<p>result4 = param1 / param2;<br />
}<br />
return &#8220;result&#8221;;<br />
}</p>
<p>public int getParam1() {return param1; }<br />
public void setParam1(int param1) { this.param1 = param1;}<br />
public int getParam2() {return param2; }<br />
public void setParam2(int param2) { this.param2 = param2; }<br />
public int getResult1() { return result1; }<br />
public void setResult1(int result1) {this.result1 = result1; }<br />
public int getResult2() { return result2; }<br />
public void setResult2(int result2) {this.result2 = result2; }<br />
public int getResult3() { return result3; }<br />
public void setResult3(int result3) {this.result3 = result3; }<br />
public int getResult4() {return result4; }<br />
public void setResult4(int result4) { this.result4 = result4; }<br />
}</p></blockquote>
<p><span style="text-decoration: underline;">Step 7: The view</span></p>
<p>The Jsp files:<br />
Create a file called Calculator.jsp and copy following contents:</p>
<blockquote><p>&lt;%@ page contentType=&#8221;text/html; charset=UTF-8&#8243; %&gt;<br />
&lt;%@ taglib prefix=&#8221;s&#8221; uri=&#8221;/struts-tags&#8221; %&gt;<br />
&lt;html&gt;<br />
&lt;head&gt;<br />
&lt;title&gt;Name Collector&lt;/title&gt;<br />
&lt;/head&gt;<br />
&lt;body&gt;<br />
&lt;h4&gt;Enter 2 Numbers&lt;/h4&gt;<br />
&lt;s:form action=&#8221;Calculate&#8221;&gt;<br />
&lt;s:textfield name=&#8221;param1&#8243; label=&#8221;Number 1&#8243;/&gt;<br />
&lt;s:textfield name=&#8221;param2&#8243; label=&#8221;Number 2&#8243;/&gt;<br />
&lt;s:submit/&gt;<br />
&lt;/s:form&gt;<br />
&lt;h4&gt;Sum = &lt;s:property value=&#8221;result1&#8243;/&gt;&lt;/h4&gt; &lt;br /&gt;<br />
&lt;h4&gt;Diffence = &lt;s:property value=&#8221;result2&#8243;/&gt;&lt;/h4&gt; &lt;br /&gt;<br />
&lt;h4&gt;param 1 X param 2 = &lt;s:property value=&#8221;result3&#8243;/&gt;&lt;/h4&gt; &lt;br /&gt;<br />
&lt;h4&gt;param 1 / param 2 = &lt;s:property value=&#8221;result4&#8243;/&gt;&lt;/h4&gt; &lt;br /&gt;<br />
&lt;/body&gt;<br />
&lt;/html&gt;</p></blockquote>
<p>The view fetches result1, result2, result3 and result4 properties from the action class targeted using the url. . In View we can access and set Action data using an expression language called OGNL.</p>
<p>When submit button is clicked, the Calculate.action will be called.( &lt;s:form action=&#8221;Calculate&#8221;&gt; )</p>
<p><span style="text-decoration: underline;">Step 6:  The struts.xml file</span></p>
<p>This is the main configuration file where we define which url request should be mapped to what Action. Controller uses this file to determine the Action and the resulting view to be displayed.<br />
Create a file named struts.xml in src folder and copy following contents.</p>
<blockquote><p>&lt;?xml version=&#8221;1.0&#8243; encoding=&#8221;UTF-8&#8243; ?&gt;<br />
&lt;!DOCTYPE struts PUBLIC<br />
&#8220;-//Apache Software Foundation//DTD Struts Configuration 2.0//EN&#8221;<br />
&#8220;<a href="http://struts.apache.org/dtds/struts-2.0.dtd">http://struts.apache.org/dtds/struts-2.0.dtd</a>&#8220;&gt;</p>
<p>&lt;struts&gt;</p>
<p>&lt;constant name=&#8221;struts.enable.DynamicMethodInvocation&#8221; value=&#8221;false&#8221; /&gt;<br />
&lt;constant name=&#8221;struts.devMode&#8221; value=&#8221;false&#8221; /&gt;</p>
<p>&lt;package name=&#8221;default&#8221; namespace=&#8221;/&#8221; extends=&#8221;struts-default&#8221;&gt;<br />
&lt;action name=&#8221;Calculator&#8221;&gt;<br />
&lt;result&gt;/Calculator.jsp&lt;/result&gt;<br />
&lt;/action&gt;<br />
&lt;action name=&#8221;Calculate&#8221;&gt;</p>
<p>&lt;result name=&#8221;result&#8221;&gt;/Calculator.jsp&lt;/result&gt;<br />
&lt;/action&gt;<br />
&lt;/package&gt;<br />
&lt;!&#8211; Add packages here &#8211;&gt;</p>
<p>&lt;/struts&gt;</p></blockquote>
<p>With above setup, any request to <a href="http://server-name:port/application_name/Calculator.action">http://server-name:port/application_name/Calculator.action</a><br />
Will load the Calculator.jsp file with empty values.<br />
Any request to Calculate.action will call execute() method onresult&#8221;&gt;)</p>
<p>Now access <a href="http://localhost/test/Calculator.action">http://localhost/test/Calculator.action</a>, You can enter 2 numbers and get results.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.skill-guru.com/blog/2009/11/15/struts-2-hands-on-tutorial/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Struts 2 New Features</title>
		<link>http://www.skill-guru.com/blog/2009/10/29/struts-2-new-features/</link>
		<comments>http://www.skill-guru.com/blog/2009/10/29/struts-2-new-features/#comments</comments>
		<pubDate>Thu, 29 Oct 2009 18:11:44 +0000</pubDate>
		<dc:creator>smitha</dc:creator>
				<category><![CDATA[Programming / tutorials]]></category>
		<category><![CDATA[struts2]]></category>

		<guid isPermaLink="false">http://www.skill-guru.com/blog/?p=902</guid>
		<description><![CDATA[       Recently for a  training I started learning struts2.  As I had worked on struts one few years back I started comparing both struts-2 and 1. I found struts2 very easier than struts1.  
Though struts 1 was very popular, comparing it with the newer frameworks, which has come into market in last 2 years, struts [...]]]></description>
			<content:encoded><![CDATA[<p>       Recently for a  training I started learning struts2.  As I had worked on struts one few years back I started comparing both struts-2 and 1. I found struts2 very easier than struts1.  </p>
<p>Though struts 1 was very popular, comparing it with the newer frameworks, which has come into market in last 2 years, struts 1 has some problems.</p>
<ul>
<li> ActionForms / Action classes etc – one action needed lots of coding and they were a pain</li>
<li>Also the action class unit testing was difficult.</li>
<li>Type conversions were not automatic.</li>
<li>There was no special feature for AJAX in the framework.. etc</li>
</ul>
<p><span id="more-902"></span></p>
<p>          Strut 2 has come up with new features to overcome all above problems. The strut 2.0 is an integration of Struts 1.0 with Webwork. The basic changes in struts 2 are struts 2 Action classes and interceptors. In struts 2 a filter named ‘FilterDispatcher’ acts as controller to dispatch the request to separate action class instance.  There is no more ActionForms in Struts 2. The view will directly pull data from action class (as it happens in JSF). Beans can be used inside Action class as properties.</p>
<p> The Struts 2 framework doesn’t impose any much ways of formal requirements on the actions classes. Any class can be an action if it wants. It simply must provide an entry method for the framework to invoke when the action is executed. Struts 2 actions don’t have to implement the Action interface. Any POJO object with a execute signature can be used as a Struts 2 Action object.</p>
<p>The struts 2 actions do three things. First is encapsulating the actual work to be done for a given request. The second major role is to serve as a data carrier in the framework’s automatic transfer of data from the request to the view.  Finally, the action must assist the framework in determining which result should render the view that’ll be returned in the request response.</p>
<p>      These make ActionForms to disappear from Struts 2. The Data will be pulled from the Action classes directly making the process simpler.</p>
<p>  Another new feature in Struts 2 is introduction of interceptors. Interceptors are Struts 2 components that execute both before and after the rest of the request processing. They provide an architectural component in which to define various workflow and cross-cutting tasks so that they can be easily reused as well as separated from other architectural concerns.</p>
<p>Logging is a good example. Logging should be done with the invocation of every action, but it probably shouldn’t be put in the action itself. Because it’s not part of the action’s own unit of work. It’s more administrative, overhead if you will. Struts 2 uses interceptors to both separate and reuse these cross-cutting concerns. </p>
<p>Another difference in struts 1 and struts-2 is the threading model. Struts one Action classes were singletons (one instance of Action class serving all the requests for that action). This requires extra care in some cases. Where as in Struts-2 the action classes are instantiated per request. Each request will be served by different thread. </p>
<p>The Struts 2 action classes do not depend upon the servlet API (The execute method of the struts 1 needs HttpServletRequest and Response objects as parameter.). Hence they can be easily tested by instantiating them.</p>
<p> Few other new features of Struts 2 are</p>
<ol>
<li>Its expression language. It can still use JSTL but It also introduces a powerful expression language OGNL (Object Graph Notation Language).</li>
<li>Type conversion in Struts 1 was always a pain. But Struts2 using OGNL expression langage can automatically convert basic object types and primitives.</li>
<li>Also Struts 2 has introduced AJAX theme to make the webapplication more dynamic.</li>
<li>It also support configuration of action classes using annotations. Also it provides easy integration with Spring and Hibernate.</li>
</ol>
<p> All these new feature make struts 2 more attractive. One draw back of struts 2 is, as its bit new, the community using struts 2 is small.  But the powerful features make development in struts 2 much easier.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.skill-guru.com/blog/2009/10/29/struts-2-new-features/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
