Understanding Struts Controller

Understanding Struts Controller Understanding Struts Controller In this section I will describe you the Controller part of the Struts Framework. I will show you how to configure the struts-config.xml file to map the request to some destination

Understanding Struts Controller

Understanding Struts Controller

     

In this section I will describe you the Controller part of the Struts Framework. I will show you how to configure the struts-config.xml file to map the request to some destination servlet or jsp file.

The class org.apache.struts.action.ActionServlet is the heart of the Struts Framework. It is the Controller part of the Struts Framework. ActionServlet is configured as Servlet in the web.xml file as shown in the following code snippets.

 

 

 

<!-- Standard Action Servlet Configuration (with debugging) -->
<servlet>
   <servlet-name>action</servlet-name>
  <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
  <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>2</load-on-startup>
</servlet>

This servlet is responsible for handing all the request for the Struts Framework, user can map the specific pattern of request to the ActionServlet. <servlet-mapping> tag in the web.xml file specifies the url pattern to be handled by the servlet. By default it is *.do, but it can be changed to anything. Following code form  the web.xml file shows the mapping.

<!-- Standard Action Servlet Mapping -->
<servlet-mapping>
  <servlet-name>action</servlet-name>
  <url-pattern>*.do</url-pattern>
</servlet-mapping>

The above mapping maps all the requests ending with .do to the ActionServlet. ActionServlet uses the configuration defined in struts-config.xml file to decide the destination of the request. Action Mapping Definitions (described below) is used to map any action. For this lesson we will create Welcome.jsp file and map the "Welcome.do" request to this page.

 

 

 

 

Welcome.jsp

<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<html:html locale="true">
<head>
   <title><bean:message key="welcome.title"/></title>
   <html:base/>
</head>
  <body bgcolor="white">
  <h3><bean:message key="welcome.heading"/></h3>
  <p><bean:message key="welcome.message"/></p>
</body>
</html:html>

Forwarding the Welcome.do request to Welcome.jsp

The "Action Mapping Definitions" is the most important part in the struts-config.xml. This section takes a form defined in the "Form Bean Definitions" section and maps it to an action class.

Following code under the <action-mappings> tag is used to forward the request to the Welcome.jsp.

<action  path="/Welcome"
  forward="/pages/Welcome.jsp"
/>
  

To call this Welcome.jsp file we will use the following code.

<html:link page="/Welcome.do">First Request to the controller</html:link>

Once the use clicks on on First Request to the controller link on the index page, request (for Welcome.do) is sent to the Controller and the controller forwards the request to Welcome.jsp. The content of Welcome.jsp is displayed to the user.

 

     

Tutorials

  1. Validations using Struts 2 Annotations
  2. Struts 1.1 Tutorials
  3. CRUD application in hibernate annotation
  4. Understanding Spring Struts Hibernate DAO Layer
  5. DAO Layer explained
  6. Developing Forgot Password Form
  7. Welcome to the Apache Struts Tutorial
  8. Developing Simple Struts Tiles Application
  9. Understanding Struts Controller
  10. Struts Hibernate Integration
  11. Developing Struts PlugIn
  12. Developing Struts Hibernate and Spring Based Login/Registration Application
  13. Struts File Upload and Save
  14. Struts 2 Features
  15. Struts 2 - History of Struts 2
  16. Struts 2 Architecture - Detail information on Struts 2 Architecture
  17. Download and Installing Struts 2
  18. Struts 2 Hello World Application Example, Learn how to develop Hello World application in struts 2.
  19. Developing JSP, Java and Configuration for Hello World Application
  20. Struts Configuration file - struts.xml
  21. Introduction to Struts 2 Tags
  22. Struts Logic Tags: An Introduction
  23. Logic Empty Tag (...)
  24. Logic Equal Tag (...)
  25. Logic greaterEqual Tag (... )
  26. Logic LessEqual Tag (...)
  27. Logic Match Tag (...)
  28. Logic Present Tag (...)
  29. Struts2 Actions
  30. Static Parameter
  31. Accessing Session Object
  32. Access Request and Response
  33. Control Tags-If / Else If / Else
  34. Append Tag (Control Tags) Example
  35. Generator Tag (Control Tags) Example
  36. Generator Tag (Control Tags) Using Count Attributes
  37. Generator Tag (Control Tags) Using an Iterator with Id Attributes
  38. Iterator Tag (Control Tags) Example
  39. Merge Tag (Control Tags) Example
  40. Subset Tag (Control Tags) Example