Work hard
[原创]Struts2 Zero configuration
上一篇 /
下一篇 2008-05-12 00:04:54
/ 个人分类:Web Framework
查看( 18 ) /
评论( 0 )
+esYb6m0Struts 2的零配置其实不是所有配置都不需要了,主要是靠注解和规则进行配置。零配置主要是在编写Action的同时,简化了struts.xml的配置方式。据说在struts2.1后零配置将整合入codebehind,本工程使用的还是struts2.0.11 海岸线网络聚合z0v4oX;o|'PZ K
我们来看一下项目所需要的jar包,主要是增加codebehind插件。海岸线网络聚合#\`6[BR+[2y*~ ?
海岸线网络聚合;j+c"B'W1j:Q{1{
海岸线网络聚合 H Q!S5E*a8A
海岸线网络聚合{b7mEZ配置web.xml
.Cvu5@mzKdOz0 <!-- 配置Struts 2框架的核心Filter -->
3LK {NbE0 <filter> 海岸线网络聚合y``!~U7Ng#A!Y|(d
<filter-name>struts</filter-name> 海岸线网络聚合2Q%J!{)J@j H x%n
<filter-class>
]*e;cV#Iy OnG0 org.apache.struts2.dispatcher.FilterDispatcher 海岸线网络聚合e"{Gq R#Sn|sKYc
</filter-class>
&J(C.?,IL?&X0 <init-param>
:qsSd
m@ O0 <param-name>actionPackages</param-name>
G
t3v2i:kBo0I1i0 <param-value>com.hopeteam.struts.action</param-value> 海岸线网络聚合a7C(W,U^/} r~P$d
</init-param>
(x/cJa$vC
RRLi0 </filter>
V6u7Y_5umO$\!iK0 <filter-mapping> 海岸线网络聚合JD
Fnt|j
<filter-name>struts</filter-name> 海岸线网络聚合vIHI/Z/QM)z
<url-pattern>/*</url-pattern> 海岸线网络聚合~Wg.g,@6b+[K E
w
</filter-mapping>
0K1NO#T)G AD0海岸线网络聚合(D1sg
b#k
t
在struts的过滤器中增加actionPackages,以及配置atcion的包com.hopeteam.struts.action。如果某个Action位于其子包下,如com.hopeteam.struts.action.test.testZeroAction,则零配置可以自动识别test作为testZeroAction的namespace。 海岸线网络聚合ZmN*} O'}f.P
T/mhd"q#M*LU0testZeroAction.java的代码如下: 海岸线网络聚合 M\$n3p-c-}aT
package com.hopeteam.struts.action.test;
R9l#|#nC0海岸线网络聚合a.F:c7F}
~/r
import org.apache.struts2.config.NullResult; 海岸线网络聚合O9X Bcu9U
import org.apache.struts2.config.Result; 海岸线网络聚合S@(V*R&zQ
import org.apache.struts2.config.Results;
/^,\%S2k7~ b:\3E0海岸线网络聚合IE@p3Wv
@Results({
5B4D3An!}'s+H0 @Result(name="success", type=NullResult.class, value = "/success.jsp", params = {}), 海岸线网络聚合bGZ!} Md Bq
u
@Result(name="error", type=NullResult.class, value = "/error.jsp", params = {})
#h.wx?{0})
NOCn)Za0public class testZeroAction {
/V"uK6Y"u$uS~z0 private String tip;
*Z*?9Y,w*Yv|0 海岸线网络聚合h(VT:en9DE l
public String getTip() { 海岸线网络聚合(GpG5Flo#Sz
return tip;
-|'RRm4J%aV/L0 } 海岸线网络聚合E"w4H"M4B#Rp#Qjx
海岸线网络聚合!G6^|Atj.o
public void setTip(String tip) { 海岸线网络聚合 f4aJ/o-y)W
this.tip = tip;
AVkEjQ:M}0 } 海岸线网络聚合Lwj,Hv?
0R'Q0r.hjN1py9\!B6K.on0 public String execute() { 海岸线网络聚合*`MS;^a;M7K!N2o
tip ="Struts2 Zero Configuration测试成功";
2Tv:mH7k'ma I-PKW0 return "success";
KjU'_n
R+Ua |/r8`0 }
],NCQ_E*Z9|0} 海岸线网络聚合0O'K#n|0X"tkL\e6m
海岸线网络聚合8W2l [A@L)L-z*[;z,I
success.jsp的代码如下:
V4E.O8T"ps,jY0<%@ page language="java" contentType="text/html; charset=GBK"%>
v5~fD^[)C7X0<%@ taglib prefix="s" uri="/struts-tags" %>
x7^i#J:tG%Z0<html>
(~ I}0E:ID#f0 <head> 海岸线网络聚合6KU9p{Cx
I'a)ab
<title>success</title>
n5QGV;k:Bx0 <meta. http-equiv="Content-Type" content="text/html; charset=GBK">
vI+e
c;a[Y0 </head>
%zC(^c,h0 <body> 海岸线网络聚合w4nXs&xY
<s:property value="tip"/>
cH:Wh^1B0 </body> 海岸线网络聚合T'{}~;@
</html>
"A
P0?4Gc.k`z0
LIo[4P3Y0Struts.xml如下: 海岸线网络聚合5bR!l+l*DGE{
<?xml version="1.0" encoding="UTF-8"?>
xV&l tV a`$L0<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 海岸线网络聚合/x2V6D3z({U$M
"http://struts.apache.org/dtds/struts-2.0.dtd">
]
y^8sm?t5RH@R0
x]5{qU"^f2h1J0<struts>
)y(i
HUKu0
[cdsq"q~-kO0</struts>
vGaT:??M%_0
$eU F,{-g
zY#p0让我们看看部署情况,在浏览器下输入 海岸线网络聚合;f/I[)FoJ9A
http://localhost:8080/Struts2ZeroConfig/test/testZero.action,其中Struts2ZeroConfig为工程的名称,test为namespace,testZero为testZeroAction的默认配置. 海岸线网络聚合vF.yi-nW~c U

,It(y%_3Y5_z#C0
论坛模式
推荐
收藏
分享给好友
推荐到圈子
管理
TAG:
原创
Zero