【问题标题】:cannot run Struts 2 Hello World无法运行 Struts 2 Hello World
【发布时间】:2020-08-07 02:29:57
【问题描述】:

问题 当我运行我的项目并尝试运行时

    ERROR Dispatcher Dispatcher initialization failed
     Unable to load configuration. - bean - jar:file:/C:/Program%20Files/Apache%20Software%20Foundation/Tomcat%208.5/wtpwebapps/Struts2Test/WEB-INF/lib/struts2-gxp-plugin-2.5.22.jar!/struts-plugin.xml:27:162
        at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:69)
 and more...

http://localhost:8081/Struts2Test/testAction

它不起作用。它显示 HTTP 状态 404(在我的浏览器上)

Eclipse 控制台

Eclipse 控制台没有错误

/Struts2Test/src/struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
    <package name="test" extends="struts-default">
        <action name="testAction" class="test.Action.TestAction" method="execute">
            <result name="success">
                /success.jsp
            </result>
            <result name="error">
                /error.jsp
            </result>
        </action>
    </package>
</struts>

TestAction.java

package test.Action;

import com.opensymphony.xwork2.ActionSupport;

public class TestAction extends ActionSupport
{
    public String execute()
    {
        return "success";
    }
}

/Struts2Test/WebContent/WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>Struts2Test</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>
  <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
   </filter>

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

【问题讨论】:

  • 你真的在使用 GXP插件吗?
  • 我不知道。如何检查我是否使用 GXP 插件?@Dave Newton
  • ...如果您不知道,那么您就没有使用它。不要随意将 S2 库放入您的应用程序中;只使用你需要的东西。我建议使用 Maven。
  • 我错误地将我的 struts.xml 文件放入了包中。它在项目结构中不可见。昨晚我经历了很多类似问题的stackoverflow帖子。我意识到 maven 项目的 struts.xml 应该在类 WEB-INF/classes 中,而对于动态 Web 项目,它应该在 src 文件夹中。虽然我仍然不明白为什么它应该在类路径上?@Dave Newton
  • 因为它是从类路径加载的。

标签: java jsp struts2 action-mapping


【解决方案1】:

Hello World(在 struts 中)

在 Eclipse 企业版中创建动态 Web 项目

Eclipse>文件>新建>动态 Web 项目

给它命名:HelloWorld

Web内容文件夹中应该有web.xml文件>WEB-INF>web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>HelloWorld</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>
   <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
   </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>  
</web-app>

创建一个类TestAction.java

(/HelloWorld/src/com/test/TestAction.java)

package com.test;

import com.opensymphony.xwork2.ActionSupport;

public class TestAction extends ActionSupport
{
    public String execute()
    {
        return "success";
    }
}

/HelloWorld/WebContent/success.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <h1> Hello World</h1>
</body>
</html>

/HelloWorld/src/struts.xml

注意:您的 struts.xml 文件应位于动态 Web 项目的 src 文件夹中。否则将无法正常工作。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
    <package name="test" extends="struts-default">
        <action name="testAction" class="com.test.TestAction" method="execute">
            <result name="success">
                /success.jsp
            </result>
            <result name="error">
                /error.jsp
            </result>
        </action>
    </package>
</struts>

Download essential jar files from struts site

你需要在你的 Java 构建路径中添加这些 jar 文件

选择您的项目>右键单击>属性>Java 构建路径>添加外部 Jar 文件

现在,您需要在 Deployment Assembly 中添加这些 jar 文件

选择您的项目>右键单击>属性>部署程序集>添加>单击 Java 构建路径>您之前添加的 jar 文件将出现在那里。选择并确定。

在服务器上运行您的项目

选择你的项目>运行方式>在服务器上运行

在您的浏览器上

http://localhost:8081/HelloWorld/testAction

(我将端口号从 8080 更改为 8081。Tomcat 的默认端口号是 8080。所以 URL 将http://localhost:8080/HelloWorld/testAction。)

【讨论】:

    猜你喜欢
    • 2014-09-25
    • 2011-07-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-03
    • 2013-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多