javaformsstruts2struts2-jquerymodel-driven

Why submitting a form with Ajax it's not setting model data using ModelDriven in Struts 2?


I have following situation in code:

Action class:

@NameSpace("/")
public class MyAction extends ActionSupport implements ModelDriven<Car> {
    private Car car = new Cart();

    @Override
    public Car getModel() {
        return car;
    }

    @Action(value = "pageAction", results = {name = SUCCESS, location = "myPage", type="tiles"})
    public String showPage() {
        return SUCCESS;
    }

    @Action(value = "formSubmitAction", results = {name = SUCCESS, location = "results.jsp"})
    public String formSubmitAction() {
        System.out.println(car);
        // everything has default values (nulls)
        return SUCCESS;
    }
}

View for myPage location:

<s:form 
    namespace="/" 
    action="pageAction" 
    method="post" >
    <s:push value="model">
        <s:textfield name="color" />
        <s:textfield name="manufacturer" />
        <sj:submit
            href="formSubmitAction"         
            targets="output"  />
    </s:push>
</s:form>

<div id="output"></div>

results.jsp:

renders empty content into div#output

<s:property value="%{model}" />
<s:property value="%{model.color}" />
<s:property value="%{model.manufacturer}" />

I wonder why is that happening? Model data is not updated after submit.

I'm using struts2-jquery submit tag.

When I'm using simple form submit without Ajax the model is being updated, but I want to load data asynchronously with Ajax.

How can I achieve that?


Solution

  • The solution is to add ID to form and to sj:submit tag. But I don't know why submit tag inside form wasn't working properly. The correct code is below:

    <s:form 
        id="formId"
        namespace="/" 
        action="pageAction" 
        method="post" >
        <s:push value="model">
            <s:textfield name="color" />
            <s:textfield name="manufacturer" />
            <sj:submit
                formIds="formId"
                href="formSubmitAction"         
                targets="output"  />
        </s:push>
    </s:form>
    

    EDIT

    As it turns out you only have to add ID to form, and everything works :)
    look at link in the comment below