Dispatching Requests to an Action Class

The second requirement for using a Controller servlet is that the servlet must be able to distinguish requests for different types of actions. The Struts servlet uses a configuration file with mappings from a part of the request path to the corresponding action class to handle this. The file is named struts-config.xml and is located in the WEB-INF directory for the application by default. Example 19-8 shows the configuration file used for the Project Billboard application.

Example 19-8. Struts configuration file
<?xml version="1.0" encoding="ISO-8859-1" ?>
  
<!DOCTYPE struts-config PUBLIC
  "-//Apache Software Foundation//DTD Struts Configuration 1.0//EN"
  "http://jakarta.apache.org/struts/dtds/struts-config_1_0.dtd">
  
<struts-config>
  <global-forwards>
    <forward name="login" path="/ch19/login.jsp" redirect="true" />
    <forward name="main" path="/ch19/protected/main.jsp" 
       redirect="true" />
  </global-forwards>
  
  <action-mappings>
    <action path="/ch19/authenticate"
      type="com.ora.jsp.servlets.AuthenticateAction" />
    <action path="/ch19/logout"
      type="com.ora.jsp.servlets.LogoutAction" />
    <action path="/ch19/protected/storeMsg"
      type="com.ora.jsp.servlets.StoreMsgAction" />
    <action path="/ch19/protected/updateProfile"
      type="com.ora.jsp.servlets.UpdateProfileAction" />
  </action-mappings>
</struts-config>

It’s an XML file, as are most configuration files nowadays. The first part of the file defines what Struts calls global forward mappings. I’ll get back to them ...

Get JavaServer Pages, 3rd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.