Java 没有为命名空间 / 和动作名称 hello 映射的动作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19537265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 18:10:43  来源:igfitidea点击:

There is no Action mapped for namespace / and action name hello

javaconfigurationstruts2web.xmlstruts2-namespace

提问by user2794306

package com.tutorialspoint.struts2;

public class HelloWorldAction{
   private String name;

   public String execute() throws Exception {
      return "success";
   }

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }
}

index.jsp

索引.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
   pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
   <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Hello World</title>
</head>
<body>
   <h1>Hello World From Struts2</h1>
   <form action="hello">
      <label for="name">Please enter your name</label><br/>
      <input type="text" name="name"/>
      <input type="submit" value="Say Hello"/>
   </form>
</body>
</html>

struts.xml

struts.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.devMode" value="true" />
   <package name="helloworld" extends="struts-default">

      <action name="hello" 
            class="com.tutorialspoint.struts2.HelloWorldAction" 
            method="execute">
            <result name="success">/HelloWorld.jsp</result>
      </action>
   </package>
</struts>

web.xml

网页.xml

<?xml version="1.0" encoding="UTF-8"?>
<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_3_0.xsd"
   id="WebApp_ID" version="3.0">

   <display-name>Struts 2</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>

HelloWorld.jsp

HelloWorld.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
   Hello World, <s:property value="name"/>
</body>
</html>

This code i have write for hello World Example In Struts But i am getting Error :

这段代码是我为 Struts 中的 hello World 示例编写的,但出现错误:

HTTP Status 404 - There is no Action mapped for namespace / and action name index.

HTTP 状态 404 - 没有为命名空间 / 和操作名称索引映射的操作。

am new in struts and try to Understand struts Mvc Framw work But i dont know here am doing Mistake where am Missing this please help me how i will Fix it

我是 struts 的新手,并尝试了解 struts Mvc Framw 的工作但我不知道我在这里做错了哪里缺少这个请帮助我如何修复它

回答by Andrea Ligios

  1. Use the new filter

    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>
            org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
        </filter-class>
    </filter>
    

    instead of the old one

    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>
            org.apache.struts2.dispatcher.FilterDispatcher
        </filter-class>
    </filter>
    

    unless you are using a very old version of Struts2 (2.0 for example).

  2. Your web.xml deployment descriptor is messed up: you are saying it is 3.0, then linking 2.5 xmlns:web.

    If you have a Servlet 3.0 Container (Java EE 6) use:

    <web-app          xmlns="http://java.sun.com/xml/ns/javaee"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
                             http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
                    version="3.0">
    

    If you have a Servlet 2.5 Container (Java EE 5) use:

    <web-app          xmlns="http://java.sun.com/xml/ns/javaee"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
                             http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
                    version="2.5">
    
  3. Specify namespace in your package declaration, so when you will add new packages you won't have problems:

    <package name="helloworld" extends="struts-default" namespace="/" >
    
  4. No need to specify the method in Action config if it is execute(), and no need to specify the result if it is "success":

    <action name="hello" class="com.tutorialspoint.struts2.HelloWorldAction" >
        <result>/HelloWorld.jsp</result>
    </action>
    
  5. Better using HTML5 DOCTYPE and UTF-8 CharSet if possible.

  1. 使用新过滤器

    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>
            org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
        </filter-class>
    </filter>
    

    而不是旧的

    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>
            org.apache.struts2.dispatcher.FilterDispatcher
        </filter-class>
    </filter>
    

    除非您使用的是非常旧版本的 Struts2(例如 2.0)。

  2. 你的 web.xml 部署描述符搞砸了:你说它是 3.0,然后链接 2.5 xmlns:web。

    如果您有 Servlet 3.0 Container (Java EE 6),请使用:

    <web-app          xmlns="http://java.sun.com/xml/ns/javaee"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
                             http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
                    version="3.0">
    

    如果您有 Servlet 2.5 Container (Java EE 5),请使用:

    <web-app          xmlns="http://java.sun.com/xml/ns/javaee"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
                             http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
                    version="2.5">
    
  3. 在你的包声明中指定命名空间,所以当你添加新包时你不会有问题:

    <package name="helloworld" extends="struts-default" namespace="/" >
    
  4. 如果是execute()就不需要在Action config中指定方法,如果是则不需要指定结果"success"

    <action name="hello" class="com.tutorialspoint.struts2.HelloWorldAction" >
        <result>/HelloWorld.jsp</result>
    </action>
    
  5. 如果可能,最好使用 HTML5 DOCTYPE 和 UTF-8 CharSet。

回答by Nimmagadda Gowtham

In your Struts.xmlyou have given

在你的Struts.xml你已经给

<package name="helloworld" extends="struts-default">Try after changing it to

<package name="helloworld" extends="struts-default">改成后试试

<package name="default" extends="struts-default">

<package name="default" extends="struts-default">

I Hope this answer solves the problem ...

我希望这个答案能解决问题......

回答by SamDJava

Clean your application directory but most importantly i see that you are missing the namespace.

清理您的应用程序目录,但最重要的是我发现您缺少命名空间。

Sample package entry is enter code here

示例包条目是 enter code here

The package name is a logical name that you give , but the namespace needs to be "/" or if there is any other context then that name

包名称是您提供的逻辑名称,但命名空间必须是“/”,或者如果有任何其他上下文,则该名称

回答by Ryan

To anyone still experiencing these issues, please take a look at my answer on here.

对于仍然遇到这些问题的任何人,请在此处查看我的回答。

This helped me after struggling for nearly an hour. I followed tutorials step-by-step and still got the error.

在挣扎了近一个小时后,这对我有所帮助。我一步一步地按照教程进行操作,但仍然出现错误。

Let me know if it helped. Happy coding :-)

如果有帮助,请告诉我。快乐编码:-)