Struts2 入門
很久沒有看Java相關的東西了,這幾天才發現Struts2 已經發布了,以前就聽說Struts2就是以前的Webwork2,只是作了少許的改動而已,我以前也沒看過Webwork,所以趕緊下來試了一把。
Struts2的地址:http://struts.apache.org/2.x
在Eclipse中新建了一個Tomcat工程(如果用WTP插件的話Dynamic Web工程)struts2Test,以下是添加了Library后的工程截圖。
Struts2支持spring,tiles等很多技術,如果要使用,則相應的插件struts2-***-plugin.jar應該包含在classpath中,為了簡單我使用struts2-all.jar。實際上用tiles的時候發現有些問題,可能是版本匹配不對,后來我采用了sitemesh來代替tiles,用sitemesh似乎比tiles更簡單。
首先,應該設置好web.xml,要使用Struts2+sitemesh,需依次設置3個filter,最后一個listener是spring的。為了使用struts2和sitemesh的標簽,我把標簽文件單獨拷了出來。





































































要使用spring來為Struts2提供IOC,需要在源碼包中的struts.properties設置objectFactory屬性。





為了使用Struts2提供的i18n功能,定義了一個全局的資源文件globalMessages.properties,其內容就不給出了,該文件也放在源碼包中。另外,Struts2還缺少一個配置Action的文件strtus.xml,該文件也放在源碼包中;再在WEB-INF目錄下新建一個spring的配置文件applicationContext.xml和sitemesh的配置文件decorator.xml。兩個文件的類容分別如下,我就不解釋了:




























































































?








新建一個struts2的Action,完成regist功能,通常Action都繼承自ActionSupport類。
import ?cn.tju.lzy.forum.dao.UserDAO;
import ?cn.tju.lzy.forum.model.User;
import ?com.opensymphony.xwork2.ActionSupport;
public ? class ?RegistAction? extends ?ActionSupport?{
???? /**
?????*?
????? */
???? private ? static ? final ? long ?serialVersionUID? = ? 1L ;
???? private ?String?name? = ? null ;
???? private ?String?password? = ? null ;
???? private ?String?password2? = ? null ;
???? private ?String?email? = ? null ;
???? private ?UserDAO?userDAO? = ? null ;
????@Override
???? public ?String?execute()? throws ?Exception?{
???????? // ?TODO?Auto-generated?method?stub
????????System.out.println( " execute()?calling?in?LoginAction. " );
???????? if ?(userDAO.isUserExist(getName()))?{
????????????addActionError(getText( " user.name.exist " ));
???????????? return ?INPUT;
????????}? else ?{
????????????User?user? = ? new ?User();
????????????user.setName(name);
????????????user.setPwd(password);
????????????user.setEmail(email);
????????????userDAO.saveUser(user);
???????????? return ?SUCCESS;
????????}
????}
????@Override
???? public ? void ?validate()?{
???????? if ?(getName()? == ? null ? || ?getName().length()? < ? 1 )?{
????????????addFieldError( " name " ,getText( " user.name.empty " ));
????????}
???????? if ?(getPassword()? == ? null ? || ?getPassword().length()? < ? 1 )?{
????????????addFieldError( " password " ,getText( " user.password.empty " ));
????????}
???????? if ?(getPassword2()? == ? null ? || ?getPassword2().length()? < ? 1 )?{
????????????addFieldError( " password2 " ,getText( " user.password2.empty " ));
????????}
???????? if ?(getPassword()? != ? null &&! getPassword().equals(getPassword2()))?{
????????????addFieldError( " password2 " ,getText( " user.password.password2.unequal " ));
????????}
????}
???? public ?UserDAO?getUserDAO()?{
???????? return ?userDAO;
????}
???? public ? void ?setUserDAO(UserDAO?userDAO)?{
???????? this .userDAO? = ?userDAO;
????}
???? public ?String?getEmail()?{
???????? return ?email;
????}
???? public ? void ?setEmail(String?email)?{
???????? this .email? = ?email;
????}
???? public ?String?getName()?{
???????? return ?name;
????}
???? public ? void ?setName(String?name)?{
???????? this .name? = ?name;
????}
???? public ?String?getPassword()?{
???????? return ?password;
????}
???? public ? void ?setPassword(String?password)?{
???????? this .password? = ?password;
????}
???? public ?String?getPassword2()?{
???????? return ?password2;
????}
???? public ? void ?setPassword2(String?password2)?{
???????? this .password2? = ?password2;
????}
}
當然還要一些頁面文件,以regist.jsp為例:








































































最后,來看一下struts.xml
????"-//Apache?Software?Foundation//DTD?Struts?Configuration?2.0//EN"
????"http://struts.apache.org/dtds/struts-2.0.dtd" >
< struts >
???? < package? name ="cn.tju.lzy.forum.action" ?namespace ="/"
????????extends ="struts-default" >
???????? < action? name ="welcome" >
???????????? < result > /pages/index.jsp </ result >
???????? </ action >
????????
???????? < action? name ="*_input" > ????????????
???????????? < result > /pages/{1}.jsp </ result >
???????? </ action >
???????? < action? name ="regist" ?
????????????class ="cn.tju.lzy.forum.action.RegistAction" >
???????????? < result? name ="input" > /pages/regist.jsp </ result >
???????????? < result? name ="success" ?type ="redirect-action" > login </ result >
???????? </ action >
????????
???????? < action? name ="login" ?
????????????class ="cn.tju.lzy.forum.action.LoginAction" > ????????????
???????????? < result? name ="input" > /pages/login.jsp </ result >
???????????? < result? name ="success" ?type ="redirect-action" > welcome </ result >
???????????? < result? name ="cancel" ?type ="redirect-action" > welcome </ result >
???????? </ action >
????????
????????
???? </ package >
</ struts >
實際上將原來的ActionForm和Action合并了,這也許也是Struts2帶來的最大變化吧,這樣肯定比以前的編寫兩個類簡單了。對于另外的一些功能像攔截器、轉化器、驗證等我還沒有嘗試,大家可以參考一下這個網址:http://www.aygfsteel.com/max/category/16130.html
?
posted on 2007-01-08 21:59 all gone 閱讀(8487) 評論(8) 編輯 收藏 所屬分類: Java