Friday, 17 April 2015

Struts2 Hello world Using Eclipse


In this post, I am going to explain, simple Hello world application using struts2 and eclipse.

Step 1: Create news dynamic web project in eclipse.


Give project name as “struts_tutorial” and press next.



Press next and tick the check box “Generate web.xml deployment descriptor” and press finish.

Project structure looks like below.

Step 2: Convert project to maven project.
Right click on the project -> go to configure -> Convert to Maven Project


Give Group Id, Artifact Id as “struts_tutorial” and press finish.


Now, project structure looks like below.


Step 3: Open pom.xml and update maven dependencies for struts2.

I am using following maven dependency for struts2.

    <dependency>
   <groupId>org.apache.struts</groupId>
   <artifactId>struts2-core</artifactId>
   <version>2.3.20</version>
  </dependency>
 
pom.xml looks like below.
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>struts_tutorial</groupId>
 <artifactId>struts_tutorial</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>war</packaging>
 <build>
  <sourceDirectory>src</sourceDirectory>
  <plugins>
   <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.1</version>
    <configuration>
     <source>1.6</source>
     <target>1.6</target>
    </configuration>
   </plugin>
   <plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.4</version>
    <configuration>
     <warSourceDirectory>WebContent</warSourceDirectory>
     <failOnMissingWebXml>false</failOnMissingWebXml>
    </configuration>
   </plugin>
  </plugins>
 </build>
 <dependencies>
  <dependency>
   <groupId>org.apache.struts</groupId>
   <artifactId>struts2-core</artifactId>
   <version>2.3.20</version>
  </dependency>
 </dependencies>

</project>


Step 3: In struts2 environment, we should handover the requests to struts2. We do this by configuring web.xml.

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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>struts_tutorial</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <display-name>Struts2 Demo App</display-name>
    <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>
</web-app>


As you observe web.xml, we are forwarding all the requests to struts2 environment by using filter mapping. So now it is struts2 responsibility to process request.

Step 4: in struts2 environment, action class handles request, so we create one action class, called HelloWorldAction.java, in the package learn.struts.

package learn.struts;

public class HelloWorldAction {

 public String execute(){
  return "hello";
 }
}


Step 5: Create index.html.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>Simple struts2 Application</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
      <form method="post" action="hello">
          <input type="submit" value="submit" />
      </form>
  </body>
</html>


Step6 : Create “helloWorld.jsp” file.
<%@ 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>
 <h1>Hello World</h1>
</body>
</html>


Step 5: It is time to create struts.xml. Since Struts 2 requires struts.xml to be present in classes folder. So create struts.xml file under the WebContent/WEB-INF/classes folder. Eclipse does not create the "classes" folder by default, so you need to do this yourself. To do this, right click on the WEB-INF folder in the project explorer and select New > Folder.

Create struts.xml file inside classes.



Project structure looks like below.
 
Copy following code into struts.xml.
<!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="hello" class="learn.struts.HelloWorldAction">
            <result name="hello">/helloWorld.jsp</result>
        </action>
    </package>
</struts>


Step 6: Run the Application. Before running make sure, your project structure like below.


Once you ran the application, you can able to see application in the browser like below.

Click on submit, you will get output as HelloWorld.

How struts works?
Struts provides a filter dispatcher ‘org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter’  to handle all the request that comes to struts. You just forward all requests to this dispatcher using below configuration in web.xml.

 <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>


Once request reaches to filter dispatcher, filter dispatcher decides which action is going to be executed.

For example
In index.html, once the request submitted, it submits for the action hello.

         <form method="post" action="hello">
          <input type="submit" value="submit" />
      </form>
     
In struts.xml, we configured action "hello" to the action class HelloWorldAction. By default "execute" method of the action class execute, the return value of execute method is "hello", in struts.xml, the result "hello" referring to helloWorld.jsp file. So once  you click on submit button, you can able to see helloWorld.jsp file.

<struts>
    <package name="default" extends="struts-default">
        <action name="hello" class="learn.struts.HelloWorldAction">
            <result name="hello">/helloWorld.jsp</result>
        </action>
    </package>
</struts>

You can specify other action method than execute. If you don’t specify action method name explicitly, then struts assume ‘execute’ method as action method by default.

<action name="hello" class="learn.struts.HelloWorldAction" method=”feedback”>

In the above action, feedback is the action method to execute.


 
Prevoius                                                 Next                                                 Home

No comments:

Post a Comment