<!--
鼓励转载,请务必注明出处!
最近项目中用到jsf+spring+hibernate, 顺便做了个小例子,我已经调试通过了,分享给大家.
1.InfoBean
package com.jsf;
import com.vo.UserVO;
import com.web.bean.UserBean;
import com.bo.UserService;
import com.framework.web.bean.BaseManagedBean;
public class InfoBean extends BaseManagedBean {
// private static final Log log = LogFactory.getLog(InfoBean.class);
private static final long serialVersionUID = 1L;
private UserBean userBean = null;
private String username = null;
private String email = null;
private String response = null;
private long maximum = 0;
private long minimum = 0;
private boolean maximumSet = false;
private boolean minimumSet = false;
public InfoBean() {
}
public String submitPersonInfo() {
// log.info(username);
// log.info(email);
//jsf-spring 1 ok
// UserService userService = (UserService)getbean("userService");
//jsf-spring 2 ok
UserService userService = (UserService)getUserBean().getUserService();
UserVO userVO = new UserVO();
userVO.setUsername(username);
userVO.setEmail(email);
boolean flag = userService.saveUser(userVO);
if (flag) {
setResponse("register success");
return "success";
} else {
setResponse("register failed");
return "failure";
}
}
public UserBean getUserBean() {
if (userBean == null) {
userBean = (UserBean)getWebbean("UserBean");
}
return userBean;
}
public void setUserBean(UserBean userBean) {
this.userBean = userBean;
}
public void setMaximum(long maximum) {
this.maximum = maximum;
this.maximumSet = true;
}
public void setMinimum(long minimum) {
this.minimum = minimum;
this.minimumSet = true;
}
public void setResponse(String response) {
this.response = response;
}
public String getResponse() {
return null;
}
public long getMaximum() {
return maximum;
}
public long getMinimum() {
return minimum;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
2.UserService
package com.bo;
import com.vo.UserVO;
import org.springframework.dao.DataAccessException;
import java.io.Serializable;
public interface UserService extends Serializable {
public boolean saveUser(UserVO userVO) throws DataAccessException;
}
3.UserServiceImpl
package com.bo.impl;
import com.bo.UserService;
import com.dao.UserDAO;
import com.vo.UserVO;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.DataAccessException;
public class UserServiceImpl implements UserService {
private static final long serialVersionUID = 1L;
private static final Log log = LogFactory.getLog(UserServiceImpl.class);
private UserDAO userDAO;
public UserServiceImpl() {
log.info("UserServiceImpl()...................");
}
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
public boolean saveUser(UserVO userVO) throws DataAccessException {
return userDAO.saveUser(userVO);
}
public UserDAO getUserDAO() {
return userDAO;
}
}
4.UserDAO
package com.dao;
import org.springframework.dao.DataAccessException;
import com.vo.UserVO;
import java.io.Serializable;
public interface UserDAO extends Serializable{
public boolean saveUser(UserVO userVO)throws DataAccessException;
}
5.UserDAOImpl
package com.dao.impl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.vo.UserVO;
import com.dao.UserDAO;
import com.hibernate.UserInfo;
//import org.doomdark.uuid.UUIDGenerator;
public class UserDAOImpl extends HibernateDaoSupport implements UserDAO {
private static final Log log = LogFactory.getLog(UserDAOImpl.class);
public boolean saveUser(UserVO userVO) throws DataAccessException{
if(userVO == null){
return false;
}
UserInfo userInfo = new UserInfo();
//ui.setId(getID());
userInfo.setId("4");
userInfo.setUsername(userVO.getUsername().trim());
userInfo.setEmail(userVO.getEmail().trim());
this.getHibernateTemplate().save(userInfo);
return true;
}
// private String getID(){
// return UUIDGenerator.getInatance().generateTimeBaseUUID().toString();
// }
}
6.UserVO
package com.vo;
import java.io.Serializable;
public class UserVO implements Serializable{
private String username;
private String email;
public String getEmail(){
return email;
}
public void setEmail(String email){
this.email = email;
}
public String getUsername(){
return username;
}
public void setUsername(String username){
this.username = username;
}
}
7.BaseManagedBean
package com.framework.web.bean;
import java.io.Serializable;
import javax.faces.application.Application;
import javax.faces.context.FacesContext;
import javax.faces.el.ValueBinding;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.context.ApplicationContext;
import org.springframework.web.jsf.FacesContextUtils;
public class BaseManagedBean implements Serializable{
public static HttpServletRequest request;
public static HttpSession session;
static{
FacesContext context =FacesContext.getCurrentInstance();
request = (HttpServletRequest)context.getExternalContext().getRequest();
session = (HttpSession) context.getExternalContext().getSession(false);
}
public Object getbean(String beanName){
ApplicationContext ac = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
return ac.getBean(beanName);
}
public Object getWebbean(String beanname){
FacesContext context =FacesContext.getCurrentInstance();
Application app = context.getApplication();
ValueBinding binding = app.createValueBinding("#{" + beanname + "}");
Object object = binding.getValue(context);
return object;
}
public String getExternalParameter(String parameter){
FacesContext context =FacesContext.getCurrentInstance();
return context.getExternalContext().getInitParameter(parameter);
}
}
8.UserBean
package com.web.bean;
import com.bo.UserService;
public class UserBean {
private UserService userService = null;
public UserService getUserService() {
return userService;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
}
9.UserInfo
package com.hibernate;
import java.io.Serializable;
//import org.apache.commons.lang.builder.ToStringBuilder;
public class UserInfo implements Serializable{
private String id;
private String username;
private String email;
public UserInfo(String id,String username,String email){
this.id = id;
this.username = username;
this.email = email;
}
public UserInfo(){
}
public UserInfo(String id){
this.id = id;
}
public String getId(){
return id;
}
public void setId(String id){
this.id = id;
}
public String getEmail(){
return email;
}
public void setEmail(String email){
this.email = email;
}
public String getUsername(){
return username;
}
public void setUsername(String username){
this.username = username;
}
// public String toString(){
// return new ToStringBuilder(this).append("id",getId()).toString();
// }
}
10.UserInfo.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.hibernate.UserInfo" table="user">
<id name="id" type="java.lang.String" column="id">
<generator class="assigned" />
</id>
<property name="username" type="java.lang.String"
column="username" length="36">
</property>
<property name="email" type="java.lang.String" column="email"
length="60">
</property>
</class>
</hibernate-mapping>
11.web.xml
<?xml version="1.0"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>TestFrame</display-name>
<context-param>
<param-name>javax.faces.CONFIG_FILES</param-name>
<param-value>/WEB-INF/faces-config.xml</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<login-config>
<auth-method>BASIC</auth-method>
</login-config>
</web-app>
12.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
class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
<property name="driverClassName">
<value>org.hsqldb.jdbcDriver</value>
</property>
<property name="url">
<value>jdbc:hsqldb:hsql://localhost</value>
</property>
<property name="username">
<value>sa</value>
</property>
<property name="password">
<value/>
</property>
</bean>
<bean
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" id="sessionFactory">
<property name="dataSource">
<ref local="dataSource"/>
</property>
<property name="mappingResources">
<list>
<value>com/hibernate/UserInfo.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean
class="org.springframework.orm.hibernate3.HibernateTransactionManager" id="transactionManager">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<bean
class="org.springframework.transaction.interceptor.TransactionInterceptor" id="txInterceptor">
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="set*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean abstract="true"
class="org.springframework.aop.framework.ProxyFactoryBean" id="baseUserService">
<property name="interceptorNames">
<list>
<value>txInterceptor</value>
</list>
</property>
</bean>
<bean id="userService" parent="baseUserService">
<property name="proxyInterfaces">
<value>com.bo.UserService</value>
</property>
<property name="target">
<bean class="com.bo.impl.UserServiceImpl">
<property name="userDAO">
<ref bean="userDAO"/>
</property>
</bean>
</property>
</bean>
<bean class="com.dao.impl.UserDAOImpl" id="userDAO">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
</beans>
13.faces-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.0//EN"
"http://java.sun.com/dtd/web-facesconfig_1_0.dtd">
<faces-config>
<managed-bean>
<description>InfoBean</description>
<managed-bean-name>InfoBean</managed-bean-name>
<managed-bean-class>com.jsf.InfoBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
<managed-property>
<property-name>userBean</property-name>
<property-class>com.web.bean.UserBean</property-class>
<value>#{UserBean}</value>
</managed-property>
<managed-property>
<property-name>minimum</property-name>
<property-class>long</property-class>
<value>6</value>
</managed-property>
<managed-property>
<property-name>maximum</property-name>
<property-class>long</property-class>
<value>18</value>
</managed-property>
</managed-bean>
<managed-bean>
<managed-bean-name>UserBean</managed-bean-name>
<managed-bean-class>com.web.bean.UserBean</managed-bean-class>
<managed-bean-scope>application</managed-bean-scope>
<managed-property>
<property-name>userService</property-name>
<property-class>com.bo.UserService</property-class>
<value>#{userService}</value>
</managed-property>
</managed-bean>
<navigation-rule>
<description>JSF Home Page</description>
<from-view-id>/example/home.jsp</from-view-id>
<navigation-case>
<description>success</description>
<from-outcome>success</from-outcome>
<to-view-id>/example/success.jsp</to-view-id>
</navigation-case>
<navigation-case>
<description>failure</description>
<from-outcome>failure</from-outcome>
<to-view-id>/example/failure.jsp</to-view-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
<from-view-id>/example/success.jsp</from-view-id>
<navigation-case>
<from-outcome>su</from-outcome>
<to-view-id>/example/home.jsp</to-view-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
<from-view-id>/example/failure.jsp</from-view-id>
<navigation-case>
<from-outcome>su</from-outcome>
<to-view-id>/example/home.jsp</to-view-id>
</navigation-case>
</navigation-rule>
<application>
<variable-resolver>
org.springframework.web.jsf.DelegatingVariableResolver
</variable-resolver>
<locale-config>
<default-locale>zh_CN</default-locale>
</locale-config>
</application>
</faces-config>
14.faces-config.xml.bak_1
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.0//EN"
"http://java.sun.com/dtd/web-facesconfig_1_0.dtd">
<faces-config>
<managed-bean>
<description>InfoBean</description>
<managed-bean-name>InfoBean</managed-bean-name>
<managed-bean-class>com.jsf.InfoBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
<managed-property>
<property-name>minimum</property-name>
<property-class>long</property-class>
<value>6</value>
</managed-property>
<managed-property>
<property-name>maximum</property-name>
<property-class>long</property-class>
<value>18</value>
</managed-property>
</managed-bean>
<navigation-rule>
<description>JSF Home Page</description>
<from-view-id>/example/home.jsp</from-view-id>
<navigation-case>
<description>success</description>
<from-outcome>success</from-outcome>
<to-view-id>/example/success.jsp</to-view-id>
</navigation-case>
<navigation-case>
<description>failure</description>
<from-outcome>failure</from-outcome>
<to-view-id>/example/failure.jsp</to-view-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
<from-view-id>/example/success.jsp</from-view-id>
<navigation-case>
<from-outcome>su</from-outcome>
<to-view-id>/example/home.jsp</to-view-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
<from-view-id>/example/failure.jsp</from-view-id>
<navigation-case>
<from-outcome>su</from-outcome>
<to-view-id>/example/home.jsp</to-view-id>
</navigation-case>
</navigation-rule>
<application>
<variable-resolver>
org.springframework.web.jsf.DelegatingVariableResolver
</variable-resolver>
<locale-config>
<default-locale>zh_CN</default-locale>
</locale-config>
</application>
</faces-config>
15.home.jsp
<%@ page contentType="text/html; charset=gbk" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<html>
<head>
<title>
用户注册
</title>
</head>
<br>
<f:view>
<h:form id="helloForm" >
<table border="10" align="center"
bordercolor="#0099CC" cellpadding="6" bordercolorlight="#999999">
<tr>
<td colspan="2" bgcolor="#66CCFF">输入用户注册信息:</td>
</tr>
<tr>
<td>
<div align="right">用户名</div>
</td>
<td>
<h:inputText id="username" value="#{InfoBean.username}">
<f:validateLength minimum="#{InfoBean.minimum}" maximum="#{InfoBean.maximum}" />
</h:inputText>
</td>
</tr>
<tr>
<td>
<div align="right">E_mail</div>
</td>
<td>
<h:inputText id="email" value="#{InfoBean.email}"/>
</td>
</tr>
<tr>
<td colspan="2" bgcolor="#FFFF40">
<span>
<h:message id="message" for="username"/>
</span>
</td>
</tr>
<tr>
<td align="center" colspan="2">
<h:commandButton id="submit" action="#{InfoBean.submitPersonInfo}" value="提交" />
</td>
</tr>
</table>
</h:form>
</f:view>
</html>
16.success.jsp
<%@ page contentType="text/html; charset=gbk" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<html>
<head>
<title>
用户注册成功
</title>
</head>
<body bgcolor="white">
<f:view>
<h:form id="responseForm">
<h:graphicImage id="successImg"
url="images/form-success.jpg" alt="注册成功!"/>
<h2>
<h:outputText id="result"
value="#{InfoBean.response}"/></h2>
<h:commandButton id="back"
value="返回" action="su"/>
<p>
</h:form>
</f:view>
</html>
17.failure.jsp
<%@ page contentType="text/html; charset=gbk" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<html>
<head>
<title>
用户注册失败
</title>
</head>
<body bgcolor="white">
<f:view>
<h:form id="responseForm">
<h:graphicImage id="successImg"
url="images/form-error.jpg" alt="注册失败!"/>
<h2>
<h:outputText id="result"
value="#{InfoBean.response}"/></h2>
<h:commandButton id="back"
value="返回" action="su"/>
<p>
</h:form>
</f:view>
</html>
18.index.jsp
<html>
<head>
</head>
<body>
<jsp:forward page="/example/home.faces" />
</body>
</html>
-->
鼓励转载,请务必注明出处!
最近项目中用到jsf+spring+hibernate, 顺便做了个小例子,我已经调试通过了,分享给大家.
1.InfoBean
package com.jsf;
import com.vo.UserVO;
import com.web.bean.UserBean;
import com.bo.UserService;
import com.framework.web.bean.BaseManagedBean;
public class InfoBean extends BaseManagedBean {
// private static final Log log = LogFactory.getLog(InfoBean.class);
private static final long serialVersionUID = 1L;
private UserBean userBean = null;
private String username = null;
private String email = null;
private String response = null;
private long maximum = 0;
private long minimum = 0;
private boolean maximumSet = false;
private boolean minimumSet = false;
public InfoBean() {
}
public String submitPersonInfo() {
// log.info(username);
// log.info(email);
//jsf-spring 1 ok
// UserService userService = (UserService)getbean("userService");
//jsf-spring 2 ok
UserService userService = (UserService)getUserBean().getUserService();
UserVO userVO = new UserVO();
userVO.setUsername(username);
userVO.setEmail(email);
boolean flag = userService.saveUser(userVO);
if (flag) {
setResponse("register success");
return "success";
} else {
setResponse("register failed");
return "failure";
}
}
public UserBean getUserBean() {
if (userBean == null) {
userBean = (UserBean)getWebbean("UserBean");
}
return userBean;
}
public void setUserBean(UserBean userBean) {
this.userBean = userBean;
}
public void setMaximum(long maximum) {
this.maximum = maximum;
this.maximumSet = true;
}
public void setMinimum(long minimum) {
this.minimum = minimum;
this.minimumSet = true;
}
public void setResponse(String response) {
this.response = response;
}
public String getResponse() {
return null;
}
public long getMaximum() {
return maximum;
}
public long getMinimum() {
return minimum;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
2.UserService
package com.bo;
import com.vo.UserVO;
import org.springframework.dao.DataAccessException;
import java.io.Serializable;
public interface UserService extends Serializable {
public boolean saveUser(UserVO userVO) throws DataAccessException;
}
3.UserServiceImpl
package com.bo.impl;
import com.bo.UserService;
import com.dao.UserDAO;
import com.vo.UserVO;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.DataAccessException;
public class UserServiceImpl implements UserService {
private static final long serialVersionUID = 1L;
private static final Log log = LogFactory.getLog(UserServiceImpl.class);
private UserDAO userDAO;
public UserServiceImpl() {
log.info("UserServiceImpl()...................");
}
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
public boolean saveUser(UserVO userVO) throws DataAccessException {
return userDAO.saveUser(userVO);
}
public UserDAO getUserDAO() {
return userDAO;
}
}
4.UserDAO
package com.dao;
import org.springframework.dao.DataAccessException;
import com.vo.UserVO;
import java.io.Serializable;
public interface UserDAO extends Serializable{
public boolean saveUser(UserVO userVO)throws DataAccessException;
}
5.UserDAOImpl
package com.dao.impl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.vo.UserVO;
import com.dao.UserDAO;
import com.hibernate.UserInfo;
//import org.doomdark.uuid.UUIDGenerator;
public class UserDAOImpl extends HibernateDaoSupport implements UserDAO {
private static final Log log = LogFactory.getLog(UserDAOImpl.class);
public boolean saveUser(UserVO userVO) throws DataAccessException{
if(userVO == null){
return false;
}
UserInfo userInfo = new UserInfo();
//ui.setId(getID());
userInfo.setId("4");
userInfo.setUsername(userVO.getUsername().trim());
userInfo.setEmail(userVO.getEmail().trim());
this.getHibernateTemplate().save(userInfo);
return true;
}
// private String getID(){
// return UUIDGenerator.getInatance().generateTimeBaseUUID().toString();
// }
}
6.UserVO
package com.vo;
import java.io.Serializable;
public class UserVO implements Serializable{
private String username;
private String email;
public String getEmail(){
return email;
}
public void setEmail(String email){
this.email = email;
}
public String getUsername(){
return username;
}
public void setUsername(String username){
this.username = username;
}
}
7.BaseManagedBean
package com.framework.web.bean;
import java.io.Serializable;
import javax.faces.application.Application;
import javax.faces.context.FacesContext;
import javax.faces.el.ValueBinding;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.context.ApplicationContext;
import org.springframework.web.jsf.FacesContextUtils;
public class BaseManagedBean implements Serializable{
public static HttpServletRequest request;
public static HttpSession session;
static{
FacesContext context =FacesContext.getCurrentInstance();
request = (HttpServletRequest)context.getExternalContext().getRequest();
session = (HttpSession) context.getExternalContext().getSession(false);
}
public Object getbean(String beanName){
ApplicationContext ac = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
return ac.getBean(beanName);
}
public Object getWebbean(String beanname){
FacesContext context =FacesContext.getCurrentInstance();
Application app = context.getApplication();
ValueBinding binding = app.createValueBinding("#{" + beanname + "}");
Object object = binding.getValue(context);
return object;
}
public String getExternalParameter(String parameter){
FacesContext context =FacesContext.getCurrentInstance();
return context.getExternalContext().getInitParameter(parameter);
}
}
8.UserBean
package com.web.bean;
import com.bo.UserService;
public class UserBean {
private UserService userService = null;
public UserService getUserService() {
return userService;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
}
9.UserInfo
package com.hibernate;
import java.io.Serializable;
//import org.apache.commons.lang.builder.ToStringBuilder;
public class UserInfo implements Serializable{
private String id;
private String username;
private String email;
public UserInfo(String id,String username,String email){
this.id = id;
this.username = username;
this.email = email;
}
public UserInfo(){
}
public UserInfo(String id){
this.id = id;
}
public String getId(){
return id;
}
public void setId(String id){
this.id = id;
}
public String getEmail(){
return email;
}
public void setEmail(String email){
this.email = email;
}
public String getUsername(){
return username;
}
public void setUsername(String username){
this.username = username;
}
// public String toString(){
// return new ToStringBuilder(this).append("id",getId()).toString();
// }
}
10.UserInfo.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.hibernate.UserInfo" table="user">
<id name="id" type="java.lang.String" column="id">
<generator class="assigned" />
</id>
<property name="username" type="java.lang.String"
column="username" length="36">
</property>
<property name="email" type="java.lang.String" column="email"
length="60">
</property>
</class>
</hibernate-mapping>
11.web.xml
<?xml version="1.0"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>TestFrame</display-name>
<context-param>
<param-name>javax.faces.CONFIG_FILES</param-name>
<param-value>/WEB-INF/faces-config.xml</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<login-config>
<auth-method>BASIC</auth-method>
</login-config>
</web-app>
12.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
class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
<property name="driverClassName">
<value>org.hsqldb.jdbcDriver</value>
</property>
<property name="url">
<value>jdbc:hsqldb:hsql://localhost</value>
</property>
<property name="username">
<value>sa</value>
</property>
<property name="password">
<value/>
</property>
</bean>
<bean
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" id="sessionFactory">
<property name="dataSource">
<ref local="dataSource"/>
</property>
<property name="mappingResources">
<list>
<value>com/hibernate/UserInfo.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean
class="org.springframework.orm.hibernate3.HibernateTransactionManager" id="transactionManager">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<bean
class="org.springframework.transaction.interceptor.TransactionInterceptor" id="txInterceptor">
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="set*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean abstract="true"
class="org.springframework.aop.framework.ProxyFactoryBean" id="baseUserService">
<property name="interceptorNames">
<list>
<value>txInterceptor</value>
</list>
</property>
</bean>
<bean id="userService" parent="baseUserService">
<property name="proxyInterfaces">
<value>com.bo.UserService</value>
</property>
<property name="target">
<bean class="com.bo.impl.UserServiceImpl">
<property name="userDAO">
<ref bean="userDAO"/>
</property>
</bean>
</property>
</bean>
<bean class="com.dao.impl.UserDAOImpl" id="userDAO">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
</beans>
13.faces-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.0//EN"
"http://java.sun.com/dtd/web-facesconfig_1_0.dtd">
<faces-config>
<managed-bean>
<description>InfoBean</description>
<managed-bean-name>InfoBean</managed-bean-name>
<managed-bean-class>com.jsf.InfoBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
<managed-property>
<property-name>userBean</property-name>
<property-class>com.web.bean.UserBean</property-class>
<value>#{UserBean}</value>
</managed-property>
<managed-property>
<property-name>minimum</property-name>
<property-class>long</property-class>
<value>6</value>
</managed-property>
<managed-property>
<property-name>maximum</property-name>
<property-class>long</property-class>
<value>18</value>
</managed-property>
</managed-bean>
<managed-bean>
<managed-bean-name>UserBean</managed-bean-name>
<managed-bean-class>com.web.bean.UserBean</managed-bean-class>
<managed-bean-scope>application</managed-bean-scope>
<managed-property>
<property-name>userService</property-name>
<property-class>com.bo.UserService</property-class>
<value>#{userService}</value>
</managed-property>
</managed-bean>
<navigation-rule>
<description>JSF Home Page</description>
<from-view-id>/example/home.jsp</from-view-id>
<navigation-case>
<description>success</description>
<from-outcome>success</from-outcome>
<to-view-id>/example/success.jsp</to-view-id>
</navigation-case>
<navigation-case>
<description>failure</description>
<from-outcome>failure</from-outcome>
<to-view-id>/example/failure.jsp</to-view-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
<from-view-id>/example/success.jsp</from-view-id>
<navigation-case>
<from-outcome>su</from-outcome>
<to-view-id>/example/home.jsp</to-view-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
<from-view-id>/example/failure.jsp</from-view-id>
<navigation-case>
<from-outcome>su</from-outcome>
<to-view-id>/example/home.jsp</to-view-id>
</navigation-case>
</navigation-rule>
<application>
<variable-resolver>
org.springframework.web.jsf.DelegatingVariableResolver
</variable-resolver>
<locale-config>
<default-locale>zh_CN</default-locale>
</locale-config>
</application>
</faces-config>
14.faces-config.xml.bak_1
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.0//EN"
"http://java.sun.com/dtd/web-facesconfig_1_0.dtd">
<faces-config>
<managed-bean>
<description>InfoBean</description>
<managed-bean-name>InfoBean</managed-bean-name>
<managed-bean-class>com.jsf.InfoBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
<managed-property>
<property-name>minimum</property-name>
<property-class>long</property-class>
<value>6</value>
</managed-property>
<managed-property>
<property-name>maximum</property-name>
<property-class>long</property-class>
<value>18</value>
</managed-property>
</managed-bean>
<navigation-rule>
<description>JSF Home Page</description>
<from-view-id>/example/home.jsp</from-view-id>
<navigation-case>
<description>success</description>
<from-outcome>success</from-outcome>
<to-view-id>/example/success.jsp</to-view-id>
</navigation-case>
<navigation-case>
<description>failure</description>
<from-outcome>failure</from-outcome>
<to-view-id>/example/failure.jsp</to-view-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
<from-view-id>/example/success.jsp</from-view-id>
<navigation-case>
<from-outcome>su</from-outcome>
<to-view-id>/example/home.jsp</to-view-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
<from-view-id>/example/failure.jsp</from-view-id>
<navigation-case>
<from-outcome>su</from-outcome>
<to-view-id>/example/home.jsp</to-view-id>
</navigation-case>
</navigation-rule>
<application>
<variable-resolver>
org.springframework.web.jsf.DelegatingVariableResolver
</variable-resolver>
<locale-config>
<default-locale>zh_CN</default-locale>
</locale-config>
</application>
</faces-config>
15.home.jsp
<%@ page contentType="text/html; charset=gbk" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<html>
<head>
<title>
用户注册
</title>
</head>
<br>
<f:view>
<h:form id="helloForm" >
<table border="10" align="center"
bordercolor="#0099CC" cellpadding="6" bordercolorlight="#999999">
<tr>
<td colspan="2" bgcolor="#66CCFF">输入用户注册信息:</td>
</tr>
<tr>
<td>
<div align="right">用户名</div>
</td>
<td>
<h:inputText id="username" value="#{InfoBean.username}">
<f:validateLength minimum="#{InfoBean.minimum}" maximum="#{InfoBean.maximum}" />
</h:inputText>
</td>
</tr>
<tr>
<td>
<div align="right">E_mail</div>
</td>
<td>
<h:inputText id="email" value="#{InfoBean.email}"/>
</td>
</tr>
<tr>
<td colspan="2" bgcolor="#FFFF40">
<span>
<h:message id="message" for="username"/>
</span>
</td>
</tr>
<tr>
<td align="center" colspan="2">
<h:commandButton id="submit" action="#{InfoBean.submitPersonInfo}" value="提交" />
</td>
</tr>
</table>
</h:form>
</f:view>
</html>
16.success.jsp
<%@ page contentType="text/html; charset=gbk" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<html>
<head>
<title>
用户注册成功
</title>
</head>
<body bgcolor="white">
<f:view>
<h:form id="responseForm">
<h:graphicImage id="successImg"
url="images/form-success.jpg" alt="注册成功!"/>
<h2>
<h:outputText id="result"
value="#{InfoBean.response}"/></h2>
<h:commandButton id="back"
value="返回" action="su"/>
<p>
</h:form>
</f:view>
</html>
17.failure.jsp
<%@ page contentType="text/html; charset=gbk" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<html>
<head>
<title>
用户注册失败
</title>
</head>
<body bgcolor="white">
<f:view>
<h:form id="responseForm">
<h:graphicImage id="successImg"
url="images/form-error.jpg" alt="注册失败!"/>
<h2>
<h:outputText id="result"
value="#{InfoBean.response}"/></h2>
<h:commandButton id="back"
value="返回" action="su"/>
<p>
</h:form>
</f:view>
</html>
18.index.jsp
<html>
<head>
</head>
<body>
<jsp:forward page="/example/home.faces" />
</body>
</html>
-->