我的 Struts 应用程序无法检测到动作类。
我的代码如下:
struts.xml
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation"
value="false" />
<constant name="struts.devMode" value="false" />
<package name="default" extends="struts-default" namespace="/">
<action name="add"
class="net.viralpatel.contact.view.ContactAction" method="add">
<result name="success" type="chain">index</result>
<result name="input" type="chain">index</result>
</action>
<action name="delete"
class="net.viralpatel.contact.view.ContactAction" method="delete">
<result name="success" type="chain">index</result>
</action>
<action name="index"
class="net.viralpatel.contact.view.ContactAction">
<result name="success">index.jsp</result>
</action>
</package>
</struts>
索引.jsp
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Contact Manager - Struts2 Hibernate Example</title>
</head>
<body>
<h1>Contact Manager</h1>
<s:actionerror/>
<s:form action="add" method="post">
<s:textfield name="contact.firstName" label="Firstname"/>
<s:textfield name="contact.lastName" label="Lastname"/>
<s:textfield name="contact.emailId" label="Email"/>
<s:textfield name="contact.cellNo" label="Cell No."/>
<s:textfield name="contact.website" label="Homepage"/>
<s:textfield name="contact.birthDate" label="Birthdate"/>
<s:submit value="Add Contact" align="center"/>
</s:form>
<h2>Contacts</h2>
<table>
<tr>
<th>Name</th>
<th>Email</th>
<th>Cell No.</th>
<th>Birthdate</th>
<th>Homepage</th>
<th>Delete</th>
</tr>
<s:iterator value="contactList" var="contact">
<tr>
<td><s:property value="lastName"/>, <s:property value="firstName"/> </td>
<td><s:property value="emailId"/></td>
<td><s:property value="cellNo"/></td>
<td><s:property value="birthDate"/></td>
<td><a href="<s:property value="website"/>">link</a></td>
<td><a href="delete?id=<s:property value="id"/>">delete</a></td>
</tr>
</s:iterator>
</table>
</body>
</html>
动作类
package net.viralpatel.contact.view;
import java.util.List;
import net.viralpatel.contact.controller.ContactManager;
import net.viralpatel.contact.model.Contact;
import com.opensymphony.xwork2.ActionSupport;
public class ContactAction extends ActionSupport {
private static final long serialVersionUID = 9149826260758390091L;
private Contact contact;
private List<Contact> contactList;
private Long id;
private ContactManager contactManager;
public ContactAction() {
contactManager = new ContactManager();
}
public String execute() {
this.contactList = contactManager.list();
System.out.println("execute called");
return SUCCESS;
}
public String add() {
System.out.println(getContact());
try {
contactManager.add(getContact());
} catch (Exception e) {
e.printStackTrace();
}
this.contactList = contactManager.list();
return SUCCESS;
}
public String delete() {
contactManager.delete(getId());
return SUCCESS;
}
public Contact getContact() {
return contact;
}
public List<Contact> getContactList() {
return contactList;
}
public void setContact(Contact contact) {
this.contact = contact;
}
public void setContactList(List<Contact> contactsList) {
this.contactList = contactsList;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
提交表单时,我收到一条错误消息:
HTTP 状态 404 - /StrutsStartup/add