Struts 1 > Hello World Application

Hello World Application
Lets say a quick hello to struts. Struts follows MVC 2 pattern. The following files are needed to create a hello world application.       index.jsp helloWorld.jsp web.xml struts-config.xml HelloWorldAction.java HelloWorldActionForm.java

web.xml

jsp file should be mentioned in web.xml file is used to configure the struts framework for the hello world application.<welcome-file>index.<action input="/index.apache.public String getMessage() { 11. 1.<welcome-file-list> 2.HelloWorldAction"> 10.HelloWorldActionForm"/> 06.action.public HelloWorldActionForm() { 07. 01. 04.vaannila.</struts-config> HelloWorldActionForm.</form-beans> 07.</welcome-file-list> The gateway for our hello world application is index.<action-mappings> 09.ActionForm { 03.} .<jsp:forward page="HelloWorld. 08. 1. The index.struts.jsp" name="HelloWorldActionForm"path="/HelloWorld" sco pe="session"type="com. index. 08.ActionForm.action. 12.type="com.do"/> struts-config.apache.</action-mappings> 13.xml struts-config.<forward name="success" path="/helloWorld.private String message.jsp</welcome-file> 3.xml is used to configure the servlet container properties of the hello world appliation.public class HelloWorldActionForm extends 02.vaannila.web.return message. HelloWorldActionForm class has one String variable message and the corresponding getter and setter methods. 06.<form-bean name="HelloWorldActionForm" 05.jsp file.xml as shown above. This file contains the details regarding the form bean and the action mapping.super().jsp In the hello world example the index.org.</action> 12. 05. 03.} 09.jsp" /> 11.java HelloWorldActionForm extends org. 01.<struts-config> 02.struts. 10.<form-beans> 04.jsp page simply forwards the request to the hello world action. 14.

jsp" /> 5.} 17.<body> 07.<head> 04. 12. 06. To access the HelloWorldActionForm variables in the Action we need to type caste the form object to HelloWorldActionForm. We use struts bean tag to do this.jsp we get the value of the form variable message and display it.} 13. 01.<forward name="success" path="/helloWorld.<action-mappings> 2.message = message.public void setMessage(String message) { 15. 14. So when the execute method in the action returns "success" the request will be forwarded to the helloWold. Then we can access the variables using the getter and setter methods.jsp In helloWorld.public ActionForward execute(ActionMapping mapping.</action> 6.apache.HelloWorldActionForm helloWorldForm = (HelloWorldActionForm) form. 03.xml file.vaannila.action.jsp. 01.java HelloWorldAction class extends org. 18. This configuration is done in struts-config.helloWorldForm.HttpServletResponse response)throws Exception { 07. 08.<action input="/index. 05.this.<title>Hello World</title> 05.Action { 02.scope="session" type="com.} 1. The execute method returns a value of type ActionForward.HelloWorldAction"> 4.struts. 16.</head> 06.HttpServletRequest request.ActionForm form.action. 10.struts. helloWorld.} HelloWorldAction.<%@taglib uri="/WEB-INF/struts-bean. based on its value the corresponding view will be called.apache.jsp page. 11.findForward(SUCCESS). 09.<h1> 08. The action class contains an execute method which contains the business logic of the application.</action-mappings> The name "success" is mapped to the view helloWorld.private final static String SUCCESS = "success".<html> 03. The name attribute of the bean tag hold the value of the action form and the property attribute holds the value of the variable to be displayed.jsp" name="HelloWorldActionForm"path="/HelloWorld" 3.setMessage("Hello World!").tld" prefix="bean" %> 02.public class HelloWorldAction extends org.13.<bean:write name="HelloWorldActionForm" property="message" /> . 04.return mapping.Action.

09. You can download the source code of the Hello World example by clicking on the Download link below.</h1> 10. There you go. Type the following url in the browser "http://localhost:8080/Example1/index. you have your first struts program up and running.jsp". Start the Tomcat server. .</html> Extract the downloaded files into the webapps folder of the Tomcat server.</body> 11.