Home

 

Using the Struts Configuration Editor

RAD v7.5 provides an editor for the Struts struts-config.xml configuration file. This editor is yet another way that you can add new form beans and actions and customize their attributes. You can also directly edit the XML source file, should you prefer to do this manually instead of by using the wizards. The Struts Configuration Editor is shown in Figure | 5-15.

Figure 15-15 Struts Configuration editor

We use this editor to add a local forward called cancel to the logon action. This forward can be used by the logon action's execute method to forward the user to the logon.jsp page, as we map this forward to the logon.jsp page.

We also specify the input attribute for all our actions. This attribute specifies which page should be displayed if the validate method of a form bean or the Struts validation framework fails to validate. Usually you want to display the input page where the user entered the data so they can correct their entry.

Open the struts-config.xml file under RAD75StrutsWeb Æ WebContent Æ WEB-INF.

The editor has tabs at the bottom of the screen to navigate between the different Struts artifacts it supports:

In the Action Mappings tab, select the /logon action. You can see the logonForm (Form Bean Name) and the logon.jsp (Input).
Notice the Scope value of request; an alternative would be session.

Create a forward:

Select the Local Forwards tab found at the top of the Action Mappings page. Select the /logon action. We already have two forwards named failure and success.
Click Add in the local forwards section. A new forward with the name forward1 is created.
Overtype the name with cancel and enter /logon.jsp in the Path field in the Forward Attributes (Figure | 5-16).

Save the file.

Figure 15-16 Struts Configuration editor: Creating new forward

Note: The Redirect check box allows you to select if a redirect or forward call should be made. A forward call keeps the same request with all attributes it contains and just passes control over to the path specified.
A redirect (or send redirect) call tells the browser to make a new HTTP request, which creates a new request object (and you lose any attributes set in the original request).

A forward call does not change the URL in the browser's address field, because it is unaware that the server has passed control to another component. With a redirect call, however, the browser updates the URL in its address field to reflect the requested address.

You can also redirect or forward to other actions. It does not necessarily have to be a JSP.

Select the Source tab to look at the Struts configuration file. Example | 5-7 shows parts of the struts-config.xml XML source.

Example 15-7 Struts configuration file: struts-config.xml

<?xml version="1.0"........>
<struts-config>
	<!-- Data Sources -->
	......
	<!-- Form Beans -->
	<form-beans>
		<form-bean name="logonForm" type="rad75strutsweb.forms.LogonForm">
		</form-bean>
	</form-beans>
	<!-- Global Exceptions -->
	......
	<!-- Global Forwards -->
	......
	<!-- Action Mappings -->
	<action-mappings>
		<action path="/logon" type="rad75strutsweb.actions.LogonAction"
					name="logonForm" scope="request" input="/logon.jsp">
			<forward name="success" path="/customerListing.jsp">
			</forward>
			<forward name="failure" path="/logon.jsp">
			</forward>
			<forward name="cancel" path="/logon.jsp">
			</forward>
		</action>
	</action-mappings>
	<message-resources
			parameter="rad75strutsweb.resources.ApplicationResources"/>
	<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
		<set-property property="pathnames"
			value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
	</plug-in>
</struts-config>

As you can see, the Struts tools have defined the logonForm bean in the <form-beans> section and the logon action in the <action-mappings> section. The JSPs, however, are not specified in the Struts configuration file. They are completely separate form the Struts framework (only the forwarding information is kept in the configuration file). At the end of the file is the name of the application resources file where our texts and error messages are stored.

The Struts Configuration Editor does round-trip editing, so if you edit something in the XML view, it is reflected in the other views.

The forwards that we use are local to each action, meaning that only the action associated with the forward can look it up. In the <global-forwards> section, you can also specify global forwards that are available for all actions in the application. Normally you have a common error page to display any severe error messages that might have occurred in your application and that prevent it from continuing. Pages like these are good targets for global forwards, and so are any other commonly used forward. Local forwards override global forwards.

Close the configuration file editor.

Notes: We recommend that all requests for JSPs go through an action class so that you have control over the flow and can prepare the view beans (form beans) necessary for the JSP to display properly. Struts provides simple forwarding actions that you can use to accomplish this.

In our example we do not perform any customization on the Struts action servlet (org.apache.struts.action.ActionServlet). If you have to do any custom life cycle processing needed to be executed, you would want to create your own action servlet, extending the ActionServlet class, and overriding the appropriate ActionServlet methods. You would then also modify the \WEB-INF\web.xml file and replace the name of the Struts ActionServlet with the name of your action servlet class.

ibm.com/redbooks