在Struts2中为不同的操作方法更改Form的操作属性 [英] Change action attribute of Form for different action methods in Struts2

查看:142
本文介绍了在Struts2中为不同的操作方法更改Form的操作属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JSP页面名称 add.jsp 中创建了一个from来保存这样的数据

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;
}

我创建了另一个列出所有域名的页面,并提供了一个编辑链接编辑任何域名。当使用点击编辑URL时,此操作被称为

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;
}

成功完成操作后,我会显示 add。 jsp 页面。 Struts在JSP页面中填充数据。

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

现在,问题是我想要更改表单标记 action属性的值。我还想将提交按钮的值更改为编辑。我打算在 Action 类中创建一些私有属性(action,Label),并且当 addDomainPage action is call我将更改这些属性的值以添加页面。类似于 loadDomain 操作。现在我不知道如何做到这一点意味着如何在视图中使用这些私有属性。告诉我,我做得正确,接下来要做什么?

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?

推荐答案

可以使用相同的动作类来映射不同的方法提交按钮。喜欢

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" />

表单操作属性应映射到操作类执行如果使用这样的提交按钮,将永远不会调用的方法。默认启用的DMI允许调用指定的方法。

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.

如果要动态更改Struts标记中的属性,可以在JSP中使用OGNL表达式而不是硬编码值。为此,您应该在执行结果之前定义动态值的操作中定义属性。例如

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}">

这篇关于在Struts2中为不同的操作方法更改Form的操作属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆