1、创建一个Web项目(Crud),引入Tapestry 5.3.8 和 Spring 4.0.5 的jar文件。
2、WEB-INF下,创建Spring的配置文件“spring-service.xml”,数据库连接配置,JdbcTemplate的注入配置,以及项目中的使用的接口配置
<?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(),如
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);
}
}
3、配置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>决定了项目的包名路径,如下图所示:
4、在src路径下创建包“org.apache.tapestry5.internal.services”,将修改后的“XMLTokenStream.Java”文件放到包下。
注:这一步不是必须的,我做这一步是为了解决中文报错的问题,Tapestry 5.3.8会遇到这样的问题,具体的解决办法可参考文章http://my.oschina.net/andy1989/blog/491103
至此项目大致搭建完成!
来源:oschina
链接:https://my.oschina.net/u/1252840/blog/496497