Change action attribute of Form for different action methods in Struts2

穿精又带淫゛_ 提交于 2019-12-08 03:20:36

问题


I have created a from in JSP page name add.jsp to save data like this

<s:form action="AddDomain">
            <s:push value="idp">
                <s:textfield name="domainName" label="Domain Name" />
                <s:textfield name="url" label="Domain URL" />
                <s:textfield name="noOfLicense" label="License Purchased" />
                <s:textfield name="licenseExpireDate" label="License Expire Date" title="YYYY-MM-DD like 2013-01-21" /> 
                <s:textfield name="userActiveDuration" label="Active User Duration"
                    title="please mention in days" />
                <s:textarea cols="30" rows="5" name="notes" label="Note"></s:textarea>
                <s:submit value="Add"></s:submit>
            </s:push>
        </s:form>

Action Method that show this view is as

public String addDomainPage() {

    return ActionSupport.SUCCESS;
}

I have created another page that list the all domains and provide a edit link to edit any domain. When Use click on edit URL this action is called

public String loadDomain() {

    HttpServletRequest request = ServletActionContext.getRequest();
    String url = request.getParameter("durl");
    IDPBroker broker = new IDPBroker();
    idp = broker.getDomainByURL(url);
    return ActionSupport.SUCCESS;
}

On successful completion of action I show the add.jsp page. Struts populate the data in JSP page.

Now, issue is that I want to change the value of action attribute of form tag. I also want to change the value of submit button to 'Edit'. I have plan to create some private attribute(action,Label) in Action class and when an addDomainPage action is call I will change the value of these attribute with respect to add page. Similar for loadDomain action. Now I don't know how to do this means how to use these private attributes in view. Tell me is I am doing correctly and what to do next?


回答1:


The same action class could be used to map different methods on submit buttons. Like

<s:submit value="Add" method="addDomainPage" />
<s:submit value="Load" method="loadDomain" />

The form action attribute should map to the action class execute method which will never call if you use submit buttons like that. The DMI which is enabled by default allows to call specified methods.

If you want to dynamically change attributes in the Struts tags you could use OGNL expressions in JSP instead of hardcoded values. For this purpose you should define properties in the action that define dynamic values before result is executed. For example

public String getAction(){
  return "AddDomain";
}  

<s:form action="%{action}">


来源:https://stackoverflow.com/questions/20661007/change-action-attribute-of-form-for-different-action-methods-in-struts2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!