Java 使用Eclipse开普勒和Struts 2+;雄猫7

Java 使用Eclipse开普勒和Struts 2+;雄猫7,java,eclipse,tomcat,configuration,struts2,Java,Eclipse,Tomcat,Configuration,Struts2,这是我第一次用Java编写应用程序,也是我第一次使用EclipseKepler、Tomcat7和Struts2,所以我很惊讶我竟然走了这么远,因为我只知道JS和Rails。无论如何,当您在运行Tomcat 7的同时在IDE中输入URL时,这个基本web应用程序实际上只是输出字符串“Success Page!” 我查看了Tomcat成功页面,因此我知道运行时服务器正在工作。而且,我的IDE中没有红色的x标记。教程告诉我,为了呈现success.jsp视图,需要添加到我已完成的web应用程序的URL

这是我第一次用Java编写应用程序,也是我第一次使用EclipseKepler、Tomcat7和Struts2,所以我很惊讶我竟然走了这么远,因为我只知道JS和Rails。无论如何,当您在运行Tomcat 7的同时在IDE中输入URL时,这个基本web应用程序实际上只是输出字符串
“Success Page!”

我查看了Tomcat成功页面,因此我知道运行时服务器正在工作。而且,我的IDE中没有红色的x标记。教程告诉我,为了呈现
success.jsp
视图,需要添加到我已完成的web应用程序的URL
gettuorial.action

但是,我在url
localhost:8080/Struts2Starter/gettuorial.action
中遇到了这个错误:

message /Struts2Starter/getTutorial.action

description The requested resource is not available.
错误日志告诉我:

    SEVERE: Dispatcher initialization failed
    Unable to load configuration. - action - file:/Users/jasonrodriguez/Java/apache-tomcat-7.0.47/wtpwebapps/Struts2Starter/WEB-INF/classes/struts.xml:10:76

SEVERE: Dispatcher initialization failed
Unable to load configuration. - action - file:/Users/jasonrodriguez/Java/apache-tomcat-7.0.47/wtpwebapps/Struts2Starter/WEB-INF/classes/struts.xml:10:76
    at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:70)
    at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:446)
    at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:490)
    at org.apache.struts2.dispatcher.ng.InitOperations.initDispatcher(InitOperations.java:74)
    at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.init(StrutsPrepareAndExecuteFilter.java:57)
    at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:281)
    at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:262)
    at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:107)
    at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4775)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5452)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)
Caused by: Action class [org.koushik.javabrains.TutorialAction] not found - action - file:/Users/jasonrodriguez/Java/apache-tomcat-7.0.47/wtpwebapps/Struts2Starter/WEB-INF/classes/struts.xml:10:76
    at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.verifyAction(XmlConfigurationProvider.java:482)
    at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addAction(XmlConfigurationProvider.java:426)
    at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addPackage(XmlConfigurationProvider.java:552)
    at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.loadPackages(XmlConfigurationProvider.java:292)
    at org.apache.struts2.config.StrutsXmlConfigurationProvider.loadPackages(StrutsXmlConfigurationProvider.java:112)
    at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:250)
    at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:67)
    ... 16 more

SEVERE: Error filterStart
My success.jsp视图:

<%@ page language="java" contentType="text/html; charset=US-ASCII"
    pageEncoding="US-ASCII"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Insert title here</title>
</head>
<body>
    Success Page!
</body>
</html>

在此处插入标题
成功页面!

我正在努力解决这个问题。在Rails中,我会加入一个
绑定。pry
,但在这里,我对如何进行故障排除没有一点线索。据我所知,也许路线在某处被弄乱了。任何见解都会非常有用,谢谢

当struts2应用程序引导时,它从
struts.xml
加载配置。它解析该文档并创建与配置相关的对象,然后将它们放入内部容器中。然后使用此配置对象实例化实际对象,如操作、结果、拦截器等。在应用程序中存在的配置中指定类很重要。Eclipse只能编译Java文件,甚至可以验证
struts.xml
的DTD,但它不会解决配置错误。Struts2是在引导上完成的。因此,您得到的错误是action类不可解析。要修复错误,您应该提供类路径上存在的类引用

<action name="getTutorial" class="org.koushik.javabrains.action.TutorialAction">
    <result>/success.jsp</result>
    <result name="failure">/error.jsp</result>
</action>

/success.jsp
/error.jsp

注意,结果名
“success”
是结果的默认名称,可以省略。

@Roman C解决了这个问题-针对那些即使在将文件添加到正确的操作文件中后程序仍无法运行的人

问题是,随着struts2发布其新版本,它正在失去对一些JAR的支持


添加jar文件(commons-lang3-3.2.1.jar)可以解决这个问题并使应用程序运行。

您知道包的概念吗??错误消息告诉我们找不到action类TutorialAction。我可以看到有一些一揽子声明。java类应该位于以下目录结构中。org/koushik/javabrains/actions/TutorialAction.java这会导致FilterDispatcher初始化失败。感谢您的帮助。我不知道包裹是什么。我只是跟着教程走,但从最好的角度来看,我能猜到,包对RubyGem有什么好处吗?下面是一个java包教程。这是关于什么是包的oracle java官方文档?以及如何创建和使用一个谢谢!这很有帮助。我很难通过本教程将这些元素上下文化。有几件事他隐瞒了,没有详细说明。哦,就像阿尤斯提到的,你能解释一下包裹是什么吗?它类似于红宝石吗?再次感谢!在
TutorialAction
中,您编写了
包org.koushik.javabrains.action。我以为他提到了这个。这是一个Java包,我不知道Ruby,但它类似于C#中的名称空间。
package org.koushik.javabrains.action;

public class TutorialAction {

    public String execute() {
        System.out.println("Hello from execute");
        return"success";
    }
}
<%@ page language="java" contentType="text/html; charset=US-ASCII"
    pageEncoding="US-ASCII"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Insert title here</title>
</head>
<body>
    Success Page!
</body>
</html>
<action name="getTutorial" class="org.koushik.javabrains.action.TutorialAction">
    <result>/success.jsp</result>
    <result name="failure">/error.jsp</result>
</action>