开发者

in struts 2 execute method is not called by default

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

}


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>


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


<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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜