Tapestry+spring+hibernate
1.
配置
tapestry
1)Web.xml
中配置
tapestry’s servlet
<
servlet
>
<
servlet-name
>
app
</
servlet-name
>
<
servlet-class
>
org.apache.tapestry.ApplicationServlet
</
servlet-class
>
</
servlet
>
<
servlet-mapping
>
<
servlet-name
>
app
</
servlet-name
>
<
url-pattern
>
/
</
url-pattern
>
</
servlet-mapping
>
Servlet mapping is associate with the file of hivemodule.xml
Note:first u couldn’t add the tapestry-spring.jar
2)config application file
<application
>
<meta
key
=
"org.apache.tapestry.messages-encoding"
value
=
"UTF-8"
/>
//encoding for tapestry
<meta
key
=
"org.apache.tapestry.page-class-packages"
value
=
"tapestry"
/>
//html file is
associate with
java file in the “tapestry” package
</application>
And the application file name must be defined the same as the servlet-class name.if u dun do that,u must declare in the application file like this “<application name=’project name’>”
3)write your html file and java file
Home.html:
<input jwcid=”@insert” value=”ongl:name”>
Home.java:(must extends BasePage)
Public absact String getName();
Public abstact void setName(String name);
Note:absact is better for the project.
Now u can run and see!
2.
hibernate+spring configuration
1)write a orm file---User.hbm.xml whatever u like(include a PK and name/pwd property).and also do a javabean for it.named User.java in domain package.
2)First add tapestry-spring.jar,and add under context into web.xml:
<
listener
>
<
listener-class
>
org.springframework.web.context.ContextLoaderListener
</
listener-class
>
</
listener
>
<
context-param
>
<
param-name
>
contextConfigLocation
</
param-name
>
<
param-value
>
/WEB-INF/application.xml
</
param-value
>
</
context-param
>
3)Then create
application.xml file:
<
bean
id
=
"sessionFactory"
class
=
"org.springframework.orm.hibernate3.LocalSessionFactoryBean"
>
<
property
name
=
"mappingResources"
>
<
list
>
<
value
>
hibernate/User.hbm.xml
</
value
>
</
list
>
</
property
>
<
property
name
=
"hibernateProperties"
>
<
props
>
<
prop
key
=
"hibernate.connection.driver_class"
>
oracle.jdbc.driver.OracleDriver
</
prop
>
//for oracle driver
<
prop
key
=
"hibernate.connection.url"
>
jdbc:oracle:thin:@localhost:1521:jseps
</
prop
>
<
prop
key
=
"hibernate.connection.username"
>
web
</
prop
>
<
prop
key
=
"hibernate.connection.password"
>
web
</
prop
>
<
prop
key
=
"hibernate.dialect"
>
org.hibernate.dialect.Oracle9Dialect
</
prop
>
<
prop
key
=
"hibernate.show_sql"
>
true
</
prop
>
<
prop
key
=
"hibernate.hbm2ddl.auto"
>
update
</
prop
>
//is the key word for auto create table in database by itself.
</
props
>
</
property
>
</
bean
>
4)and u can create a service interface and impl class files,one is in service.interface package and another in service.impl package.
public
interface
UserService {
void
create(User user);
void
update(User user);
void
delete(User user);
Collection query();
User load(Long id);
}
And UserServiceImpl.java must inject
sessionFactory
public
class
UserServiceImpl
implements
UserService {
private
SessionFactory
sessionFactory
;
public
void
setSessionFactory(SessionFactory sessionFactory) {
this
.
sessionFactory
= sessionFactory;
}
public
void
create(User user) {
HibernateTemplate ht =
new
HibernateTemplate(
this
.
sessionFactory
);
ht.save(user);
}
……………………………………………………….
}
Now u must add these into application.xml
<
bean
id
=
"userService"
class="service.impl.UserServiceImpl">
<
property
name
=
"sessionFactory
ref
=
"sessionFactory"
>
</
property
>
</
bean
>
5)
add a table on html
<
table
cellpadding
=
"0"
cellspacing
=
"0"
class
=
"grid"
width
=
"100%"
jwcid
=
"tableUser"
>
<
tr
jwcid
=
"@contrib:TableRows"
>
<
td
jwcid
=
"@contrib:TableValues"
/>
</
tr
>
</
table
>
Then Home.java will change:
@InjectObject
(
"spring:userService"
)
public
abstract
UserManager getUserManager();
//inject userService
@Component
(id =
"tableUser"
, type =
"contrib:Table"
, bindings = {
"columns=literal:!name,!pwd"
,
"row=currUser"
,
"source=userList"
})
public
abstract
IComponent getTableUser();
//for the tableUser component,see more on “http://tapestry.apache.org/tapestry4”
public
Collection getUserList(){
return
this
.getUserManager().query();
}
//for source
public
abstract
User getCurrUser();
public
abstract
void
setCurrUser(User user);
. . . . . . . . . . . . .
To make use of this library, you must include its JAR on the classpath (typically, by copying it into WEB-INF/lib), and extend your application specification, and add a <library> element :
<library id="contrib" specification-path="classpath:/org/apache/tapestry/contrib/
Contrib.library"/>
Now u can add some data in database,and run . . .see the list loaded from database!
3.
spring
事务
define a form in new page---create.html
<
form
jwcid
=
"@Form"
>
NAME:
<
input
jwcid
=
"@TextField"
value
=
"ognl:user.name"
/>
PWD:
<
input
jwcid
=
"@TextField"
value
=
"ognl:user.pwd"
hidden
=
"true"
/>
<
input
jwcid
=
"save"
value
=
"save"
/>
</
form
>
Add a method in create.java
public
String save(){
getUserManager().create(getUser());
return
"Home"
;
}
//can return a string type
And add these in application.xml:
<
bean
id
=
"txMgr"
class
=
"org.springframework.orm.hibernate3.HibernateTransactionManager"
>
<
property
name
=
"sessionFactory"
>
<
ref
bean
=
"sessionFactory"
/>
</
property
>
</
bean
>
<!-- the txInterceptor bean will get auto-wrapped around all beans matched with the combination of "txAdvisor"+"txAutoProxyCreator" -->
<
bean
id
=
"txInterceptor"
class
=
"org.springframework.transaction.interceptor.TransactionInterceptor"
>
<
property
name
=
"transactionManager"
>
<
ref
bean
=
"txMgr"
/>
</
property
>
<
property
name
=
"transactionAttributes"
>
<
props
>
<
prop
key
=
"*"
>
PROPAGATION_REQUIRED
</
prop
>
</
props
>
</
property
>
</
bean
>
<!-- the txAdvisor advises all matched methods in a given bean against its patterns with its interceptor -->
<
bean
id
=
"txAdvisor"
class
=
"org.springframework.aop.support.RegexpMethodPointcutAdvisor"
>
<
property
name
=
"advice"
>
<
ref
bean
=
"txInterceptor"
/>
</
property
>
<
property
name
=
"patterns"
>
<!-- the patterns must match *fully qualified* method names, hence the .* -->
<
list
>
<
value
>
.*
</
value
>
<!—-
将
.*
的方法包括在
TRANSACTION
中
-->
</
list
>
</
property
>
</
bean
>
<!-- the txProxyAutoCreator automagically proxies all matched beans with its interceptors if they match any pointcuts -->
<
bean
id
=
"txProxyAutoCreator"
class
=
"org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"
>
<
property
name
=
"beanNames"
>
<
list
>
<!-- managers -->
<
value
>
*Manager
</
value
>
<
value
>
*Service
</
value
>
<!—-
与以上命名的类文件匹配
-->
</
list
>
</
property
>
<
property
name
=
"interceptorNames"
>
<
list
>
<
value
>
txAdvisor
</
value
>
</
list
>
</
property
>
</
bean
>