Simple struts2 program with Eclipse & Apache Tomcat Server

Project View :
For setting struts2 environment Read How to set Struts2 Environment 

index.jsp
This is page for user's input and on submit action is hello.
 <%@ 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>Hello User</title>  
 </head>  
 <body>  
 <h1> Simple Struts2</h1>  
 <form action="hello">  
 <label for="name" >Your Name:</label>  
 <input type="text" name="name"/>  
 <input type="submit" value="Go"/>  
 </form>  
 </body>  
 </html>  

HelloUserAction.java
This is a POJO class and it is working as action class for Struts.
 package webideaworld.blog.hello;  
 public class HelloUserAction {  
 private String name;  
 public String execute() throws Exception{  
      return "success";  
 }  
 public String getName() {  
      return name;  
 }  
 public void setName(String name) {  
      this.name = name;  
 }  
 }  

HelloUser.jsp
This is a success page that displays hello message with User Name.
 <%@ 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">  
 <%@ taglib prefix="s" uri="/struts-tags" %>  
 <html>  
 <head>  
 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
 <title>Hello! <s:property value="name"/> </title>  
 </head>  
 <body>  
 Welcome <s:property value="name"/>  
 </body>  
 </html>  


struts.xml
This file contains information about which action class to be invoked. Here we are invoking HelloUserAction class using action name hello.
 <?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="hellouser" extends="struts-default">  
   <action name ="hello" class="webideaworld.blog.hello.HelloUserAction" method="execute">  
   <result name="success" >/HelloUser.jsp</result>  
   </action>  
   </package>  
   </struts>  

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>HelloStruts2</display-name>  
  <welcome-file-list>  
   <welcome-file>index.jsp</welcome-file>  
    </welcome-file-list>  
    <filter>  
    <filter-name>struts2</filter-name>  
    <filter-class>  
      org.apache.struts2.dispatcher.FilterDispatcher  
    </filter-class>  
   </filter>  
   <filter-mapping>  
    <filter-name>struts2</filter-name>  
    <url-pattern>/*</url-pattern>  
   </filter-mapping>  
 </web-app>  


Download Code Link 1
Download Code Link 2


output:




More Struts Topics :

No comments: