Struts struts-config.xml action-mapping explained
Asked Answered
P

1

8

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, this and this.

Prosenchyma answered 6/4, 2016 at 17:35 Comment(2)
It is S1 -> struts-config_1_1.dtd.Nick
thank you. I have updated the question.Prosenchyma
C
6

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.

Cottbus answered 6/4, 2016 at 18:59 Comment(2)
After reading the article i posted in my edit above, your answer makes sense to me. From what I understand, if the action tag has 'validate' attribute set to true then the bean will be given control by Struts before the Action class. If errors are found during the form validation then bean will return an ActionErrors object. This should forward whatever view is in the action tag 'input' attribute. However, if no form validation errors are found, then the Action class is given control. ...sound about right?Prosenchyma
That's right. But your question is not about validation. If you have another question you should do it by asking a new question.Cottbus

© 2022 - 2024 — McMap. All rights reserved.