之前碰到这一个问题,在网上查找解决方法,改了两天也没有解决,今天重新建了一个工程一开始还是出错了,后来查看Console的信息发现以下问题:
严重: Exception starting filter struts2 java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.register(XmlConfigurationProvider.java:213) at org.apache.struts2.config.StrutsXmlConfigurationProvider.register(StrutsXmlConfigurationProvider.java:102) at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:240) at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:67) at org.apache.struts2.dispatcher.Dispatcher.getContainer(Dispatcher.java:970) at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:438) at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:482) 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:279) at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:260) at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:105) at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4908) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5602) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1572) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1562) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745) Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang3.StringUtils at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1858) at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1701) ... 21 more
首先,我的工程结构如下:
注意,复制到WebContent下的WEB-INF下的lib中的jar包总共有以下12个!
第一步,创建工程的时候勾选自动创建web.xml文件,如下图点击next
进入如下界面,再点击next
进入下一个界面,勾选Generate web.xml deployment descriptor,再点击finish就可以创建出web.xml文件了
编写web.xml的配置信息,注意Struts2使用的过滤器类是org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter,以下为我的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" 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>StrutsDemo02</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的名字 --> <filter-name>struts2</filter-name> <!-- 定义核心Filter的实现类 --> <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> </web-app>
<struts.i18n.encoding value="UTF-8"/>接下来在src文件夹下创建struts.xml,我的代码如下,文件中声明了一个名字为hello的action,该action对应的类为com.action.TestAction,返回结果为success.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> <package name="default" namespace="/" extends="struts-default"> <action name="hello" class="com.action.TestAction" > <result>/success.jsp</result> </action> </package> </struts>然后在src下创建包com.action,在包下创建TestAction.java,我的代码如下
import com.opensymphony.xwork2.ActionSupport; public class TestAction extends ActionSupport { /** * */ private static final long serialVersionUID = 1L; private String helo; //helo的getter与setter public String getHelo() { return helo; } public void setHelo(String hello) { this.helo = hello; } @Override public String execute() throws Exception { // TODO Auto-generated method stub helo="hello struts"; return SUCCESS; } }
index.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> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <!-- Struts的超链接标签,链接一个名为hello的action --> <s:a action="hello">hello</s:a> </body> </html>
<%@ 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> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <s:property value="helo"/> </body> </html>到此,右键工程,Run on Sever就成功了!
点击hello