【问题标题】:Struts 2.3 simple action error - beginner level [duplicate]Struts 2.3 简单动作错误 - 初学者级别 [重复]
【发布时间】:2016-07-06 17:19:00
【问题描述】:

尝试连接到本地服务器时出现以下错误。

我正在访问我的页面,如下所示... http://localhost:8080/MyFirstStruts/HomeScreen.action

HTTP 状态 404 - 没有为与上下文路径 [/MyFirstStruts] 关联的命名空间 [/] 和操作名称 [HomeScreen] 映射的操作。

我的项目名称是 MyFirstStruts。

HomeScreen.Java 代码

package com.cmdb.actions;

import com.opensymphony.xwork2.ActionSupport;

public class HomeScreen extends ActionSupport {

/**
 * The action method
 * @return name of view
 */
public String execute() {

    return SUCCESS;
}


} //end of class 
 // end of class

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" 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>MyFirstStruts</display-name>


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

    <filter-mapping>
      <filter-name>struts2</filter-name>
       <url-pattern>/*</url-pattern>
    </filter-mapping>


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

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>
    <package name="default" namespace="/" extends="struts-default">
        <action name="HomeScreen" class="actions.HomeScreen"
            method="execute">
            <result name="success">/Home.jsp</result>
        </action>
    </package>
</struts>

【问题讨论】:

  • struts.xml的位置在哪里?
  • Struts.xml 位于 Web-INF 文件夹中。 web.xml 和 Struts.xml 在同一个文件夹中。
  • 它应该在类路径上,否则您应该更改 init 参数。

标签: java configuration struts2 http-status-code-404


【解决方案1】:

你在struts.xml中提到的action的类路径actions.HomeScreen和你的类包com.cmdb.actions不一样,这些一定是一样的。

【讨论】:

  • 感谢 Alireza 的帮助。即使更改了类包,它也不起作用。
  • ---> 请。查看新的类路径。
【解决方案2】:

struts.xml 文件必须位于 ../WEB-INF/classes 文件夹下,您必须手动创建该文件夹并将您的 struts.xml 文件移至该文件夹。

您的/WEB-INF 文件夹树应如下所示:

../WEB-INF
../WEB-INF/类
../WEB-INF/classes/struts.xml
../WEB-INF/web.xml

另外,正如@Alireza 提到的,你应该在struts.xml 中设置class 属性,如下class="com.cmdb.actions.HomeScreen"

这将使您启动并运行。

【讨论】:

    猜你喜欢
    • 2013-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-17
    • 2016-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多