<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    all gone

    all gone

    Struts2 入門

    很久沒有看Java相關的東西了,這幾天才發現Struts2 已經發布了,以前就聽說Struts2就是以前的Webwork2,只是作了少許的改動而已,我以前也沒看過Webwork,所以趕緊下來試了一把。
    Struts2的地址:http://struts.apache.org/2.x

    在Eclipse中新建了一個Tomcat工程(如果用WTP插件的話Dynamic Web工程)struts2Test,以下是添加了Library后的工程截圖。o_strut2_1.png

    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的標簽,我把標簽文件單獨拷了出來。

    <? xml?version="1.0"?encoding="UTF-8" ?>
    < web-app? id ="Struts2" ?version ="2.4" ?xmlns ="http://java.sun.com/xml/ns/j2ee" ?xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" ?xsi:schemaLocation ="http://java.sun.com/xml/ns/j2ee?http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >

    ????
    < display-name > Struts2 </ display-name >
    ???
    ????
    < filter > ?
    ????????
    < filter-name > Struts2-cleanup </ filter-name > ?
    ????????
    < filter-class > org.apache.struts2.dispatcher.ActionContextCleanUp </ filter-class > ?
    ????
    </ filter >

    ????
    < filter-mapping > ???
    ??????????
    < filter-name > Struts2-cleanup </ filter-name > ???
    ??????????
    < url-pattern > /* </ url-pattern > ?
    ???????
    </ filter-mapping > ?

    ????
    < filter >
    ???????????
    < filter-name > sitemesh </ filter-name >
    ????????
    < filter-class > com.opensymphony.module.sitemesh.filter.PageFilter </ filter-class > ?????
    ????
    </ filter >
    ????
    ????
    < filter-mapping > ???
    ??????????
    < filter-name > sitemesh </ filter-name > ???
    ??????????
    < url-pattern > /* </ url-pattern > ?
    ???????
    </ filter-mapping > ?
    ???????
    ????
    < filter >
    ????????
    < filter-name > struts2 </ filter-name >
    ????????
    < filter-class > org.apache.struts2.dispatcher.FilterDispatcher </ filter-class >
    ????
    </ filter >

    ????
    < filter-mapping >
    ????????
    < filter-name > struts2 </ filter-name >
    ????????
    < url-pattern > /* </ url-pattern >
    ????
    </ filter-mapping >
    ????
    ????
    < listener >
    ????????
    < listener-class > org.springframework.web.context.ContextLoaderListener </ listener-class >
    ????
    </ listener >
    ???

    ????
    < welcome-file-list >
    ????????
    < welcome-file > index.html </ welcome-file >
    ????
    </ welcome-file-list >

    ????
    <!--
    ????????This?typically?isn't?required,?as?the?taglib?is?included?in?struts-core.jar.
    ????????If?you?really?need?a?taglib?configuration?within?web.xml,
    ????????copy?struts2-core/core/src/main/resources/META-INF/struts-tags.tld
    ????????to?the?WEB-INF?directory?as?struts-tags.tld.
    ????
    ?????
    -->
    ?????
    ????
    < taglib >
    ????????
    < taglib-uri > /sitemesh-page </ taglib-uri >
    ????????
    < taglib-location > /WEB-INF/sitemesh-page.tld </ taglib-location >
    ????
    </ taglib >
    ????
    ????
    < taglib >
    ????????
    < taglib-uri > /sitemesh-decorator </ taglib-uri >
    ????????
    < taglib-location > /WEB-INF/sitemesh-decorator.tld </ taglib-location >
    ????
    </ taglib >
    ????
    ????
    < taglib >
    ????????
    < taglib-uri > /struts-tags </ taglib-uri >
    ????????
    < taglib-location > /WEB-INF/struts-tags.tld </ taglib-location >
    ????
    </ taglib >

    </ web-app >

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

    struts.action.extension? = ?do
    struts.custom.i18n.resources?
    = ?globalMessages
    struts.objectFactory?
    = ?spring
    struts.objectFactory.spring.autoWire?
    = ?type
    struts.ui.theme
    = simple

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

    <? xml?version="1.0"?encoding="UTF-8" ?>
    <! DOCTYPE?beans?PUBLIC?"-//SPRING/DTD?BEAN/EN"?"http://www.springframework.org/dtd/spring-beans.dtd" >
    < beans? default-autowire ="autodetect" >

    ????
    < bean? id ="dataSource"
    ????????class
    ="org.springframework.jdbc.datasource.DriverManagerDataSource" >
    ????????
    < property? name ="driverClassName" >
    ????????????
    < value > com.mysql.jdbc.Driver </ value >
    ????????
    </ property >
    ????????
    < property? name ="url" >
    ????????????
    < value > jdbc:mysql://localhost:3306/forum </ value >
    ????????
    </ property >
    ????????
    < property? name ="username" >
    ????????????
    < value > test </ value >
    ????????
    </ property >
    ????????
    < property? name ="password" >
    ????????????
    < null? />
    ????????
    </ property >
    ????
    </ bean >


    ????
    < bean? id ="sessionFactory"
    ????????class
    ="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
    ????????
    < property? name ="dataSource" >
    ????????????
    < ref? local ="dataSource" ? />
    ????????
    </ property >
    ????????
    < property? name ="mappingResources" >
    ????????????
    < list >
    ????????????????
    < value > cn/tju/lzy/forum/model/User.hbm.xml </ value >
    ????????????????
    < value > cn/tju/lzy/forum/model/Article.hbm.xml </ value >
    ????????????????
    < value > cn/tju/lzy/forum/model/Board.hbm.xml </ value >
    ????????????
    </ list >
    ????????
    </ property >
    ????????
    < property? name ="hibernateProperties" >
    ????????????
    < props >
    ????????????????
    < prop? key ="hibernate.dialect" >
    ????????????????????org.hibernate.dialect.MySQLDialect
    ????????????????
    </ prop >
    ????????????????
    < prop? key ="hibernate.show_sql" > true </ prop >
    ????????????????
    < prop? key ="hibernate.cache.provider_class" >
    ????????????????????net.sf.ehcache.hibernate.SingletonEhCacheProvider
    ????????????????
    </ prop >
    ????????????
    </ props >
    ????????
    </ property >
    ????
    </ bean >

    ????
    < bean? id ="transactionManager"
    ????????class
    ="org.springframework.orm.hibernate3.HibernateTransactionManager" >
    ????????
    < property? name ="sessionFactory" >
    ????????????
    < ref? local ="sessionFactory" ? />
    ????????
    </ property >
    ????
    </ bean >

    ????
    < bean? id ="userDAO"
    ????????class
    ="cn.tju.lzy.forum.dao.hibernate.UserDAOHibernate" >
    ????????
    < property? name ="sessionFactory" >
    ????????????
    < ref? local ="sessionFactory" ? />
    ????????
    </ property >
    ????
    </ bean >



    ????
    < bean? id ="boardDAO"
    ????????class
    ="cn.tju.lzy.forum.dao.hibernate.BoardDAOHibernate" >
    ????????
    < property? name ="sessionFactory" >
    ????????????
    < ref? local ="sessionFactory" ? />
    ????????
    </ property >
    ????
    </ bean >

    ????
    < bean? id ="articleDAO"
    ????????class
    ="cn.tju.lzy.forum.dao.hibernate.ArticleDAOHibernate" >
    ????????
    < property? name ="sessionFactory" >
    ????????????
    < ref? local ="sessionFactory" ? />
    ????????
    </ property >
    ????
    </ bean >
    ????
    ????
    < bean? id ="loginAction" ?singleton ="true"
    ????????class
    ="cn.tju.lzy.forum.action.LoginAction" >
    ????????
    < property? name ="userDAO" >
    ????????????
    < ref? local ="userDAO" ? />
    ????????
    </ property >
    ????
    </ bean >
    ????
    ????
    < bean? id ="registAction" ?singleton ="true"
    ????????class
    ="cn.tju.lzy.forum.action.RegistAction" >
    ????????
    < property? name ="userDAO" >
    ????????????
    < ref? local ="userDAO" ? />
    ????????
    </ property >
    ????
    </ bean >

    </ beans >

    ?

    < decorators? defaultdir ="/decorators" >
    ????
    < excludes >
    ????????
    < pattern > /decorators/main.jsp </ pattern >
    ????
    </ excludes >
    ????
    < decorator? name ="main" ?page ="main.jsp" >
    ????????
    < pattern > * </ pattern >
    ????
    </ decorator >
    </ decorators >

    新建一個struts2的Action,完成regist功能,通常Action都繼承自ActionSupport類。

    package ?cn.tju.lzy.forum.action;

    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為例:

    <% @?page?contentType = " text/html;?charset=UTF-8 " %>
    <% @?taglib?uri = " /sitemesh-decorator " ?prefix = " decorator " %>
    <% @?taglib?uri = " /struts-tags " ?prefix = " s " %>

    < html >
    < head >
    < title > regist </ title >
    </ head >
    < body >

    < center >
    < div? style ="color:red" ?align ="left" >< s:actionerror? /></ div >
    < s:form? method ="POST" ?action ="regist" >
    ????
    < table? border ="0" ?width ="600" >
    ????????
    < thead? bgcolor ="#CCCCCC" >
    ????????????
    < font? size ="5" > ? < b > ? < s:text? name ="user.regist.form" ? /> ? </ b > ? </ font >
    ????????
    </ thead >

    ????????
    < tr >
    ????????????
    < td? height ="40" ?width ="100" ?align ="right" ? >< s:text
    ????????????????
    name ="user.name" ? /></ td >
    ????????????
    < td >< s:textfield? name ="name" ?size ="20" ? /></ html:text ></ td >
    ????????????
    < td? height ="40" ?align ="left" ?valign ="center"
    ????????????????style
    ="color:red;font-size:18" >< s:fielderror >
    ????????????????
    < s:param > name </ s:param >
    ????????????
    </ s:fielderror ></ td >
    ????????
    </ tr >
    ????????
    < tr >
    ????????????
    < td? height ="40" ?width ="100" ?align ="right" >< s:text? name ="user.password" ? /></ td >
    ????????????
    < td >< s:password? name ="password" ?size ="20" ? /></ td >
    ????????????
    < td? height ="40" ?align ="left" ?valign ="center"
    ????????????????style
    ="color:red;font-size:18" >< s:fielderror >
    ????????????????
    < s:param > password </ s:param >
    ????????????
    </ s:fielderror ></ td >
    ????????
    </ tr >
    ????????
    < tr >
    ????????????
    < td? height ="40" ?width ="100" ?align ="right" >< s:text? name ="user.password2" ? /></ td >
    ????????????
    < td >< s:password? name ="password2" ?size ="20" ? /></ td >
    ????????????
    < td? height ="40" ?align ="left" ?valign ="center"
    ????????????????style
    ="color:red;font-size:18" >< s:fielderror >
    ????????????????
    < s:param > password2 </ s:param >
    ????????????
    </ s:fielderror ></ td >
    ????????
    </ tr >
    ????????
    < tr >
    ????????????
    < td? height ="40" ?width ="100" ?align ="right" >< s:text? name ="user.email" ? /></ td >
    ????????????
    < td >< s:textfield? name ="email" ?size ="30" ? /></ td >
    ????????????
    < td? height ="40" ?align ="left" ?valign ="center"
    ????????????????style
    ="color:red;font-size:18" >< s:fielderror >
    ????????????????
    < s:param > email </ s:param >
    ????????????
    </ s:fielderror ></ td >
    ????????
    </ tr >

    ????????
    < tr >
    ????????????
    < td? height ="40" ?width ="100" ?align ="right" ></ td >
    ????????????
    < td >< s:submit? value ="%{getText('button.submit')}" ? /> ? < s:reset
    ????????????????
    value ="%{getText('button.reset')}" ? /> ? < s:submit
    ????????????????
    value ="%{getText('button.cancel')}" ?action ="welcome" ></ s:submit ></ td >
    ????????????
    < td? height ="40" ?align ="right" ></ td >
    ????????
    </ tr >
    ????
    </ table >
    </ s:form ></ center >


    </ body >
    </ html >


    最后,來看一下struts.xml

    <! DOCTYPE?struts?PUBLIC
    ????"-//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.tkk7.com/max/category/16130.html




    ?

    posted on 2007-01-08 21:59 all gone 閱讀(8483) 評論(8)  編輯  收藏 所屬分類: Java

    評論

    # re: Struts2 入門 2007-03-19 14:36 強,BOSS能不能加我msn: riyuuleah@hotmail.com 我在學hibernate

    強,BOSS能不能加我msn: riyuuleah@hotmail.com 我在學hibernate 和spring 想討教點問題  回復  更多評論   

    # re: Struts2 入門[未登錄] 2007-05-29 16:46 哈哈

    樓主的程序不大對阿,struts2比struts1的一個很大的進步就是action不需要繼承框架里的基類的,而你的public class RegistAction extends ActionSupport {
    something。。。

    }
    明顯不符合其精神  回復  更多評論   

    # re: Struts2 入門 2007-06-30 09:31 refactoring

    可以選擇實現Action接口或者繼承ActionSupport基類。  回復  更多評論   

    # re: Struts2 入門 2007-11-14 16:37 feng ge

    兄弟!你已經最少5年沒碰過java了吧!
    現在要好好趕?。?
    你很聰明。但不要浪費了哈!  回復  更多評論   

    # re: Struts2 入門 2007-11-30 15:29 RYOU

    我知道在struts2中properties怎么配置,我為什么不能在他給的框架中struts2-blank-2.0.11這個里怎么吧package.properties重的內容呢,我想改成日語的但是有限制  回復  更多評論   

    # re: Struts2 入門 2008-01-07 09:33 bizairshop

    struts2不配合spring好使嗎?
    航服機票: http://www.bizairshop.com  回復  更多評論   

    # 打折機票 2008-04-24 10:56 zjc1999

    <a href="http://www.shoudujp.cn/catalog.asp?cate=3">上海打折機票</a>
    <a href="http://www.shoudujp.cn/catalog.asp?cate=4">上海特價機票</a>

    <a href="http://www.xingchengzaixian.cn/catalog.asp?cate=3">歐洲國際機票/特價機票/打折機票/飛機票</a>
    <a href="http://www.xingchengzaixian.cn/catalog.asp?cate=1">北美特價機票/打折機票/國際機票預訂</a>  回復  更多評論   

    # 打折機票 2008-04-24 11:13 zjc1999

    http://www.jipiaorexian.cn/catalog.asp?cate=2
    http://www.jipiaorexian.cn/catalog.asp?cate=7

    http://www.shoudujp.cn/catalog.asp?cate=3
    http://www.shoudujp.cn/catalog.asp?cate=4

    http://www.xingchengzaixian.cn/catalog.asp?cate=3
      回復  更多評論   

    主站蜘蛛池模板: 在线播放免费人成视频在线观看| 国产成人在线免费观看| 亚洲精品福利你懂| 亚洲AV伊人久久青青草原| 成人性生交大片免费看好| 亚洲精品第一国产综合野| 亚洲熟妇少妇任你躁在线观看无码| 成人久久免费网站| 亚洲啪AV永久无码精品放毛片| 亚洲人成人无码网www国产| 99久久99久久免费精品小说| 人人狠狠综合久久亚洲| 亚洲av无码片在线播放| 午夜神器成在线人成在线人免费| 中文在线观看免费网站| 国产亚洲国产bv网站在线| 亚洲色婷婷六月亚洲婷婷6月| 黄页网站在线观看免费高清| 精选影视免费在线 | 亚洲avav天堂av在线网爱情| 国产亚洲精品无码拍拍拍色欲| 免费一本色道久久一区| a级毛片无码免费真人久久| 久久亚洲精品无码av| 91情国产l精品国产亚洲区| 亚洲精品第一综合99久久| 亚洲精品无码成人AAA片| 四虎国产精品免费视| 4455永久在线观免费看| 免费无码又爽又刺激网站| 小说专区亚洲春色校园| 亚洲国产精品成人综合久久久| 自拍偷自拍亚洲精品情侣| 在线观看免费亚洲| 久久久高清免费视频| 最近中文字幕无免费| 国产午夜无码精品免费看| a毛片成人免费全部播放| 久久亚洲中文字幕无码| 亚洲综合色7777情网站777| 久久丫精品国产亚洲av不卡|