Work hard

[原创]Struts2 Zero configuration

上一篇 / 下一篇  2008-05-12 00:04:54 / 个人分类:Web Framework

查看( 41 ) / 评论( 0 )
海岸线网络聚合Agn,p;\%k N

Struts 2的零配置其实不是所有配置都不需要了,主要是靠注解和规则进行配置。零配置主要是在编写Action的同时,简化了struts.xml的配置方式。据说在struts2.1后零配置将整合入codebehind,本工程使用的还是struts2.0.11
LR{;y]W0我们来看一下项目所需要的jar包,主要是增加codebehind插件。

XEpO7U9b s:S0

Zj Zr*v#^&] q0海岸线网络聚合V,~B.Nn4t

海岸线网络聚合bQ!rOW \.} K5~3xG

配置web.xml 海岸线网络聚合~B*uN a
    <!-- 配置Struts 2框架的核心Filter --> 海岸线网络聚合TN\D8k2GG)V
    <filter>
-j(_c8_:^c zg u?}0       <filter-name>struts</filter-name>
(j4k}z/D]q CF0       <filter-class> 海岸线网络聚合7nsjQ(a |D g'K1r
           org.apache.struts2.dispatcher.FilterDispatcher 海岸线网络聚合+@+H^U p.VPQ3q
       </filter-class> 海岸线网络聚合^6Z,c@(u5?H?K
       <init-param> 海岸线网络聚合_,T R PfjD |?H?
           <param-name>actionPackages</param-name>
0v%K;K2M+K4V0           <param-value>com.hopeteam.struts.action</param-value> 海岸线网络聚合;^)P j1R.gg1O0l+L
       </init-param>
~|.qkB-VC8x0MTT0    </filter>
x,o"M4|*`b0    <filter-mapping> 海岸线网络聚合RNY!uK
       <filter-name>struts</filter-name> 海岸线网络聚合 @!R@IJV0Ic
       <url-pattern>/*</url-pattern> 海岸线网络聚合L Qut+?ISU&d
</filter-mapping> 海岸线网络聚合0Iahx(iTf6S$E:l`(V:f

%hdC V%nV6G_0struts的过滤器中增加actionPackages,以及配置atcion的包com.hopeteam.struts.action。如果某个Action位于其子包下,如com.hopeteam.struts.action.test.testZeroAction,则零配置可以自动识别test作为testZeroAction的namespace。
V2ok eF2~ ].K0海岸线网络聚合\.Ecn/e
testZeroAction.java的代码如下:
F%P2`/M9b0package com.hopeteam.struts.action.test; 海岸线网络聚合+j(_ck'N$`E`
海岸线网络聚合GB~lm#d
import org.apache.struts2.config.NullResult;
+\2l7fgQ5KDT;i.lT8Lh0import org.apache.struts2.config.Result;
&G4xV }z$p DquI0import org.apache.struts2.config.Results;
;H%ku^ e0海岸线网络聚合#DfN,I1Ts/J3pN
@Results({  
CI z&_EVtH0    @Result(name="success", type=NullResult.class, value = "/success.jsp", params = {}),   海岸线网络聚合&u3qk.y`@ {-u
    @Result(name="error", type=NullResult.class, value = "/error.jsp", params = {})   海岸线网络聚合2k-c;Y3Bk_7c u2b
})   海岸线网络聚合|0BD'^E D&r"ZARZ
public class testZeroAction { 海岸线网络聚合+dZ q*w@w NI9[4m
    private String tip; 海岸线网络聚合"jt)P;W?*c#Ps;R
     海岸线网络聚合$wp x u(j sOq5]a
    public String getTip() { 海岸线网络聚合"T zFQd
       return tip;
6Ll$U-e:w1q!^0    } 海岸线网络聚合5P5p2_ q-?r_ R

1F j.D-M0El#}O0    public void setTip(String tip) {
(H#m%n.M7|'D*c&D0       this.tip = tip; 海岸线网络聚合,| G0w/W],eyZ
    }
,Z"o~$Sw0
0L_Rh%`-HG0    public String execute() {  
4{g!CbfI:q$lv@0    tip ="Struts2 Zero Configuration测试成功";
B a5A3X S ]j W tn0        return "success";   海岸线网络聚合-D9]#o(Y;Dy.Xko*H6V
    }  
'hk;q v)o|0}  
'_}2["d#M0海岸线网络聚合0g0Nk?;mR)Sm
success.jsp的代码如下:
~|xYUid g No0<%@ page language="java" contentType="text/html; charset=GBK"%>
$g)r~O#K)|+[dM Q0<%@ taglib prefix="s" uri="/struts-tags" %> 海岸线网络聚合!ygd V;P.|:B'XfI!M1P
<html>
yc&ZFVj!`0    <head> 海岸线网络聚合&@W)Br+WOk g
       <title>success</title>
z$q)k~hW0       <meta. http-equiv="Content-Type" content="text/html; charset=GBK">
CQj(uw&a#Y4w)lr8D0    </head>
NP8C_1X:u]ICE{Wt0    <body>
D TAQ+kFl;W0        <s:property value="tip"/>
*g4~)b0t\Z*|0    </body>
N6H/{'{u8db} G0</html> 海岸线网络聚合 f!eV.pD}W

t)nQ a*M/D&b5J/y"e0Struts.xml如下: 海岸线网络聚合Y1q_T)F
<?xml version="1.0" encoding="UTF-8"?> 海岸线网络聚合r}&dy"b]2`K$H6P
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
r"C!d4qW$y0"http://struts.apache.org/dtds/struts-2.0.dtd"> 海岸线网络聚合C#pT lX*Rys {
海岸线网络聚合5H7|'fu H$mOt6^o
<struts>
5| Wu%]V}'@0海岸线网络聚合V A,N*Z uU)ql0I
</struts>
7EV1_.mKx9J0海岸线网络聚合{2t?9[7TU zg
让我们看看部署情况,在浏览器下输入
t4sbKKZKV0
http://localhost:8080/Struts2ZeroConfig/test/testZero.action,其中Struts2ZeroConfig为工程的名称,testnamespacetestZerotestZeroAction的默认配置.
}} g1wAyH\0

^v9z~+T2z*b*C[0

TAG: 原创 Zero

我来说两句

(可选)

日历

« 2009-07-05  
   1234
567891011
12131415161718
19202122232425
262728293031 

数据统计

  • 访问量: 1285
  • 日志数: 23
  • 建立时间: 2007-05-21
  • 更新时间: 2008-05-18

RSS订阅

Open Toolbar