Tapestry 5.3.8 + Spring 4.0.5 小例子(一)

一、建立一個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

相關文章
相關標籤/搜索