Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Struts struts-config.xml action-mapping explained

I am a noob to Struts framework. I am trying to understand how action-mapping works exactly. Suppose I have a JavaScript file that sends an AJAX request:

$("button").click(function(){
    $.ajax({url: "myTestUrl.do", success: function(result){
        //do something with result
    });
});

and my struts-config.xml file looks like this:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
    <form-beans>
        <form-bean name="testForm" type="com.test.TestForm"/>       
    </form-beans>
    
    <!-- Global Forwards -->    
    <global-forwards>
    </global-forwards>
    
    <!-- Action Mappings -->
    <action-mappings>

        <action path="/myTestUrl" 
                type="com.test.TestAction" 
                name="testForm" 
                scope="request" />

    </action-mappings>
    <controller locale="true"/>
</struts-config>

I don't understand the relationship between the action and the form-bean. Will my request be handled by TestAction? If so, what is the purpose of the form bean type attribute?

UPDATE:

For anyone who needs a great overview of struts MCV framework check out this link.

like image 639
Tom O. Avatar asked Apr 06 '16 17:04

Tom O.


People also ask

How does Struts-config xml work?

The controller servlet uses a struts-config. xml file to map incoming requests to Struts Action objects, and instantiate any ActionForm objects associated with the action to temporarily store form data. The Action object processes requests using its execute method, while making use of any data stored in the form bean.

What is Struts what configuration files are used in Struts?

The struts-config. xml configuration file is a link between the View and Model components in the Web Client. It plays an important role in building both Controller components and Application-specific configurations. In Web NMS, this file is created specific to every application in the format as <module>-struts-config.

What is the purpose of Struts xml?

The struts. xml file contains the configuration information that you will be modifying as actions are developed. This file can be used to override default settings for an application, for example struts. devMode = false and other settings which are defined in property file.


1 Answers

The relationship is made by the name attribute in the action config. So if you use name="testForm" then form bean with the name testForm will be injected to the action's execute method.

Your request might be handled if the relative url match the path value in action config and you have mapped the action servlet to *.do in servlet mapping pattern.

The type attribute of the <form-bean> is used to enter FQCN of the bean class that would probably extend the ActionForm. It's needed by Struts to be able to instantiate a bean when required.

like image 183
Roman C Avatar answered Oct 23 '22 04:10

Roman C