tsh Tapestry Spring Hibernate 集成笔记 (转)
1、在eclipse中新建tapestry项目,项目名称tshDemo
2、菜单myEclipse-->Add Spring Capabilities...
点击Next> ,注意的一点是,没有使用myeclipse自带的jar包,而是选择User Libraries自己定义的最新的spring开发包(方法是在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"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<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" "http://www.springframework.org/dtd/spring-beans.dtd">
<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);//你可以看看这一句在什么时候执行,从而了解Engine是什么时候被调用的;
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"
"http://jakarta.apache.org/tapestry/dtd/Tapestry_3_0.dtd">
<!-- 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"
"http://jakarta.apache.org/tapestry/dtd/Tapestry_3_0.dtd">
<!-- 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,如果页面正常显示,配置成功。