Search code examples
formsjspstruts2struts-tags

Attribute namespace in the Struts2 form tag not working


I'm currently using Struts2 (2.3.7) and i'm facing with the following issue.
When i specify a namespace /x in the form tag on my jsp:

<%@ taglib prefix="s" uri="/struts-tags"%>
<s:form namespace="/x">
    <s:submit action="z" />
</s:form>

the html form tag that is generated contains y that is different from declared x:

<form action="/contextRoot/y/z.action" method="post"> 

where y is the namespace of the previous request, so it seems that attribute namespace has no effect. I know that tags are "namespace aware" Is prefix needed for Struts2 namespace in forms and links? but i want to specify a different one.


Solution

  • tl;dr

    Put some action attribute to your <s:form> tag as well.

    Explanation:

    If there is no action attribute in the <s:form> tag then the form action url defaults to current request url. i.e. Action and namespace come from current request. Side effect of this is that namespace attribute on <s:form> tag is simply ignored.

    Example:

    Current request: app/some_namespace/some_action

    And the form:

    <s:form namespace="/x">
        <s:submit action="z" />
    </s:form>
    

    Will produce something like that for the form url:

    app/some_namespace/some_action
    

    and for the submit button:

    app/some_namespace/z