Sie sind auf Seite 1von 7

Top of Form

/w EPDw UKMjA1M
/w EWBw Kco5nJ

Getting started with Struts 2.0


In this blog, I show how to get started with Struts 2.0 with a simple application. I use NetBeans IDE 6.5 for this and Struts 2.0 plugin.

What is Struts 2.0?


Struts 2 is the next version of Struts 1.x Unfortunately there are not many things that carried forward from Struts 1.x to Struts 2.0. A small application is the best way to understand the differences. I am writing this blog keeping on my mind readers who have already worked with Struts 1.x. However, Struts 2 depends more on Intercepting Filter pattern replacing Front controller (remember ActionServlet is based on Front Controller design pattern).

Getting started with Struts 2.0


In order to use Struts 2.0 in NetBeans IDE 6.5 follow the steps given below:

Go to Struts 2.0 support page for NetBeans at Struts 2 for NetBeans IDE. Download both the files org-netbeans-modules-web-frameworks-struts2.nbm and org-netbeans-modules-web-frameworks-struts2lib20011.nbm. Start NetBeans 6.5. Go to Tools -> Plugins option in NetBeans. Select Downloaded tab in Plugins window Click on Add Plugins button and select two .nbm files that you have downloaded earlier. Click on Install button to install these plugins. When you are prompted to restart NetBeans IDE, do so.

You are ready to create a new web application with Struts 2 support.

Creating Web Application With Struts 2.0 support


Now, let us create a new web application that uses Struts 2. As we select Struts 2.0 support all the required libaries and configuration files are automatically added to our project.
1. Select File->New Project. 2. Select Java Web under Categories and Web application under Projects

3. Give web application name and project location as you like. 4. Select Apache Tomcat as Sever. However, you can select any server you like.
5. In Frameworks step, select Struts2 as the framework to use. 6. Click on Finish.

NetBeans creates a project with required files. Though not all of them are required for now, you need not disturb them. Let us focus on files that we have to create for our application.

Struts2 is based on Filters. The following web.xml shows how filter FilterDispatcher is configured by default. Netbeans IDE automatically places these entries when a new web application is created with Struts2 support.
<filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filterclass> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>

Creating Interest Calculation application


We will create necessary files to take Principal Amount and Interest rate and from user and then display interest amount as the result of the process.

interest.jsp
The first thing we want to create a .jsp file to take input from user. Following code displays two textboxes to take input from user. Label attribute is used for prompt message. Attribute name specifies the property in the action class that is associated with the input field. So first textfield copies values into amount property and second textfield copies value into rate property. Another important attribute is action attribute of form tag.
<%@page contentType="text/html" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <body> <h2>Interest Calculation </h2> <s:form action="interest"> <s:textfield name="amount" label="Principal Amount "/> <s:textfield name="rate" label="Interest Rate "/> <p/> <s:submit value="Calculate"/> </s:form> </body> </html>

InterestAction.java
Now we need to create action class to receive the data from input elements and do the required process. It contains execute() method, but it has no parameters (like in previous version). Create required properties - amount, rate and result. Property result is used to provide result to result.jsp (yet to create it). Place InterestAction class in action package as follows.
package action;

public class InterestAction { private double amount, rate, result; public double getResult() { return result; } public double getAmount() { return amount; } public void setAmount(double amount) { this.amount = amount; } public double getRate() { return rate; } public void setRate(double rate) { this.rate = rate; } public InterestAction() { } public String execute() throws Exception { result = amount * rate / 100; return "success"; } }

struts.xml
Modify struts.xml, which is in default package under source pacakges. In Struts 2, xml file providing details of action class is placed in classpath. Action tag specifies the name of the class, action name and also where to go based on the value returned by execute() method of InterestAction class. In this case we want to go to result.jsp when execute() method returns success. Create a package that extends struts-default package so that all the required functionality is inherited. Place action tag in package tag as follows shown in the following code.
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="default" extends="struts-default"> <action name="interest" class="action.InterestAction"> <result name="success">/result.jsp</result> </action>

</package> </struts>

result.jsp
Result.jsp is used to display the result after business logic is done. Business logic is placed in execute() method of InterestAction class. Using property tag we retrieve value from result property of InterestAction class.
<%@page contentType="text/html" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <body> <h2>Interest </body> </html>

= <s:property value="result" /> </h2>

That's all you have to do. Now deploy the web application and run interest.jsp. You will see two textboxes with prompts on the left. Enter amount and interest rate then click on Calculate button. You should see interest displayed in result.jsp page.
gs_struts2

Home

Blogs

Post Your Comment

Enter the code given in the above image : Your Name : Your Email Address : Comment :

Please enter four digits! Please enter code given in the image! Name Is Required!

Comment Is Required!

Post Comment

Comments
Posted By srinivasu On 30-Nov-09 12:38:41 PM I am one of the regular user of this website.This url is really good to under the basic stuff of struts 2.0. Sir, if possile could you please provide a list of points, of basic differences between 1.x and 2.0 versions in terms of framework features. thx.

Posted By madhusudhan On 30-Nov-09 03:54:31 PM sir, i am one of your student sir .. on comparing to the struts 1, here there no representation of actionform with the validate method, without this how we will validate the input values .. and also tell me sir how the input values(form) are submitted to the getter- setter methods

Posted By madhusudhan On 30-Nov-09 04:51:57 PM sir ,, now i got the new project on springs sir,, i would i approach sir... actually some part is of struts

Posted By madhusudhan On 02-Dec-09 11:42:33 AM sir, im using netbeans 6.7.1 struts2 plugin is not processing

Posted By Zubin Shah On 11-Dec-09 05:05:53 PM It really helps me a lot....Thanks

Posted By visitor On 09-Feb-10 05:34:10 PM Madhusudhan ! Download the new plug-in from https://nbstruts2support.dev.java.net version 1.2 for orgnetbeans-modules-web-frameworks-struts2.nbm and org-netbeans-modules-web-frameworksstruts2lib20011.nbm. it will work!! Thanks... Visitor of this site!! Add 2003 to my email id (reducing spam)

Posted By Kishore On 10-Apr-10 01:07:54 PM

This Example is really good and very easy to understand for a beginner.

Posted By sakthivel On 28-May-10 04:05:30 PM dear sir iam working in one it company we are using netbeans 6.7,here i can download struts2.1 org-netbeans-modules-web-frameworks-struts2.nbm and org-netbeans-modules-webframeworks-struts2lib20011.nbm. but it will not work in netbeans6.7,so please help for me

Posted By Pirabu G On 03-Jun-10 04:27:28 PM thankyou. it is very useful.

Posted By Anjali On 24-Jul-10 10:45:29 AM Hello sir, I am using netbeans 6.9. i dono how to run struts in this ide.i searching in net .but i wont get the proper knowledge. please help me sir.

Posted By anonymous On 06-Aug-10 11:05:17 PM Great Article! I am a newbie with struts framework and I was wondering if you can place the struts2 configuration, properties, *-validation.xml files etc out of the WEB-INF directory and some other directory of my choice? Will really appreciate any response. Thanks.

Posted By Vishal S. Pawar On 21-May-11 11:10:37 PM Hello Sir, I am a regular viewer of your site, The examples which you have gives are very useful and simple to clear doubts.Would you please add some more examples related to the database transactions. Thanks,

Posted By Venkat On 22-Jul-11 06:53:54 PM Excellent. Very nicely explained the basics of struts 2.0 . Thankyou.

Bottom of Form

Copyright Srikanth Technologies. All rights reserved.

Das könnte Ihnen auch gefallen