如何使用约定插件在 Struts 2 中调用提交按钮的方法? [英] How to invoke a method on submit button in Struts 2 using convention plugin?

查看:17
本文介绍了如何使用约定插件在 Struts 2 中调用提交按钮的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面一段关于Struts动作类的代码中,

In the following piece of code about Struts action class,

@Namespace("/admin_side")
@ResultPath("/WEB-INF/content")  // Default.

public final class TestAction extends ActionSupport implements Serializable
{
    private static final long serialVersionUID = 1L;
    private static final String SUCCESS = "success";

    private String name;
    private String email;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    @Action(value="test",results={@Result(name="success",location="Test.jsp")})
    public String execute() throws Exception
    {
        System.out.println("name = "+name);
        System.out.println("email = "+email);
        return SUCCESS;
    }

    // Some annotations to map this method to <s:submit>
    public String postAction()
    {
        System.out.println("postAction() invoked.");
        System.out.println("name = "+name);
        System.out.println("email = "+email);
        return SUCCESS;
    }
}

我想像这样在给定的提交按钮上调用 postAction() 方法,

I want to invoke the postAction() method on the given submit button like so,

<s:form action="test">
    <s:textfield id="name" name="name" label="Enter your name"/>
    <s:textfield id="email" name="email" label="Enter your email"/>

    <s:submit value="Submit" method="postAction"/>
</s:form>


我见过一些问题,比如这个,但它们都在struts.xml<中使用了XML配置/代码>.


I have seen some questions like this one but all of them use XML configurations in struts.xml.

如何使用约定插件?

推荐答案

您需要使用 namespace 属性,因为您的操作是用 @Namespace 映射的.

You need to use namespace attribute because your action is mapped with @Namespace.

<s:form namespace="/admin_side" action="test">
    <s:textfield id="name" name="name" label="Enter your name"/>
    <s:textfield id="email" name="email" label="Enter your email"/>

    <s:submit value="Submit" method="postAction"/>
</s:form>

如果根据 WW-4023 那么你可以直接调用一个方法

If this doesn't work in the latest version according to WW-4023 then you can invoke a method directly

<s:url var="myUrl" namespace="/admin_side" action="test" method="postAction"/> 
<s:form action="%{#myUrl}">
    <s:textfield id="name" name="name" label="Enter your name"/>
    <s:textfield id="email" name="email" label="Enter your email"/>

    <s:submit value="Submit"/>
</s:form>

或配置params拦截器参数 excludeParams 从排除的参数中删除 method 参数.

or configure params interceptor parameter excludeParams to remove method parameter from excluded parameters.

注意,要么你使用 DMI 和上面的方法或不使用它,在这种情况下,您应该将操作映射到方法并且不要使用属性methodsubmit 标签的methodaction 作为上述问题限制使用特殊参数.如果您有多个提交按钮,并且遵循该限制,则必须使用 JavaScript onclick 事件动态更改 form 标记的操作属性.

Note, either you use DMI and the methods above or don't use it, in this case you should map an action to the method and don't use attributes method or submit tag's method and action as the issue above restrict using special parameters. If you have multiple submit buttons if you follow that restriction you have to dynamically change form tag's action attribute using JavaScript onclick event.

这篇关于如何使用约定插件在 Struts 2 中调用提交按钮的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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