Struts2搭建过程

来源:互联网 发布:java switch 枚举 编辑:程序博客网 时间:2024/05/18 03:24

一、找到开发Struts2应用需要使用到的jar文件:

  1. asm-x.x.jar:官方说要加,但我不加也行
  2. asm-commons-x.x.jar:官方说要加,但我不加也行
  3. asm-tree-x.x.jar官方说要加,但我不加也行
  4. Struts2-core-2.x.x.jar:Struts2框架的核心类库;
  5. xwork-2.x.x.jar :xwork类库,Struts2是在其之上构建的;
  6. ognl-2.6.x.jar:对象图导航语言(Object Graph Navigation Language),Struts2框架通过其读写对象属性;
  7. freemarker-2.2.x.jar:Struts2的UI标签的模板使用freemarker编写;
  8. commons-fileupload-1.2.1.jar:文件上传组件, 2.1.6版本后必须加入文件;
  9. javassist-3.11.0.GA.jarJavassist是一个开源的分析、编辑和创建Java字节码的类库
  10. commons-io-2.0.1.jarIO输入输出流组件,主要完成文件的读写功能
  11. commons-lang3-3.1.jarcommons-lang是一个很有用的开源包,它弥补了Java API在提供的基本处理方法上的不足
  12. commons-lang3-X.X.X.jar: as from version 2.3.3 Struts 2 bases on Commons Lang 3, but there are still external dependencies that base on Commons Lang.
  13. commons-logging-1.1.x.jar:ASF出品的日志包,Struts2框架使用这个日志包来支持Log4J和JDK1.4+的日志记录(如果不想显示日志的话,不需要导入)。

二、 编写Struts2的配置文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEstruts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <constantname="struts.devMode"value="true"></constant>
    <constantname="struts.i18n.encoding"value="utf-8"></constant>
 
    <packagename="struts"namespace="/"extends="struts-default">
     
    <default-action-refname="test"></default-action-ref>
     
        <global-results>
            <resultname="success">/success.jsp</result>
            <resultname="error">/error.jsp</result>
        </global-results>
         
        <actionname="test"class="com.test.action.TestAction">
            <resultname="index">/index.jsp</result>
        </action>
         
    </package>
 
</struts

三、 在web.xml中加入Struts2 MVC框架启动配置
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?xmlversion="1.0"encoding="UTF-8"?>
<web-appversion="3.0"
    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_3_0.xsd">
  <display-name></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.ng.filter.StrutsPrepareAndExecuteFilter
    </filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
   
</web-app>

四、 编写TestAction类:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
packagecom.test.action;
 
importcom.opensymphony.xwork2.ActionSupport;
 
publicclass TestAction extendsActionSupport {
     
    privatestatic final long serialVersionUID = 1L;
 
    publicString success() {
        returnSUCCESS;
    }
     
    publicString error() {
        returnERROR;
    }
     
    publicString index() {
        return"index";
    }
     
}

四、 最后编写三个页面index.jsp、success.jsp、error.jsp就OK了!

0 0