Struts2搭建过程

一、找到开发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
<? xml version = "1.0" encoding = "UTF-8" ?>
<! DOCTYPE struts PUBLIC
     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
     "http://struts.apache.org/dtds/struts-2.3.dtd">
< struts >
     < constant name = "struts.devMode" value = "true" ></ constant >
     < constant name = "struts.i18n.encoding" value = "utf-8" ></ constant >
 
     < package name = "struts" namespace = "/" extends = "struts-default" >
     
     < default-action-ref name = "test" ></ default-action-ref >
     
         < global-results >
             < result name = "success" >/success.jsp</ result >
             < result name = "error" >/error.jsp</ result >
         </ global-results >
         
         < action name = "test" class = "com.test.action.TestAction" >
             < result name = "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
<? xml version = "1.0" encoding = "UTF-8" ?>
< web-app version = "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
package com.test.action;
 
import com.opensymphony.xwork2.ActionSupport;
 
public class TestAction extends ActionSupport {
     
     private static final long serialVersionUID = 1L;
 
     public String success() {
         return SUCCESS;
     }
     
     public String error() {
         return ERROR;
     }
     
     public String index() {
         return "index" ;
     }
     
}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值