在struts 2中默认不调用execute方法

发布于 2024-09-24 10:04:12 字数 1899 浏览 3 评论 0原文

在struts 2中,默认情况下不调用execute方法。

我有 HelloWorld.java 作为控制器和 HelloWorld.jsp 这是我的 struts.xml

<struts>
<package name="example" namespace="/example" extends="struts-default">
    <action name="add" class="example.HelloWorld" method="add">
        <result name="SUCCESS" type="redirect">HelloWorld</result>
    </action>
    <action name="HelloWorld"
            class="example.HelloWorld">
        <result name="input">/example/HelloWorld.jsp</result>
    </action>
</package>

package example;

import com.opensymphony.xwork2.ActionSupport;
import java.util.Date; 
import java.util.List;

/**
* <code>Set welcome message.</code>
*/
public class HelloWorld extends ActionSupport {

private static final long serialVersionUID = 9149826260758390091L;
private Contacts Contacts;
private ContactManager linkController;
private List<Contacts> ContactsList;

public HelloWorld() {
    linkController = new ContactManager();
}

@Override
public String execute() {
    if (null != Contacts) {
        linkController.add(getContacts());
    }
    this.ContactsList = linkController.list();
    System.out.println(ContactsList);
    System.out.println(ContactsList.size());
    return SUCCESS;
}

public String add() {
    System.out.println(getContacts());
    getContacts().setBirthdate(new Date());
    try {
        linkController.add(getContacts());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return SUCCESS;
}

public Contacts getContacts() {
    return Contacts;
}

public void setContacts(Contacts Contacts) {
    this.Contacts = Contacts;
}

public List<Contacts> getContactsList() {
    return ContactsList;
}

public void setContactsList(List<Contacts> ContactsList) {
    this.ContactsList = ContactsList;
}

}

in struts 2 execute method is not called by default.

I have HelloWorld.java as controller and HelloWorld.jsp this is my struts.xml

<struts>
<package name="example" namespace="/example" extends="struts-default">
    <action name="add" class="example.HelloWorld" method="add">
        <result name="SUCCESS" type="redirect">HelloWorld</result>
    </action>
    <action name="HelloWorld"
            class="example.HelloWorld">
        <result name="input">/example/HelloWorld.jsp</result>
    </action>
</package>

package example;

import com.opensymphony.xwork2.ActionSupport;
import java.util.Date; 
import java.util.List;

/**
* <code>Set welcome message.</code>
*/
public class HelloWorld extends ActionSupport {

private static final long serialVersionUID = 9149826260758390091L;
private Contacts Contacts;
private ContactManager linkController;
private List<Contacts> ContactsList;

public HelloWorld() {
    linkController = new ContactManager();
}

@Override
public String execute() {
    if (null != Contacts) {
        linkController.add(getContacts());
    }
    this.ContactsList = linkController.list();
    System.out.println(ContactsList);
    System.out.println(ContactsList.size());
    return SUCCESS;
}

public String add() {
    System.out.println(getContacts());
    getContacts().setBirthdate(new Date());
    try {
        linkController.add(getContacts());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return SUCCESS;
}

public Contacts getContacts() {
    return Contacts;
}

public void setContacts(Contacts Contacts) {
    this.Contacts = Contacts;
}

public List<Contacts> getContactsList() {
    return ContactsList;
}

public void setContactsList(List<Contacts> ContactsList) {
    this.ContactsList = ContactsList;
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

仲春光 2024-10-01 10:04:12

您只需在 struts.xml 中输入结果并在execute() 中返回成功。

<package name="example" namespace="/example" extends="struts-default">
    <action name="add" class="example.HelloWorld" method="add">
        <result name="SUCCESS" type="redirect">HelloWorld</result>
    </action>
    <action name="HelloWorld"
            class="example.HelloWorld">
        <result name="input">/example/HelloWorld.jsp</result>
        <!-- FOLLOWING LINE IS MISSING -->
        <result name="SUCCESS">/example/HelloWorld.jsp</result>
    </action>
</package>

You have only input result in struts.xml and returning success in execute().

<package name="example" namespace="/example" extends="struts-default">
    <action name="add" class="example.HelloWorld" method="add">
        <result name="SUCCESS" type="redirect">HelloWorld</result>
    </action>
    <action name="HelloWorld"
            class="example.HelloWorld">
        <result name="input">/example/HelloWorld.jsp</result>
        <!-- FOLLOWING LINE IS MISSING -->
        <result name="SUCCESS">/example/HelloWorld.jsp</result>
    </action>
</package>
请叫√我孤独 2024-10-01 10:04:12

我遇到了同样的问题并找到了解决方案。

  1. 您的validation.xml 应该仅处理ActionClass 中的属性。
  2. 对于每个ActionClass应该维护唯一的Action-Validation文件。
  3. 不要将所有动作混合在不同的 J 中

I Faced same issue and found solution for this.

  1. Your validation.xml should handle the attributes which are in ActionClass only.
  2. For each ActionClass should maintain unique Action-Validation file.
  3. Dont mingle all actions in different J
逆流 2024-10-01 10:04:12

<action name="add" class="example.HelloWorld" method="add">
    <result name="SUCCESS" type="redirect">HelloWorld</result>
    <result name="input" type="redirect">HelloWorld</result>
</action>
<action name="HelloWorld" class="example.HelloWorld">
    <result name="input">/example/HelloWorld.jsp</result>
    <result name="SUCCESS">/example/HelloWorld.jsp</result>
</action>`

试试这个。这可能对你有帮助。

<package name="example" namespace="/example" extends="struts-default">

<action name="add" class="example.HelloWorld" method="add">
    <result name="SUCCESS" type="redirect">HelloWorld</result>
    <result name="input" type="redirect">HelloWorld</result>
</action>
<action name="HelloWorld" class="example.HelloWorld">
    <result name="input">/example/HelloWorld.jsp</result>
    <result name="SUCCESS">/example/HelloWorld.jsp</result>
</action>`

Try this. This may help you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文