一、建立一个Web项目(Crud),引入Tapestry 5.3.8 和 Spring 4.0.5 的jar文件。 java
二、WEB-INF下,建立Spring的配置文件“spring-service.xml”,数据库链接配置,JdbcTemplate的注入配置,以及项目中的使用的接口配置 web
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd "> <bean id="peopleDao" class="example.crud.dao.impl.PeopleDaoImpl" p:jdbcTemplate-ref="jdbcTemplate"/> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" p:dataSource-ref="dataSource"/> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" p:driverClassName="oracle.jdbc.driver.OracleDriver" p:url="jdbc:oracle:thin:@localhost:1521:orcl" p:username="he" p:password="1"/> </beans>
注:Tapestry 自带的IOC配置方法是在servives包下的AppModule.java中添加bing(),如 spring
package example.spring.services; import org.apache.tapestry5.SymbolConstants; import org.apache.tapestry5.ioc.MappedConfiguration; import org.apache.tapestry5.ioc.ServiceBinder; import example.spring.dao.ConnectionPool; import example.spring.dao.PhoneBookService; import example.spring.dao.impl.ConnectionPoolImpl; import example.spring.dao.impl.PhoneBookServiceImpl; public class AppModule { public static void bind(ServiceBinder serviceBinder) { serviceBinder.bind(PhoneBookService.class, PhoneBookServiceImpl.class); serviceBinder.bind(ConnectionPool.class, ConnectionPoolImpl.class); } }三、配置web.xml文件,配置项目的根包名(example.crud ),配置Spring的加载路径(/WEB-INF/spring-service.xml),配置项目的Filter路径(org.apache.tapestry5.spring.TapestrySpringFilter)
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>Crud</display-name> <context-param> <param-name>tapestry.app-package</param-name> <param-value>example.crud</param-value> </context-param> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-service.xml</param-value> </context-param> <filter> <filter-name>app</filter-name> <filter-class>org.apache.tapestry5.spring.TapestrySpringFilter</filter-class> </filter> <filter-mapping> <filter-name>app</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
注:Tapestry 和 Spring 结合使用,项目的过滤器为“org.apache.tapestry5.spring.TapestrySpringFilter”若Tapestry单独使用,则过滤器为“org.apache.tapestry5.TapestryFilter”。参数<param-name>tapestry.app-package</param-name>决定了项目的包名路径,以下图所示: 数据库
四、在src路径下建立包“org.apache.tapestry5.internal.services”,将修改后的“XMLTokenStream.Java”文件放到包下。 apache
注:这一步不是必须的,我作这一步是为了解决中文报错的问题,Tapestry 5.3.8会遇到这样的问题,具体的解决办法可参考文章http://my.oschina.net/andy1989/blog/491103
spring-mvc
至此项目大体搭建完成! mvc