tags:

views:

55

answers:

1

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;

/** * Set welcome message. */ 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;
}

}

A: 

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>
Trick
Can u please give me a proper answer as I am a fresher in Struts 2.
taher
I have added how your struts.xml should look like.
Trick
Did it work?? Or did you find an answer elsewhere?
Trick