March 6, 2011

Struts 1.x - Hello World Example

Download the Struts 1.3.8 jars from here, unzip and copy them to the lib folder of the web application

Download the Apache Commons jars (Struts dependencies) from here, unzip and also copy to the lib folder of the web application

Add the following Servlet configuration to the web.xml file of the web application:

<servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
      <param-name>application</param-name>
      <param-value>resources.application</param-value>
    </init-param>
    <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <init-param>
      <param-name>debug</param-name>
      <param-value>2</param-value>
    </init-param>
    <init-param>
      <param-name>detail</param-name>
      <param-value>2</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>


Create a struts-config.xml file in the WEB-INF folder of the web application and add the following content in it:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd">

<struts-config>

  <form-beans>
     <form-bean name="helloForm" type="info.icontraining.struts.HelloForm" /> 
  </form-beans>

  <action-mappings>
     <action path="/hello" type="info.icontraining.struts.HelloAction" name="helloForm"> 
   <forward name="success" path="/hello.jsp" /> 
     </action> 

  </action-mappings>

</struts-config>


Create a welcome.jsp page

<html>
<body>
<form action="hello.do" method="post">
<input type="text" name="user" />
<input type="submit" name="Submit" />
</form>
</body>
</html>


Create a HelloForm.java class

package info.icontraining.struts;

import org.apache.struts.action.ActionForm;

public class HelloForm extends ActionForm {
 
   private String user;

   public String getUser() {
      return user;
   }

   public void setUser(String user) {
      this.user = user;
   }
}


Create a HelloAction.java class

package info.icontraining.struts;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import org.apache.struts.action.*;

public class HelloAction extends Action {
 
   public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    
      request.setAttribute("user", ((HelloForm)form).getUser()); 
      return mapping.findForward("success");
   }
}


Create a hello.jsp page

<%@ page isELIgnored="false" %>
<html>
<body>
Welcome, ${requestScope.user}
</body>
</html>


Enter the following URL in the browser to test:

http://localhost:8080/WebAppName/welcome.jsp

No comments:

Post a Comment