Dict.CN 在線詞典, 英語學習, 在線翻譯

          都市淘沙者

          荔枝FM Everyone can be host

          統(tǒng)計

          留言簿(23)

          積分與排名

          優(yōu)秀學習網(wǎng)站

          友情連接

          閱讀排行榜

          評論排行榜

          tsh Tapestry Spring Hibernate 集成筆記 (轉)

          原文:http://blog.yesky.com/236/javafoot/1209236.shtml

          參考了網(wǎng)上好多文章,記不太清了,在此一并感謝!主要有:
          Tapestry整合Spring實踐 http://tech.blogbus.com/logs/2004/06/219144.html

          1、在eclipse中新建tapestry項目,項目名稱tshDemo
          2、菜單myEclipse-->Add Spring Capabilities... ,


          點擊Next> ,注意的一點是,沒有使用myeclipse自帶的jar包,而是選擇User Libraries自己定義的最新的spring開發(fā)包(方法是在Window-->Preferences...-->Java-->Build Path-->User Libraries中定義),為了使WEB-INF/lib目錄下包含相應jar包,選擇拷貝到目錄


          點擊Finish,完成對spring支持,有一個不好的是,eclipse會把所有的jar包文件一個個明細的列在項目名下,倍長非常不方便,修改辦法,在項目屬性-->Java Build Path-->Libraries中,把所有的明細jar包全部選中刪除,點擊Add Library...-->User Library-->Next> -->勾選相應的user library-->Finish 即可

          3、修改web.xml如下:
          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE web-app
                PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
                "

          <web-app>
            <display-name>tshDemo</display-name>

            <filter>
              <filter-name>redirect</filter-name>
              <filter-class>org.apache.tapestry.RedirectFilter</filter-class>
              <init-param>
                <param-name>redirect-path</param-name>
                <param-value>/app</param-value>
              </init-param>
            </filter>

            <filter-mapping>
              <filter-name>redirect</filter-name>
              <url-pattern>/</url-pattern>
            </filter-mapping>

            <listener>
              <listener-class>
                org.springframework.web.context.ContextLoaderListener
              </listener-class>
            </listener>

            <servlet>
              <servlet-name>tshDemo</servlet-name>
              <servlet-class>
                org.apache.tapestry.ApplicationServlet
              </servlet-class>
              <load-on-startup>1</load-on-startup>
            </servlet>
            <servlet-mapping>
              <servlet-name>tshDemo</servlet-name>
              <url-pattern>/app</url-pattern>
            </servlet-mapping>
          </web-app>

          4、修改applicationContext.xml如下:
          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "

          <beans>
            <description>Spring Quick Start</description>
            <bean id="theAction" class="tsh.demo.service.LowerAction">
              <property name="message">
                <value>HeLLo </value>
              </property>
            </bean>

          </beans>

          5、新建3個包,tsh.demo.dao 持久層,tsh.demo.service 中間層,tsh.demo.web 表示層

          6、tapestry支持spring,新建tapestry engine類,如下:
          /*
           * package tsh.demo.web;
           * class tsh.demo.web.EngineWithSpring
           * Created on 2006-1-23, 17:49:43
           */
          package tsh.demo.web;

          import java.util.Map;

          import org.apache.tapestry.engine.BaseEngine;
          import org.apache.tapestry.request.RequestContext;
          import org.springframework.context.ApplicationContext;
          import org.springframework.web.context.support.WebApplicationContextUtils;

          public class EngineWithSpring extends BaseEngine {

           private static final long serialVersionUID = 1L;
           public static final String APPLICATION_CONTEXT_KEY = "appContext";
           
           protected void setupForRequest(RequestContext context) {
            super.setupForRequest(context);
            Map global = (Map) getGlobal();
            ApplicationContext ac = (ApplicationContext) global.get(APPLICATION_CONTEXT_KEY);
            if (ac == null) {
             ac = WebApplicationContextUtils.getWebApplicationContext(context.getServlet().getServletContext());
            }
            System.out.println("測試" + ac);//你可以看看這一句在什么時候執(zhí)行,從而了解Engine是什么時候被調(diào)用的;
            global.put(APPLICATION_CONTEXT_KEY, ac);
           }
          }

          7、修改tapestry配置文件tshDemo.application,如下:
          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE application PUBLIC
            "-//Apache Software Foundation//Tapestry Specification 3.0//EN"
            "
          <!-- generated by Spindle, http://spindle.sourceforge.net -->

          <application name="tshDemo" engine-class="tsh.demo.web.EngineWithSpring">
             
              <description>add a description</description>
             
              <page name="Home" specification-path="Home.page"/>
             
          </application>

          8、修改Home.page文件,如下:
          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE page-specification PUBLIC
            "-//Apache Software Foundation//Tapestry Specification 3.0//EN"
            "<!-- generated by Spindle, http://spindle.sourceforge.net -->

          <page-specification class="tsh.demo.web.pages.Home">

              <description>add a description</description>
              <property-specification name="theAction" type="tsh.demo.service.Action" >
               global.appContext.getBean("theAction")
              </property-specification>
              <property-specification name="greeting" type="java.lang.String" />
             
          </page-specification>

          9、Home類,如下:
          /*
           * package tsh.demo.web.pages;
           * class tsh.demo.web.pages.Home
           * Created on 2006-1-24, 10:51:12
           */
          package tsh.demo.web.pages;

          import org.apache.tapestry.event.PageEvent;
          import org.apache.tapestry.event.PageRenderListener;
          import org.apache.tapestry.html.BasePage;

          import tsh.demo.service.Action;

          public abstract class Home extends BasePage implements PageRenderListener {

           public abstract Action getTheAction();
           public abstract void setGreeting(String greeting);
           
           /* (non-Javadoc)
            * @see org.apache.tapestry.event.PageRenderListener#pageBeginRender(org.apache.tapestry.event.PageEvent)
            */
           public void pageBeginRender(PageEvent event) {
            System.out.println(getTheAction().execute("Rod Johnson"));
            setGreeting(getTheAction().execute("Rod Johnson"));
           }
          }

          10、Home.html文件,如下:
          <html jwcid="@Shell" title="application">
          <head></head>
          <body>
             <h3>welcome, </h3><h1><span jwcid="@Insert" value="ognl:greeting" /></h1>
          </body>
          </html>

          11、相關spring bean 類如下:
          /*
           * package tsh.demo.service;
           * class tsh.demo.service.Action
           * Created on 2005-11-23, 16:32:52
           */
          package tsh.demo.service;

          public interface Action {
           public String execute(String str);
          }

          /*
           * package tsh.demo.service;
           * class tsh.demo.service.LowerAction
           * Created on 2005-11-23, 16:36:32
           */
          package tsh.demo.service;

          public class LowerAction implements Action {
            private String message;
            
            public String getMessage() {
            return message;
            }
           
            public void setMessage(String string) {
             message = string;
            }
           
            public String execute(String str) {
              return (getMessage() + str).toLowerCase();
           }

          }

          /*
           * package tsh.demo.service;
           * class tsh.demo.service.UpperAction
           * Created on 2005-11-23, 16:34:17
           */
          package tsh.demo.service;

          public class UpperAction implements Action {

            private String message;
            
            public String getMessage() {
            return message;
            }
           
            public void setMessage(String string) {
             message = string;
            }
           
            public String execute(String str) {
             return (getMessage() + str).toUpperCase();
            }

          }

          12、在%CATALINA_HOME%\conf\Catalina\localhost目錄下建tshDemo.xml文件,如下:
          <Context path="/tshDemo" docBase="D:\eclipseWorks\workspace\tshDemo\context"
                  debug="0" privileged="true" reloadable="true">
            <Logger className="org.apache.catalina.logger.FileLogger"
                       prefix="localhost_Mssql_log." suffix=".txt"
                       timestamp="true"/>

              <!-- maxActive: Maximum number of dB connections in pool. Make sure you
                   configure your mysqld max_connections large enough to handle
                   all of your db connections. Set to 0 for no limit.
                   -->

              <!-- maxIdle: Maximum number of idle dB connections to retain in pool.
                   Set to -1 for no limit.  See also the DBCP documentation on this
                   and the minEvictableIdleTimeMillis configuration parameter.
                   -->

              <!-- maxWait: Maximum time to wait for a dB connection to become available
                   in ms, in this example 10 seconds. An Exception is thrown if
                   this timeout is exceeded.  Set to -1 to wait indefinitely.
                   -->

              <!-- username and password: MySQL dB username and password for dB connections  -->

              <!-- driverClassName: Class name for the old mm.mysql JDBC driver is
                   org.gjt.mm.mysql.Driver - we recommend using Connector/J though.
                   Class name for the official MySQL Connector/J driver is com.mysql.jdbc.Driver.
                   -->
             
              <!-- url: The JDBC connection url for connecting to your MySQL dB.
                   The autoReconnect=true argument to the url makes sure that the
                   mm.mysql JDBC Driver will automatically reconnect if mysqld closed the
                   connection.  mysqld by default closes idle connections after 8 hours.
                   -->
            <!--Resource name="jdbc/m2Base" auth="Container" type="javax.sql.DataSource"
              maxActive="100" maxIdle="30" maxWait="10000"
              url="jdbc:jtds:sqlserver://im04:1433;DatabaseName=m2Base"
              username="xxxx" password="xxxxxxxxx" driverClassName="net.sourceforge.jtds.jdbc.Driver"
              removeAbandoned="true" removeAbandonedTimeout="60" logAbandoned="true" />

            <Resource name="jdbc/merp" auth="Container" type="javax.sql.DataSource"
              maxActive="100" maxIdle="30" maxWait="10000"
              url="jdbc:jtds:sqlserver://im04:1433;DatabaseName=merp"
              username="xxxxxxx" password="xxxxxxxxx" driverClassName="net.sourceforge.jtds.jdbc.Driver"
              removeAbandoned="true" removeAbandonedTimeout="60" logAbandoned="true" /-->

          </Context>


          /**
           * Hibernate的集成日后補充
           **/
          66、啟動tomcat,訪問http://locahost:8080/tshDemo,如果頁面正常顯示,配置成功。


           

          posted on 2006-02-26 10:40 都市淘沙者 閱讀(1110) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 阿尔山市| 宜州市| 民丰县| 乌拉特前旗| 德令哈市| 华安县| 紫云| 绥棱县| 新津县| 南投县| 岳池县| 石林| 锡林浩特市| 平果县| 平遥县| 常熟市| 锦屏县| 鹤壁市| 马尔康县| 宁晋县| 裕民县| 兴仁县| 永顺县| 武穴市| 顺昌县| 中宁县| 兰州市| 叙永县| 中阳县| 鄯善县| 郓城县| 河津市| 揭西县| 讷河市| 拉孜县| 酒泉市| 开化县| 荣昌县| 麻栗坡县| 卓资县| 梅河口市|