Struts : Setup Hello World Example
Points To Remember
Download the struts required jars and add to the lib folder.Setup : Struts Hello World Example
web.xml File<?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"> <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> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
- Struts Filter- This filter routes all incoming requests to struts.xml. struts.xml matches the actions and provides response accordingly.
- Welcome File List- this defines which file will be loaded on starting the application. We can have multiple welcome-file one followed by another.
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> <!-- ********************* START STRUTS FILE ********************************* --> <package name="HelloWorld" extends="struts-default"> <action name="HelloWorld" class="com.ekiras.action.Action" method="sayHello"> <result name="success">result.jsp</result> </action> </package> <!-- ********************* END STRUTS FILE *********************************** --> </struts>struts.xml contains
- Package - name="any unique name" extends="any other package or struts-default" namespace="optional (default namespace if not nemtioned)"
- Action name="HelloWorld" when this action is performed it sends request to function sayHello of class com.ekiras.action.Action
- Result- result="success" if the return type of method sayhello is equal to "success" then it will redirect to result.jsp page
<%@ 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>Struts Hello World</title> </head> <body> <h1>Hello World Test - STRUTS 2.0</h1> <s:form action="HelloWorld"> <s:textfield label="Enter Your Name" name="bean.usrName"/> <s:submit label="Submit" /> </s:form> </body> </html>This page is the welcome page in this example. It contains a text field that takes a name as an input and set's the value of useName fiels of Bean class on form submittion.
result.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"> <%@taglib prefix="s" uri="/struts-tags"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Struts-Hello World</title> </head> <body> <h1>Hello - <s:property value="bean.usrName"/></h1> </body> </html>This page is loaded when the action HelloWorld is performed and the method sayHello() of class Action returns a string success.
Action.java
package com.ekiras.action; import com.ekiras.bean.Bean; import com.opensymphony.xwork2.ActionSupport; public class Action extends ActionSupport{ private static final long serialVersionUID = 1L; private Bean bean; public Bean getBean() { return bean; } public void setBean(Bean bean) { this.bean = bean; } public String sayHello() { return "success"; } }This class has only one function sayHello() and it just returns a string success. It also contains an object of Bean class and its getter setters.
Bean.java
package com.ekiras.bean; public class Bean { private String usrName; public String getUsrName() { return usrName; } public void setUsrName(String usrName) { this.usrName = usrName; } }This class contains
- One member variables
- usrName- it is used to store the user name
- getter setters for member variable
No comments: