无法运行Struts 2 Hello World

问题描述 投票:1回答:1

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

    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

Project Structure

它不起作用。它显示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>
java jsp struts2 action-mapping
1个回答
0
投票

Hello World(在struts中)

在Eclipse Enterprise Edition中创建动态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。]

© www.soinside.com 2019 - 2024. All rights reserved.