Friday, 3 January 2014

Basic Struts2 Application in Eclipse

This tutorial demonstrates how to create a basic Struts 2 Application in Eclipse using Tomcat 7.0 server (A simple login application).

Before we start with the application we need the following:



Step 1: Create a dynamic web project in Eclipse.


Step 2: Enter the project name and make sure the target runtime is set to the downloaded Apache tomcat server as shown in the below picture and click on Next. If it’s set to ‘None’, Click on ‘New Runtime’ tab and select the downloaded Apache version from the list.

Step 3: Click again on next and make sure ‘Generate web.xml deployment descriptor’ field is checked on the last page before you click on finish.


Step 4: Add the following struts2 jars (present in the downloaded struts2 -> lib folder) in the
 WebContent -> WEB-INF -> lib location of the project.


Step 5: Edit the web.xml file ( WebContent -> WEB-INF -> lib) and the following code just after the  </welcome-file-list> end tag as follows:

<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
view raw Filter hosted with ❤ by GitHub

The new web.xml should look as follows:


Step 6: Create struts.xml file in the Java Resources -> src folder of the project and copy the following code into struts.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- Configuration for the default package. -->
<package name="default" extends="struts-default">
<action name="getTutorial" class="com.action.TutorialAction">
<result name="success">/output.jsp</result>
<result name="failure">/error.jsp</result>
</action>
</package>
</struts>
view raw Filter hosted with ❤ by GitHub


Step 7: Create a package ‘com.action’ and create a class named ‘TutorialAction’ within that package and paste the following code in the class file:

package com.action;
import com.opensymphony.xwork2.ActionSupport;
public class TutorialAction extends ActionSupport {
private String username;
private String password;
public String execute() {
if (this.username.equals("admin")
&& this.password.equals("admin123")) {
return "success";
} else {
return "failure";
}
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
} // End of Class
view raw class hosted with ❤ by GitHub

Step 8: Create 3 JSP files in Webcontent folder namely index.jsp, output.jsp and error.jsp and paste the following codes into the respective JSP files.


index.jsp
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<body>
<h2>Struts 2 Application</h2>
<s:form action="getTutorial.action">
<s:textfield name="username" key="Enter username" size="20" />
<s:password name="password" key="Enter password" size="20" />
<s:submit type="button" name="Login" align="center"/>
</s:form>
</body>
</html>
view raw class hosted with ❤ by GitHub

output.jsp
<%@ 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>Insert title here</title>
</head>
<body>
<p>Hello <s:property value="username" />.</p>
</body>
</html>
view raw class hosted with ❤ by GitHub

error.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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>Insert title here</title>
</head>
<body>
Sorry. Invalid username or password.
</body>
</html>
view raw class hosted with ❤ by GitHub

Step 9: The final directory structure should look as follows:



Step 10: Run the application by right clicking on the ‘MyFirstStruts2App’ and click on ‘Run on Server’. If you see the following screen everything went right. (In case you get some error check all the steps again if you have missed anything).



Enter the username as ‘admin’ and password as ‘admin123’ and click on Submit. You would get the welcome screen of output.jsp



Test your application by entering a wrong username or password and click on Submit. You would get the error screen of error.jsp





Notes:
  •    The index.jsp is created as the first page because it’s already present by default in the web.xml file. If you want to add a new file as the first page (e.g. home.jsp), add the appropriate file name in the <welcome-file-list> tag of web.xml
  •    Make sure you have all the jar files in the proper location. (In case you get an error of NoClassDefFoundError, it’s probably because of a missing jar file).
  • The username and password can be changed by modifying the execute method of the TutorialAction class.
  • By default the execute method is invoked whenever the action class is called and this method should always return a String which is mapped in the struts.xml. If you need to call your own method modify the struts.xml by adding a method attribute with the method name as the value in the action tag as follows:  
    <action name="getTutorial" class="com.action.TutorialAction" method=”method_name”>
    view raw class hosted with ❤ by GitHub
  •  The main entry point to the Struts2  part of the application is the filter tag part of the web.xml
  • The line <%@ taglib prefix="s" uri="/struts-tags" %> in the output.jsp and index.jsp is used to enable struts tags.



No comments:

Post a Comment