【问题标题】:struts 2 - action class is not mappedstruts 2 - 动作类未映射
【发布时间】:2012-07-25 02:21:11
【问题描述】:

我开始学习struts 2。我按照例子here.

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 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 id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>HelloWorld3</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
</web-app>

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

HelloWorldAction.java

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;
   }
}

但是运行并提交表单后,出现404错误。

type: Status report

message: /HelloWorld3/hello

description:The requested resource (/HelloWorld3/hello) is not available.

它不会在控制台中产生任何错误。我认为动作类没有被映射。我知道这种错误对于所有 struts 初学者来说都很常见,但我发誓我从昨天开始就在谷歌上搜索过。

struts.xml在/src/里面,我也试过放在WEB-INF/classes里面,还是不行。

我在里面使用 eclipse gallileo 和 tomcat 6。

非常感谢您的回复。

【问题讨论】:

    标签: struts2


    【解决方案1】:

    我的错,刚刚意识到web.xml 与教程中的web.xml 不一样。另外我忘了在struts.xml 中指定包名。更正这些后,我在控制台中遇到了错误,

    java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils

    为了解决这个问题,我在WEB-INF/lib 目录中添加了commons-lang3-x.y 以及本教程需要添加的其他jar。

    【讨论】:

      【解决方案2】:

      你的类可以扩展 ActionSupport 类或 Action 接口,那么只有它会成为一个动作类。并且不需要显式调用方法execute,默认会调用execute方法。如果您要覆盖它,则需要提及该方法名称。

      并且它不是强制使用 ActionSupport 类。这基本上是一个帮助类,它为您提供了许多开箱即用的功能,但同时 Struts2 框架不要求使用这个类,它只需要一个返回类型为 String 的动作类的入口方法,它可以抛出一般异常

      除了验证或国际化之外,该类还提供许多其他功能,如操作级错误等。

      <action name="hello" 
                  class="com.tutorialspoint.struts2.HelloWorldAction">
      
      
          package com.tutorialspoint.struts2;
      
          public class HelloWorldAction extends ActionSupport{
             private String name;
      
             public String execute() throws Exception {
                return "success";
             }
      
             public String getName() {
                return name;
             }
      
             public void setName(String name) {
                this.name = name;
             }
          }
      

      【讨论】:

      • 您的 HelloWorld.jsp 保存在哪里?将您的 HelloWorld.jsp webcontent 文件夹放入并尝试一下。
      • HelloWorld.jsp 和 index.jsp 在 WebContent 文件夹中。
      • 首先,您需要确定您的请求是否会执行 hello 操作。放一些肥皂,确保它会按您的意愿行事。
      • 对不起,sop 是什么?我可以通过什么方式确定我的请求是否会执行 hello 操作?
      • System.out.println("我在你好世界");常见于老兄:P
      【解决方案3】:

      commons-lang3.x.y.z jarcommons-fileupload-x.y.z jar 添加到您的buildpath 以避免'resource not available' 错误

      【讨论】:

        【解决方案4】:

        http://viralpatel.net/blogs/tutorial-create-struts-2-application-eclipse-example/

        您可以获取带有 jar 文件的 Simple Struts 2 Example Demo。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-07-07
          • 1970-01-01
          • 2012-02-15
          • 1970-01-01
          • 1970-01-01
          • 2014-11-09
          • 1970-01-01
          • 2018-06-12
          相关资源
          最近更新 更多