Understanding Struts Framework: Configuring the Controller and Mapping Requests, Lecture notes of Java Programming

The role of the controller in the struts framework using the example of actionservlet. It covers the configuration of the struts-config.xml file and the mapping of requests to specific servlets or jsp files. The document also demonstrates how to forward requests to specific pages using action mapping definitions.

Typology: Lecture notes

2011/2012

Uploaded on 08/09/2012

dhanyaa
dhanyaa 🇮🇳

4.7

(3)

60 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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.
docsity.com
pf2

Partial preview of the text

Download Understanding Struts Framework: Configuring the Controller and Mapping Requests and more Lecture notes Java Programming in PDF only on Docsity!

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.

action org.apache.struts.action.ActionServlet

config /WEB-INF/struts-config.xml

debug 2

detail 2

2

This servlet is responsible for handing all the request for the Struts Framework, user can map the specific pattern of request to the ActionServlet. **** 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.

action *.do

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.

docsity.com

Welcome.jsp

<%@ taglib uri="/tags/struts-bean" prefix="bean" %> <%@ taglib uri="/tags/struts-html" prefix="html" %>

<bean:message key="welcome.title"/>

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 tag is used to forward the request to the welcomeSturts.jsp.

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

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

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 welcomeSturts.jsp. The content of welcomeSturts.jsp is displayed to the user.

docsity.com