Monday 18 February 2013

Hello World Example


Web.xml
         The program starts from web.xml file. The web.xml file needs to be created under the WEB-INF folder under WebContent. Eclipse had already created a skeleton web.xml file for you when you created the project. So, let’s just modify it as follows:

Code-
<?xml version="1.0" encoding="UTF-8"?>

   Demo
   
    Hello.jsp
   
 
   
            struts2
            
                  org.apache.struts2.dispatcher.FilterDispatcher
            
   
      
            struts2
            /*
      
     

Struts.xml
 create struts.xml file under the src  folder.The action is mapped in strus.xml. Here the hello action is mapped and HelloAction class is execute the execute() method. From HelloAction  string “success” is return,then it mapped with result name and success.jsp is executed.
 

Code-
<?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">

      
      
                 
      
         
            /success.jsp
         
      

HelloAction.java
 The flow is coming from the struts.xml file and execute method is executed. The string “success” is return to struts.xml file. 


package view;

import com.opensymphony.xwork2.ActionSupport;


public class HelloAction
{
   public String name;
   public String getName()
    {
      return name;
    }

    public void setName(String name)
    {
      this.name = name;
     }

    public String execute()
     {
       return "success";
     }
     
}
Hello.jsp
We also need to create Hello.jsp in the WebContent folder. This file will serve as the initial action URL
Code-
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ 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>
  
  Hello World
</head>
<body>

   br>
   

</body>
</html>
Success.jsp
In success.jsp the final message is display.
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Hello</title>
</head>
<body>  
    Hello<s:property value="name"/> 
</body>
</html>
How to Run:-


Output:-






0 comments :

Post a Comment

    Categories

    Text Widget

    Followers