在struts 2中默认不调用execute方法
在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您只需在 struts.xml 中输入结果并在execute() 中返回成功。
You have only input result in struts.xml and returning success in execute().
我遇到了同样的问题并找到了解决方案。
I Faced same issue and found solution for this.
试试这个。这可能对你有帮助。
<package name="example" namespace="/example" extends="struts-default">
Try this. This may help you.