【问题标题】:There is no Action mapped for namespace / and action name hello没有为命名空间 / 和动作名称 hello 映射的动作
【发布时间】:2013-11-01 11:14:19
【问题描述】:
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

<%@ 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

<?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 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

<%@ 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>

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

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

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

【问题讨论】:

  • 我认为您需要在 Action 类中扩展 com.opensymphony.xwork2.ActionSupport

标签: java configuration struts2 web.xml struts2-namespace


【解决方案1】:
  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 容器 (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 容器 (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。

【讨论】:

    【解决方案2】:

    在你的Struts.xml你已经给了

    &lt;package name="helloworld" extends="struts-default"&gt;改成后试试

    &lt;package name="default" extends="struts-default"&gt;

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

    【讨论】:

      【解决方案3】:

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

      示例包条目是 enter code here

      包名是你给的一个逻辑名称,但命名空间需要是“/”或者如果有任何其他上下文那么这个名字

      【讨论】:

        【解决方案4】:

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

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-04-14
          • 2016-01-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多