Posted on 2007-12-11 09:46
疯狂 阅读(442)
评论(0) 编辑 收藏
JSF与Spring整合的原理是获得彼此的上下文引用,以此进一步获得各自管理的bean,这是可能的,因为两者是Web应用框架都遵循Servlet规范,为二者整合提供了可能和基础。
1、在faces-config.xml中定义:
<application>
<variable-resolver>
org.springframework.web.jsf.DelegatingVariableResolver </variable-resolver>
</application>
|
这样jsf就能够找到spring注入的bean
2、在Backing Bean中定义如下例所示:
package com.web.jsf;
import com.manager.UserManager;
import com.model.User;
publicclass UserAction {
private User user;
private UserManager userManager;
public User getUser() {
if(user == null){
user = new User();
}
returnuser;
}
publicvoid setUser(User user) {
this.user = user;
}
public String add(){
if(user == null){
System.out.println("用户数据是空的");
}else{
System.out.println(user.getUsername());
System.out.println(user.getPassword());
}
userManager.addUser(user);
return"add_success";
}
public void setUserManager(UserManager userManager) {
this.userManager = userManager;
}
}
|
3、在对应的JSF Backing Bean配置中,如下定义这个属性的值:
<managed-bean>
<managed-bean-name>userAction</managed-bean-name>
<managed-bean-class>
com..web.jsf.UserAction
</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<managed-property>
<property-name>userManager</property-name>
<value>#{userManager}</value>//spring中的bean的id
</managed-property>
</managed-bean>
|
这样,在Backing Bean中就可以自由调用spring中的对象了。