新手学struts2之配置Struts2及问题总结

来源:互联网 发布:淘宝成交记录没有了 编辑:程序博客网 时间:2024/05/02 04:52

        最近想学习struts2,在学校的时候也没有好好学过,可现在毕竟工作了,不能那么随意了,该学的还是得学学了,先得说下,本人现在对struts2是零基础,希望能和大家一起分享、学习、进步。

在Struts2.1.8版本之前,配置Struts2只需要6个jar包,分别是:

commons-fileupload-x.x.x.jar   用于文件上传

commons-logging- x.x.x.jar 日志记录

freemarker- x.x.x.jar      模板引擎、模板技术

ognl- x.x.x.jar     对象图导航语言(Object Graph Navigation Language)

struts2-core- x.x.x.jarstruts2核心包

xwork-core- x.x.x.jar      webwork核心包

如要整合其他框架,则需要添加相应的xxx-plugin.jar如:
整合spring需要将这个jar包导入]。struts2-spring-plugin.jar ---struts2的spring插件
开发struts2项目时,不要一股脑把struts2框架lib下的所有jar复制到自己的项目中,要是在整合其他框架。那样使得项目显得非常之臃肿。而且根本没有什么用的加进去。起不了什么作用。

我从Struts官网上下载的是最新的版本:structs-2.3.4,不少同学可能的做法是解压后把所有的jar包全导入到工程中,个人觉得甚为不妥,那样可以就会让我对每个jar包的作用失去了解,所以我只先导入了上述六个jar包

1.  建立一个名中structs_hello的Web工程,修改web.xml中内容为:

<?xmlversion="1.0"encoding="UTF-8"?><web-appversion="2.5"    xmlns="http://java.sun.com/xml/ns/javaee"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <filter>       <filter-name>struts2</filter-name>   <!--  此为struts-2.1.8版本之前的写法   <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> -->       <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>

 

2.       在src目录下新建一个名为struts.xml的名字,内容为:

<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEstruts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN""http://struts.apache.org/dtds/struts-2.0.dtd"> <struts>    <packagename="struts2"extends="struts-default">       <actionname="login"class="com.abc.action.LoginAction"method="login">           <resultname="success">/welcome.html</result>           <resultname="logout">/index.jsp</result>       </action>    </package></struts>

 

增加一个Action

package com.abc.action;public class LoginAction{ private String name; public String getName() {  return name; } public void setName(String name) {  this.name = name; }  public String login(){  System.out.println("method of execute");  if("123".equals(name)){   return "success";  }  return "logout"; }}

 

下面还是说说在配的过程最易出现什么错误吧

1.严重: Dispatcher initialization failed

java.lang.RuntimeException:java.lang.reflect.InvocationTargetException

at com.opensymphony.xwork2.inject.ContainerImpl$MethodInjector.inject(ContainerImpl.java:301)

解决方法:在使用struts-2.2.1时,需要引入javassist-3.7.ga.jar

 

2.警告: Could not create JarEntryRevision for [jar:file:/D:/apache-tomcat-6.0.28/webapps/struts_hello/WEB-INF/lib/struts2-core-2.3.4.jar]!

java.lang.NoClassDefFoundError: org/apache/commons/io/FileUtils

解决方法:需要引入commons-io-1.x.x.jar

 

3.严重: Exception starting filter struts2

java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils

解决方法:可能是缺少commons-lang-x.x.jarcommons-fileupload-x.x.x.jar包,加上这两上包

最后说一点啊,加了包之后,工程一定要重部署!

总结:随着struts2版本的升级,必需包的个数也相应的在增加,通过上述的配置过程,我们可知struts2的必需包已变成了9个。

         不对的地方,还请高手及时指出。