最近在做一个需要JSF和Spring整合的项目,之前不知道如何在后台管理Bean获取被Spring管理的业务类对象,现在解决了,可以跑起来了!
- web.xml
1 <?xml version="1.0"?>
2 <web-app xmlns="http://java.sun.com/xml/ns/j2ee"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
5 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
6 version="2.4">
7
8 <display-name>example</display-name>
9
10 <context-param>
11 <param-name>contextConfigLocation</param-name>
12 <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
13 </context-param>
14
15 <listener>
16 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
17 </listener>
18
19 <servlet>
20 <display-name>FacesServlet</display-name>
21 <servlet-name>FacesServlet</servlet-name>
22 <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
23 <load-on-startup>0</load-on-startup>
24 </servlet>
25
26 <servlet-mapping>
27 <servlet-name>FacesServlet</servlet-name>
28 <url-pattern>*.faces</url-pattern>
29 </servlet-mapping>
30
31 </web-app>
- faces-config.xml
1 <?xml version='1.0' encoding='UTF-8'?>
2 <!DOCTYPE faces-config PUBLIC
3 "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN"
4 "http://java.sun.com/dtd/web-facesconfig_1_1.dtd">
5 <faces-config>
6 <application>
7 <variable-resolver>
8 org.springframework.web.jsf.DelegatingVariableResolver
9 </variable-resolver>
10 </application>
12 <managed-bean>
13 <managed-bean-name>infoBean</managed-bean-name>
14 <managed-bean-class>
15 com.jam.managedbeans.InfoBean
16 </managed-bean-class>
17 <managed-bean-scope>request</managed-bean-scope>
18 </managed-bean>
19
20 </faces-config>
-
InfoBean.java
假如InfoBean中需要使用业务类UserService的方法,而UserService交由Spring管理(id="userService"),InfoBean可以这样写:
package com.jam.managedbeans;
import java.util.Map;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import org.springframework.context.ApplicationContext;
import org.springframework.web.jsf.FacesContextUtils;
import com.jam.hibeinate.po.Bloguser;
import com.jam.service.UserService;
/**
* @author Jam Chan
*
*/
public final class InfoBean extends Object {
/**
* 用户名
*/
private String userName;
/**
* 业务类
*/
private UserService userService;
/**
* 密码
*/
private String password;
/**
* 登陆验证
*
* @return
*/
public String loginValidate() {
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext extnContext = facesContext.getExternalContext();
// POJO类
Bloguser objUser = new Bloguser();
objUser.setUsername(getUserName());
objUser.setPassword(getPassword1());
// 从Spring中获取需要的业务类UserService,我就是在这里被卡住,终于OK了
ApplicationContext appContext = FacesContextUtils.getWebApplicationContext(facesContext);
userService = (UserService) appContext.getBean("userService");
// 验证用户
boolean result = userService.loginValidate(objUser);
// 获取SessionMap
Map sessionMap = extnContext.getSessionMap();
if (result) { // 登陆成功
/* 把用户名设置在Session中 */
sessionMap.put("userName", getUserName());
return "success";
} else { // 登录失败
/* 清空session */
if (sessionMap.containsKey("userName")) {
sessionMap.remove("userName");
}
return "failed";
}
}
// setter getter省略
暂时写到这,可能还要查漏补缺,发现问题继续加上!
posted on 2008-03-20 20:44
Jam Chan 阅读(1940)
评论(0) 编辑 收藏 所属分类:
SSH整合