您的位置:首页 > 编程语言 > Java开发

tapestry4.0 &spring&hibernate3.0完全配置(servlet2.4)

2006-10-18 21:50 701 查看
相关基本配置文件
web.xml
hivemind.xml
applicationContent.xml

1。 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee

http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>BadmintonClub</display-name>
<filter>
<filter-name>redirect</filter-name>
<filter-class>org.apache.tapestry.RedirectFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>redirect</filter-name>
<url-pattern>/</url-pattern>
</filter-mapping>

<!--*****************************-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-

value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!--*****************************-->
<servlet>
<servlet-name>BadmintonClub</servlet-name>
<servlet-class>
org.apache.tapestry.ApplicationServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>BadmintonClub</servlet-name>
<url-pattern>/app</url-pattern>
</servlet-mapping>

<session-config>
<session-timeout>15</session-timeout>
</session-config>
</web-app>

其中
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-

value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
为与spring相关

2。 hivemind.xml

<?xml version="1.0"?>
<module id="badminton" version="1.0.0">
<contribution configuration-id="hivemind.ApplicationDefaults">
<default symbol="hivemind.lib.spring-bean-factory" value="/service-

property:app.SpringContextFactory:appContext"/>
</contribution>
<service-point id="SpringContextFactory">
<invoke-factory>
<construct class="SpringContextFactory">
<set-service property="servletContext" service-

id="tapestry.globals.ServletContext"/>
</construct>
</invoke-factory>
</service-point>
</module>
其中类SpringContextFactory包名在projectname.application中指定。
<meta key="org.apache.tapestry.page-class-packages" value="

com.springAndTapestry"/>
SpringContextFactory
package com.springAndTapestry;

import javax.servlet.ServletContext;

import org.springframework.web.context.WebApplicationContext;
import

org.springframework.web.context.support.WebApplicationContextUtils;

public class SpringContextFactory {
private ServletContext servletContext;
private WebApplicationContext appContext;

public WebApplicationContext getAppContext() {
return appContext;
}

public ServletContext getServletContext() {
return servletContext;
}

public void setServletContext(ServletContext servletContext) {
this.servletContext = servletContext;
appContext =

WebApplicationContextUtils.getWebApplicationContext

(getServletContext());
}
}

3. 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>
<bean id="dataSource"

class="org.apache.commons.dbcp.BasicDataSource" destroy-

method="close">
<property name="driverClassName">
<value>org.gjt.mm.mysql.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/erp</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value></value>
</property>
</bean>

<bean id="sessionFactory"

class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource" />
</property>
<property name="mappingResources">
<list>
<value>CourtChoiceInfo.hbm.xml</value>
<value>CourtLocation.hbm.xml</value>
<value>MeterialInfo.hbm.xml</value>
<value>UserInfo.hbm.xml</value>
<value>User.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>

<bean id="utildao" class="com.sap.badminton.hibernate.dao.UtilDao">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<bean id="param" class="com.sap.badminton.common.Parameters">
<property name="maxResult">
<value>5</value>
</property>
</bean>
</beans>

通过这三部,配置就完成了,然后可以在java类中通过Tapestry的

@InjectObject来注入在applicationContext.xml中定义的bean:
例·:Home.java

package com.springAndTapestry;

import lxh.hibernate.User;

import org.apache.tapestry.annotations.InjectObject;
import org.apache.tapestry.html.BasePage;

import com.sap.badminton.common.Parameters;
import com.sap.badminton.hibernate.dao.UtilDao;

public abstract class Home extends BasePage {
@InjectObject("spring:param")
public abstract Parameters getParam();

public String getTest(){
// return this.getParam().getMaxResult()+"" ;
return this.getii();

}

//注入UtilDao类
@InjectObject("spring:utildao")
public abstract UtilDao getUtilDao();

public String getii(){
UtilDao ud = this.getUtilDao() ;
User user = new User() ;
user.setId("6666XX") ;
user.setPassword("888888") ;
ud.save(user) ;
System.out.println(ud.getSessionFactory());
return "Hello xiaohongli" ;
}

}

Home.page

<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE page-specification PUBLIC
"-//Apache Software Foundation//Tapestry Specification 4.0//EN"
"http://jakarta.apache.org/tapestry/dtd/Tapestry_4_0.dtd">

<page-specification>
<component id="test" type="Insert">
<binding name="value" value="test"/>
</component>
</page-specification>

Home.html

<span jwcid="test" />

这个测试已经成功,如果大家还有什么更好的方法,麻烦教我一下,谢谢
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: