没有为名称空间/和操作名称打招呼的操作 [英] There is no Action mapped for namespace / and action name hello

查看:132
本文介绍了没有为名称空间/和操作名称打招呼的操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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 Example编写了此代码,但出现错误:

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

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

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

一个新的Struts并尝试了解Struts Mvc Framw的工作,但我不知道这是在做误区,想念在哪里,请帮助我如何解决问题

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

推荐答案

  1. 使用新的过滤器

  1. Use the new filter

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

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

您的web.xml部署描述符混乱了:您说的是3.0,然后链接了2.5 xmlns:web.

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

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

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

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

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

  • 在包声明中指定名称空间,因此,当您添加新包时,不会有问题:

  • 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="/" >
    

  • 如果是execute(),则无需在Action config中指定方法,如果是"success",则无需指定结果:

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

  • 如果可能,最好使用HTML5 DOCTYPE和UTF-8 CharSet.

  • Better using HTML5 DOCTYPE and UTF-8 CharSet if possible.

    这篇关于没有为名称空间/和操作名称打招呼的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆