无法运行Struts 2 Hello World [英] cannot run Struts 2 Hello World

查看:297
本文介绍了无法运行Struts 2 Hello World的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

Eclipse控制台

Eclipse控制台上没有错误

There are no errors on Eclipse Console

/Struts2Test/src/struts.xml

/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

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

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

推荐答案

Hello World(在struts中)

Hello World(in struts)

在Eclipse Enterprise Edition中创建一个动态Web项目

Create a dynamic web project in Eclipse Enterprise edition

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

Eclipse>File>New>Dynamic Web Project

命名为:HelloWorld

Name it: HelloWorld

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

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>

创建一个类TestAction.java

Create a Class TestAction.java

(/HelloWorld/src/com/test/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

/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

/HelloWorld/src/struts.xml

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

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>

从struts网站下载必要的jar文件

您需要在Java Build Path中添加这些jar文件

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

选择您的项目>右键单击>属性> Java Build Path>添加外部Jar文件

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

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

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

选择您的项目>右键单击>属性> Deployment Assembly> Add>单击Java构建路径>您之前添加的jar文件将出现在此处.选择并确定.

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.

在服务器上运行项目

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

Select your project> Run as>Run on Server

在浏览器上

http://localhost:8081/HelloWorld/testAction

(我将端口号从8080更改为8081.Tomcat的默认端口号为8080.因此,URL http://localhost:8080/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. )

这篇关于无法运行Struts 2 Hello World的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆