struts.xml映射问题

时间:2011-04-27 06:23:25

标签: java struts2

  

类型:状态报告

     

消息:没有映射的Action   命名空间/和操作名称添加。

     

description:请求的资源   (没有映射的Action   命名空间/和动作名称添加。)是   不可用。


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>

web .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>

1 个答案:

答案 0 :(得分:0)

你使用

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

您只需使用

即可实现此目的
<s:form action="add" method="post"> 

问题是struts2框架无法在给定的命名空间中找到您的操作。