2011-04-27 68 views
3

類型:狀態報告的struts.xml映射問題

消息:沒有映射爲 命名空間/和動作名稱添加操作。

描述:所請求的資源 (沒有映射爲 命名空間/和動作名稱添加操作。)是 不可用。


struts.xml的文件如下,並將它與web.xml文件創建

<?xml version="1.0" encoding="UTF-8" ?> 
<!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.vanita.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.vanita.contact.view.ContactAction" method="delete"> 
     <result name="success" type="chain">index</result> 
    </action> 

    <action name="index" 
     class="net.vanita.contact.view.ContactAction"> 
     <result name="success">index.jsp</result> 
    </action> 
</package> 
</struts> 

網絡的.xml

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">  
<display-name>StrutsHelloWorld</display-name>  
<welcome-file-list> 
<welcome-file>index.jsp</welcome-file> 
</welcome-file-list> 
<filter> 
<filter-name>struts2</filter-name> 
    <filter-class> 
    org.apache.struts2.dispatcher.FilterDispatcher 
    </filter-class> 
</filter>  
<filter-mapping> 
    <filter-name>struts2</filter-name> 
    <url-pattern>/*</url-pattern> 
</filter-mapping> 
</web-app> 

ContactAction.java低於

package net.vanita.contact.view; 

import java.util.List; 
import net.vanita.contact.controller.ContactManager; 
import net.vanita.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 linkController; 

    public ContactAction() { 
     linkController = new ContactManager(); 
    } 

    public String execute() { 
     if (null != contact) { 
      linkController.add(getContact()); 
     } 
     this.contactList = linkController.list(); 
     System.out.println(contactList); 
     System.out.println(contactList.size()); 
     return SUCCESS; 
    } 

    public String add() { 
     System.out.println(getContact()); 
     try { 
      linkController.add(getContact()); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return SUCCESS; 
    } 

    public String delete() { 
     linkController.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; 
    } 
} 

index.jsp文件低於

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
<%@ 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.id" label="Id"/> 
    <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> 
+0

請問您可以添加您的web.xml嗎?> – 2011-04-27 06:28:02

+0

net.vanita.contact.view.ContactAction是否擴展org.apache.struts.actionAction? – lobster1234 2011-04-27 08:04:50

+0

公共類ContactAction擴展ActionSupport – user714698 2011-04-27 09:23:40

回答

0

爲什麼你使用

<s:form action="/add" method="post"> 

在那裏你可以簡單地通過使用

<s:form action="add" method="post"> 

問題做到這一點的是,Struts2框架無法找到你的行動給定的命名空間。