P. 1
struts2.3.4 Hello World Example

struts2.3.4 Hello World Example

|Views: 17|Likes:
Published by atul randev
Plenty of struts2 hello world examples are out there, but when i tried to find one that worked a little here or there was missing. I've tried to put together as complete an example as possible. please do suggest improvements or corrections to the post.

In case you face any issues while implementing this do leave a message over here or contact me on facebook (the name is pretty unique), and i'd try to resolve it as best as i can. tc
Plenty of struts2 hello world examples are out there, but when i tried to find one that worked a little here or there was missing. I've tried to put together as complete an example as possible. please do suggest improvements or corrections to the post.

In case you face any issues while implementing this do leave a message over here or contact me on facebook (the name is pretty unique), and i'd try to resolve it as best as i can. tc

More info:

Published by: atul randev on Aug 14, 2012
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOCX, PDF, TXT or read online from Scribd
See more
See less

05/13/2014

pdf

text

original

Basic Struts2 'Hello World' Application

A basic struts2 application can be made in 7 simple steps. There are umpteen struts2 tutorials on the web so I've refrained from going in a lot of detail about the theoretical information, this article will help you implement the most basic struts2 application in eclipse. I've tried to explain all the steps adequately, providing screenshots and code hints wherever required. It is possible that you may be familiar with a few of the initial steps, but for the benefit of those that maybe the slightest bit confused I've not edited them out. I hope that you find this article to be of use. Please do let me know if you face any issues while implementing this, I’d love to help you out, additionally, if you believe that some part of it could be implemented in a better manner I’d love to hear of it. Hope this helps.

Step 1 - Creating a web Project
Right click on the project explorer and click on New. Select 'Dynamic Web Project' from the side-slide menu that appears.

Enter the project name 'Struts2Project' and click on finish.

Step 2 - Add the required libraries
The following jars should be added to the lib folder in WEB-INF.

1. commons-io-2.0.1.jar ---------> http://www.versioneye.com/package/commons-io--commonsio/version/2~1

2. commons-lang3-3.1.jar --------> http://www.versioneye.com/package/org~apache~commons-commons-lang3/version/0

3. commons-logging-1.1.1.jar -----> http://www.versioneye.com/package/commons-logging-commons-logging/version/0

4. -2.3.19.jar ----------> http://www.versioneye.com/package/org~freemarker--freemarker 5. ognl-3.0.5.jar -----------------> http://www.versioneye.com/package/ognl--ognl/version/0 6. struts2-core-2.3.4.jar
core/version/2~3~4

---------> http://www.versioneye.com/package/org~apache~struts--struts2-

7. xwork-core-2.3.4.jar ----------> http://www.versioneye.com/package/org~apache~struts~xwork-xwork-core/version/0

While the exact use of some of these jars may not be evident at first sight, there are many interlinked dependencies which makes it important to include each of the 7 jars, explaining the dependencies would have been out of scope of the post so I skipped the part.

Step 3 - Edit Web.xml
You would find the web.xml in WEB-INF folder in Web Content folder. Filter definition and filter mapping needs to be given in the file. Paste the following code in web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>Struts2Project</display-name> <filter> <filter-name>struts2</filter-name> <filterclass>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</ filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>Index.jsp</welcome-file> </welcome-file-list> </web-app>

Step 4 - Create Index.jsp
Copy and paste the following code into the jsp. Note the second line of the code; you would need to include the tag library which you are going to use in the code. Struts2 tags are slightly different as opposed to your tradational HTML codes, hence the use of <s:submit> instead
of the good old <input type="submit">. You can research this further on the web. <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@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> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Home</title> </head> <body> <s:form action="Login.action"> <h2>Username : <s:textfield id="username" name = "username"></s:textfield></h2> <s:submit value="Submit"></s:submit> </s:form> </body> </html>

The action attribute mentioned with the form is the keyword which is going to help the application redirect to your action class.

Step 5 - Configuring struts.xml
Create a new xml file in the source folder of the project.

Paste the following code into the new file. Once you click on submit on the Index.jsp, struts.xml sends you over to the mentioned class to the given method. If you do not give a method name in the action tag in struts.xml, the execute ( ) method is called automatically.
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="default" extends="struts-default" namespace="/"> <action name="Login" class="com.struts2.action.Login" method="validation"> <result name="success">Success.jsp</result> </action> </package> </struts>

Step 6 - Creating the Action class
Create a new class Login.java in the package com.struts2.action. This is where all the action is (pun intended). As mentioned earlier, the control would be passed over to the mentioned method validation() in Login.java .
package com.struts2.action; import com.opensymphony.xwork2.ActionSupport; public class Login extends ActionSupport{ /** * */ private static final long serialVersionUID = -6105380673356535743L; String username; public String validation(){ setUsername( username); return SUCCESS; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } }

Step 7 - The result page
We are almost done now. Create the result page Success.jsp in webcontent. This is the page mentioned in the result tag in struts.xml
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@taglib uri="/struts-tags" prefix="s" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Success</title> </head> <body> <h2>welcome <s:property value="#attr.username"/></h2> </body> </html>

And we are done!!!!!
Your struts2 application is now ready for deployment. There are a couple of questions I've come across while creating this sample project. One of them being why do we add the exact jars that we add, as in, each and every jar file that we add to our project is essential, why though? I'd try to answer that in a following post. Additionally, I came across some changes I had to make while implementing the project in struts2.3.4 as opposed to struts2.1; I wanted to list them out in the beginning of this post but in an attempt to keep it short and trying not to venture too far off the mark I edited them out. That and the rest deserve a small pot of their own and soon one shall follow. Do let me know in case something does not work out for you over here, I'd love to help you out. You can get in touch with me over here, or on facebook (the name shouldn't be hard to find.) Let me take the opportunity to thank you for reading this, please feel free to suggest improvements or innovations.

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->