默认情况下不调用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;
}
}
答案 0 :(得分:2)
你只在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>
答案 1 :(得分:0)
我遇到了同样的问题并为此找到了解决方案。
答案 2 :(得分:0)
<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>`
试试这个。这可能会对你有帮助。