java Struts2 Hello World jsp 示例错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14278258/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 15:43:45  来源:igfitidea点击:

Struts2 Hello World jsp example error

javajspstruts2tomcat6

提问by lulu88

I am trying to run a Hello World Struts2 project in Tomcat 6.

我正在尝试在 Tomcat 6 中运行一个 Hello World Struts2 项目。

I keep getting:

我不断得到:

  root cause: java.lang.ClassNotFoundException: org.apache.struts2.interceptor.SessionAware

  java.lang.NoClassDefFoundError: org/apache/struts2/interceptor/SessionAware
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:621)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
    at java.net.URLClassLoader.access
  package za.co.vine.tutorialspoint.struts2;

  public class HelloWorldAction{
  private String name;

  public String execute() throws Exception {
  return "success";
  }

  public String getName() {
  return name;
  }

  public void setName(String name) {
  this.name = name;
  }
  }
0(URLClassLoader.java:56) at java.net.URLClassLoader.run(URLClassLoader.java:195) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:188) at java.lang.ClassLoader.loadClass(ClassLoader.java:307) at java.lang.ClassLoader.loadClass(ClassLoader.java:252) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1374) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1233) at com.opensymphony.xwork2.util.ClassLoaderUtil.loadClass(ClassLoaderUtil.java:138) at com.opensymphony.xwork2.ObjectFactory.getClassInstance(ObjectFactory.java:96) at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.verifyAction(XmlConfigurationProvider.java:398) at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addAction(XmlConfigurationProvider.java:355) at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addPackage(XmlConfigurationProvider.java:460) at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.loadPackages(XmlConfigurationProvider.java:265) at org.apache.struts2.config.StrutsXmlConfigurationProvider.loadPackages(StrutsXmlConfigurationProvider.java:111) at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:189) at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:55) at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:360) at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:403) at org.apache.struts2.dispatcher.FilterDispatcher.init(FilterDispatcher.java:190)

However I do have the required jars for struts2 in the WEB-INF/lib folder for the project.

但是,我在项目的 WEB-INF/lib 文件夹中确实有 struts2 所需的 jars。

Jars:

罐子:

struts-core xwork ognl freemarker commons-fileupload commons-io commons-lang

struts-core xwork ognl freemarker commons-fileupload commons-io commons-lang

Any ideas?

有任何想法吗?

I followed an online Struts2 HelloWorld example:

我遵循了一个在线 Struts2 HelloWorld 示例:

HelloWorld.action class:

HelloWorld.action 类:

  <%@ page contentType="text/html; charset=UTF-8" %>
  <%@ taglib prefix="s" uri="/struts-tags" %>
  <html>
  <head>
  <title>Hello World</title>
  </head>
  <body>
     Hello World, <s:property value="name"/>
  </body>
  </html>

HelloWorld.jsp:

HelloWorld.jsp:

  <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
   pageEncoding="ISO-8859-1"%>
  <%@ taglib prefix="s" uri="/struts-tags"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
  "http://www.w3.org/TR/html4/loose.dtd">
  <html>
  <head>
  <title>Hello World</title>
  </head>
  <body>
     <h1>Hello World From Struts2</h1>
      <form action="hello">
        <label for="name">Please enter your name</label><br/>
        <input type="text" name="name"/>
        <input type="submit" value="Say Hello"/>
     </form>
  </body>
  </html>

index.jsp:

索引.jsp:

  <?xml version="1.0" encoding="UTF-8"?>
  <!DOCTYPE struts PUBLIC
   "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
   <struts>
    <constant name="struts.devMode" value="true" />
    <package name="helloworld" extends="struts-default">

     <action name="hello" 
        class="za.co.vine.tutorialspoint.struts2.HelloWorldAction" 
        method="execute">
        <result name="success">/HelloWorld.jsp</result>
     </action>
  </package>
  </struts>

struts.xml:

struts.xml:

  <?xml version="1.0" encoding="UTF-8"?>
  <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://java.sun.com/xml/ns/javaee" 
  xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
  http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  id="WebApp_ID" version="3.0">

  <display-name>Struts 2</display-name>
  <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <filter>
  <filter-name>struts2</filter-name>
  <filter-class>
     org.apache.struts2.dispatcher.FilterDispatcher
  </filter-class>
  </filter>

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

web.xml:

网页.xml:

asm-3.3.jar
asm-commons-3.3.jar
asm-tree-3.3.jar
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
commons-lang3-3.1.jar
freemarker-2.3.19.jar
javaassist-3.11.0.GA.jar
ognl-3.0.5.jar
struts2-core-2.3.7.jar
xwork-core-2.3.7.jar

回答by arvin_codeHunk

Just add the following Jars only in the WEB-INF/lib. Remove all related jars if you don't have need.

只需在 WEB-INF/lib 中添加以下 Jars 即可。如果不需要,请删除所有相关的 jar。

<?xml version="1.0" encoding="UTF-8"?>
  <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://java.sun.com/xml/ns/javaee" 
  xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
  http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  id="WebApp_ID" version="3.0">

  <display-name>Struts 2</display-name>
  <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <filter>
  <filter-name>struts2</filter-name>
  <filter-class>
     org.apache.struts2.dispatcher.FilterDispatcher
  </filter-class>
  </filter>

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

If you dont have these jars then download it.

如果您没有这些 jar,请下载它。

Here org.apache.struts2.dispatcher.FilterDispatcheris now deprecated .So Edit your web.xml from

这里 org.apache.struts2.dispatcher.FilterDispatcher现在已被弃用。所以编辑你的 web.xml 从

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>Struts2 Application</display-name>

  <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
   </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

  <welcome-file-list>
    <welcome-file>/index.jsp</welcome-file>  
  </welcome-file-list>
</web-app>

to

public String execute() throws Exception {
  return "success";
}

I guess this could do the work for you.

我想这可以为你做这项工作。

回答by TechSpellBound

It seems the version of xwork-core jar that you are using is not compatible with the versions of other jars.

您使用的 xwork-core jar 版本似乎与其他 jar 版本不兼容。

Try using xwork-2.1.2.jar. I have used it earlier successfully.

尝试使用xwork-2.1.2.jar。我之前已经成功使用过它。

If it still does not work, be sure you use the same versions of each and every jar as in the sample application.

如果它仍然不起作用,请确保您使用的每个 jar 版本都与示例应用程序中的版本相同。

EDIT:

编辑

As per your code, try changing

根据您的代码,尝试更改

public String execute() throws Exception {
  setName("Honey Bunny");
  return "success";
}

to

##代码##

It seems that since you have not called the setter, Struts is searching for the property name in the session.

好像是因为你还没有调用setter,所以Struts在session中搜索属性名。

回答by Raki

Regarding your question, the problem might be in the server or in your IDE tool so clean both of them under project clean and under server clean respectively.

关于您的问题,问题可能出在服务器或您的 IDE 工具中,因此分别在项目清理和服务器清理下清理它们。

If you face the same problem again download struts2 from this link

如果您再次遇到同样的问题,请从此链接下载 struts2

struts2Just you have to copy the struts-blank.war from that folder and with the new workspacerun one program (that struts-blank.war contains test programs) definitely it will work.

struts2只需从该文件夹中复制 struts-blank.war 并the new workspace运行一个程序(该 struts-blank.war 包含测试程序)肯定会起作用。

回答by Deepak

No classdeffound error usually comes when you haven't got that class in classpath or there are more than one versions of that class in classpath. Troubleshoot using these two options you are there. I encountered this when I was working on my JSP examplesfor code samples.

当您在类路径中没有该类或在类路径中有多个该类的版本时,通常不会出现 classdeffound 错误。使用这两个选项进行故障排除。我在处理代码示例的JSP 示例时遇到了这个问题。