Wednesday, 18 July 2012

struts 2 hello world example

This is 3 of 8 part of struts 2 tutorial.It provides struts 2 hello world example.

Tutorial Content:

Part-1:Introduction to struts 2
Part-2:configuring struts 2 in eclipse
Part-3:Struts 2 hello world example
Part-4:Login page with validation in struts 2
Part-5:Struts 2 interceptors with example
Part-6:File upload in struts 2
Part-7:Struts 2 ajax example
Part-8:Struts 2 spring 3 integration example 


lets start with first struts2 application.



We will implement modules described in above diagram in our project.
Create dynamic web project named "Struts2FirstProject".For configuring struts 2 in your eclipse ide please refer configuring struts 2 link.

The web.xml file:

The web.xml configuration file is a J2EE configuration file that determines how elements of the HTTP request are processed by the servlet container. It is not strictly a Struts2 configuration file, but it is a file that needs to be configured for Struts2 to work.
As discussed earlier, this file provides an entry point for any web application. The entry point of Struts2 application will be a filter defined in deployment descriptor (web.xml). Hence we will define an entry of FilterDispatcher class in web.xml. The web.xml file needs to be created under the folder WebContent/WEB-INF.

copy following content in web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd">

 <display-name>Struts2FirstProject</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>

 <welcome-file-list>
  <welcome-file>default.jsp</welcome-file>
 </welcome-file-list>
</web-app>

Action Class:

We will have action class which is simple POJO class having attributes and method.
By default execute() method is called when client request goes to action class.

Create TutorialAction.java under src.Here we have TutorialAction.java as action class in our project.



copy following content into TutorialAction.java
package org.arpit.javapostsForLearning;

public class TutorialAction {

 public String execute()
 {
  String success="success";
  return success;
 }
}

JSP:

Create two jsp files named "TutorialView.jsp" and "Error.jsp" under WebContent.




copy following content into TutorialView.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>Struts2 tutorial</title>
</head>
<body bgColor="lightblue">
Hello world!!!This is Struts 2 tutorial
</body>
</html>

copy following content into 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>
Error!!!!
</body>
</html>

The struts.xml file:

strut2.xml provides mapping between url to action mapping.

So if client enters "http://mywebapp/getTutorial" then Tutorial action will be called.

Create strut2.xml under src folder.


copy following content 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>
 <package name="default" extends="struts-default" namespace="/">
<action name="getTutorial" class="org.arpit.javapostsForLearning.TutorialAction">
<result name="success">TutorialView.jsp</result>
<result name="error">Error.jsp</result>
</action>
</package>
</struts>




This diagram describes the request workflow :

  1. Request(http://localhost:8080/Strut2FirstProject/getTutorial) is generated by client and sent to Servlet container.
  2. Servlet container invokes FilterDispatcher filter which in turn determines appropriate action.In this project,getTutorial action goes to TutorialAction class.
  3. In tutorialAction,execute() method is executed and returns "success"
  4. As per mapping in struts2.xml,<result> name is matched with returned string "success".
  5. Accordingly,TutorialView.jsp is rendered and returned to user.
Now finally we will run our project.

right click on project->run as->run on server





so when you paste resultant url to your browser,you will get some thing like this.



  We are getting this error because we have declared welcome file as "default.jsp" which do not exist.
  so if you paste "http://localhost:8080/Strut2FirstProject/getTutorial" you will get your output as



as we included action(getTutorial) in url,It rendered "tutorialView.jsp" as output.

Source code:


Now in next post,we will write another example of login page with validation
 

Java tutorial for beginners Copyright © 2012