cannot run Struts 2 Hello World

故事扮演 提交于 2020-05-15 21:21:46

问题


Problem When I run my project and try to run

    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

It does not work. It show HTTP status 404(On my browser)

Eclipse Console

There are no errors on Eclipse Console

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

回答1:


Hello World(in struts)

Create a dynamic web project in Eclipse Enterprise edition

Eclipse>File>New>Dynamic Web Project

Name it: HelloWorld

It should have web.xml file in Web content folder>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>

Create a Class 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

Note: Your struts.xml file should be inside src folder of your dynamic web project. Otherwise it will not work.

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

You need to add these jar files in your Java Build Path

Select your Project>Right Click>Properties>Java Build Path>Add External Jar files

Now, You need to add these jar files in Deployment Assembly

Select your Project>Right Click>Properties>Deployment Assembly>Add>Click on Java Build Path>The jar files which you added earlier will be present there. Select and ok.

Run your project on Server

Select your project> Run as>Run on Server

On your browser

http://localhost:8081/HelloWorld/testAction

(I changed my port number from 8080 to 8081. Default port number for Tomcat is 8080. So the URL will http://localhost:8080/HelloWorld/testAction. )



来源:https://stackoverflow.com/questions/61392743/cannot-run-struts-2-hello-world

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!