源文件结构:
Web文件结构:
1 UserBean.java
package
officeautomatic;
import javax.faces.event.ActionEvent;
public class UserBean ... {
private Integer id;
private String name;
private String pass;
private String err;
private UserDAO userDAO;
private String outcome;
public void setId(Integer id) ...{
this.id = id;
}
public Integer getId() ...{
return id;
}
public void setName(String name) ...{
this.name = name;
}
public String getName() ...{
return name;
}
public void setPass(String pass) ...{
this.pass = pass;
}
public String getPass() ...{
return pass;
}
public void setErr(String err) ...{
this.err = err;
}
public String getErr() ...{
return err;
}
public void setUserDAO(UserDAO userDAO) ...{
this.userDAO = userDAO;
}
public UserDAO getUserDAO() ...{
return userDAO;
}
public void checkValue(ActionEvent e) ...{
if((getUserDAO().find(id) != null) && pass.equals(getUserDAO().find(new Integer(id)).getPass()))...{
setName(getUserDAO().find(new Integer(id)).getName());
outcome = "success";
}
else...{
setErr("error!");
outcome = "failure";
}
}
public String outcome()...{
return outcome;
}
}
import javax.faces.event.ActionEvent;
public class UserBean ... {
private Integer id;
private String name;
private String pass;
private String err;
private UserDAO userDAO;
private String outcome;
public void setId(Integer id) ...{
this.id = id;
}
public Integer getId() ...{
return id;
}
public void setName(String name) ...{
this.name = name;
}
public String getName() ...{
return name;
}
public void setPass(String pass) ...{
this.pass = pass;
}
public String getPass() ...{
return pass;
}
public void setErr(String err) ...{
this.err = err;
}
public String getErr() ...{
return err;
}
public void setUserDAO(UserDAO userDAO) ...{
this.userDAO = userDAO;
}
public UserDAO getUserDAO() ...{
return userDAO;
}
public void checkValue(ActionEvent e) ...{
if((getUserDAO().find(id) != null) && pass.equals(getUserDAO().find(new Integer(id)).getPass()))...{
setName(getUserDAO().find(new Integer(id)).getName());
outcome = "success";
}
else...{
setErr("error!");
outcome = "failure";
}
}
public String outcome()...{
return outcome;
}
}
2 IUserDao.java
package
officeautomatic;
public interface IUserDAO ... {
public UserBean find(Integer id);
}
public interface IUserDAO ... {
public UserBean find(Integer id);
}
3 UserDao.java
package
officeautomatic;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
public class UserDAO implements IUserDAO ... {
private SessionFactory sessionFactory;
public UserDAO() ...{ }
public UserDAO(SessionFactory sessionFactory) ...{
this.setSessionFactory(sessionFactory);
}
public void setSessionFactory(SessionFactory sessionFactory) ...{
this.sessionFactory = sessionFactory;
}
public UserBean find(Integer id) ...{
Session session = sessionFactory.openSession();
UserBean userBean = (UserBean) session.get(UserBean.class, id);
Hibernate.initialize(userBean);
session.close();
return userBean;
}
}
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
public class UserDAO implements IUserDAO ... {
private SessionFactory sessionFactory;
public UserDAO() ...{ }
public UserDAO(SessionFactory sessionFactory) ...{
this.setSessionFactory(sessionFactory);
}
public void setSessionFactory(SessionFactory sessionFactory) ...{
this.sessionFactory = sessionFactory;
}
public UserBean find(Integer id) ...{
Session session = sessionFactory.openSession();
UserBean userBean = (UserBean) session.get(UserBean.class, id);
Hibernate.initialize(userBean);
session.close();
return userBean;
}
}
4 index.jsp
<%
...
@ page language="java" contentType="text.html; charset=BIG5"
pageEncoding="BIG5" %>
<% ... @taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<% ... @taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
< html >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=BIG5" >
< title > Login </ title >
</ head >
< body >
< f:view locale ="zh_CN" >
< h:form id ="loginForm" >
< h:message for ="userNameInput" ></ h:message >
< h:outputText value ="#{userBean.err}" ></ h:outputText >
< p >
< h:outputLabel for ="userNameInput" >
< h:outputText id ="userNameLabel" value ="User ID:" ></ h:outputText >
</ h:outputLabel >
< h:inputText id ="userNameInput" value ="#{userBean.id}" required ="true" ></ h:inputText >
</ p >
< p >
< h:outputLabel for ="passwordInput" >
< h:outputText id ="passwordLabel" value ="Password:" ></ h:outputText >
</ h:outputLabel >
< h:inputSecret id ="passwordInput" value ="#{userBean.pass}" required ="true" ></ h:inputSecret >
</ p >
< h:commandButton id ="submitButton" value ="Submmit" actionListener ="#{userBean.checkValue}" action ="#{userBean.outcome}" ></ h:commandButton >
</ h:form >
</ f:view >
</ body >
</ html >
pageEncoding="BIG5" %>
<% ... @taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<% ... @taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
< html >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=BIG5" >
< title > Login </ title >
</ head >
< body >
< f:view locale ="zh_CN" >
< h:form id ="loginForm" >
< h:message for ="userNameInput" ></ h:message >
< h:outputText value ="#{userBean.err}" ></ h:outputText >
< p >
< h:outputLabel for ="userNameInput" >
< h:outputText id ="userNameLabel" value ="User ID:" ></ h:outputText >
</ h:outputLabel >
< h:inputText id ="userNameInput" value ="#{userBean.id}" required ="true" ></ h:inputText >
</ p >
< p >
< h:outputLabel for ="passwordInput" >
< h:outputText id ="passwordLabel" value ="Password:" ></ h:outputText >
</ h:outputLabel >
< h:inputSecret id ="passwordInput" value ="#{userBean.pass}" required ="true" ></ h:inputSecret >
</ p >
< h:commandButton id ="submitButton" value ="Submmit" actionListener ="#{userBean.checkValue}" action ="#{userBean.outcome}" ></ h:commandButton >
</ h:form >
</ f:view >
</ body >
</ html >
5 welcome.jsp
<%
...
@ page language="java" contentType="text.html; charset=BIG5"
pageEncoding="BIG5" %>
<% ... @taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<% ... @taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
< html >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=BIG5" >
< title > welcome </ title >
</ head >
< body >
< f:view >
< h:outputText value ="#{userBean.name}" />
< h3 > login successfully </ h3 >
</ f:view >
</ body >
</ html >
pageEncoding="BIG5" %>
<% ... @taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<% ... @taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
< html >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=BIG5" >
< title > welcome </ title >
</ head >
< body >
< f:view >
< h:outputText value ="#{userBean.name}" />
< h3 > login successfully </ h3 >
</ f:view >
</ body >
</ html >
6 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 >
< application >
< variable-resolver >
org.springframework.web.jsf.DelegatingVariableResolver
</ variable-resolver >
</ application >
< navigation-rule >
< from-view-id > /index.jsp </ from-view-id >
< navigation-case >
< from-outcome > success </ from-outcome >
< to-view-id > /welcome.jsp </ to-view-id >
</ navigation-case >
< navigation-case >
< from-outcome > failure </ from-outcome >
< to-view-id > /index.jsp </ to-view-id >
</ navigation-case >
</ navigation-rule >
< managed-bean >
< managed-bean-name > userBean </ managed-bean-name >
< managed-bean-class > officeautomatic.UserBean </ managed-bean-class >
< managed-bean-scope > session </ managed-bean-scope >
< managed-property >
< property-name > userDAO </ property-name >
< value > #{userDAO} </ value >
</ managed-property >
</ managed-bean >
</ faces-config >
<! 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 >
< application >
< variable-resolver >
org.springframework.web.jsf.DelegatingVariableResolver
</ variable-resolver >
</ application >
< navigation-rule >
< from-view-id > /index.jsp </ from-view-id >
< navigation-case >
< from-outcome > success </ from-outcome >
< to-view-id > /welcome.jsp </ to-view-id >
</ navigation-case >
< navigation-case >
< from-outcome > failure </ from-outcome >
< to-view-id > /index.jsp </ to-view-id >
</ navigation-case >
</ navigation-rule >
< managed-bean >
< managed-bean-name > userBean </ managed-bean-name >
< managed-bean-class > officeautomatic.UserBean </ managed-bean-class >
< managed-bean-scope > session </ managed-bean-scope >
< managed-property >
< property-name > userDAO </ property-name >
< value > #{userDAO} </ value >
</ managed-property >
</ managed-bean >
</ faces-config >
7 beans-config.xml
<?
xml version="1.0" encoding="UTF-8"
?>
< beans xmlns ="http://www.springframework.org/schema/beans"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation ="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd" >
< bean id ="dataSource" class ="org.springframework.jdbc.datasource.DriverManagerDataSource" >
< property name ="driverClassName" value ="com.mysql.jdbc.Driver" />
< property name ="url" value ="jdbc:mysql://localhost:3306/cie" />
< property name ="username" value ="root" />
< property name ="password" value ="123456" />
</ bean >
< bean id ="sessionFactory"
class ="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
destroy-method ="close" >
< property name ="dataSource" ref ="dataSource" />
< property name ="mappingResources" >
< list >
< value > officeautomatic/UserBean.hbm.xml </ value >
</ list >
</ property >
< property name ="hibernateProperties" >
< props >
< prop key ="hibernate.dialect" >
org.hibernate.dialect.MySQLDialect
</ prop >
</ props >
</ property >
</ bean >
< bean id ="userDAO" class ="officeautomatic.UserDAO" >
< property name ="sessionFactory" ref ="sessionFactory" ></ property >
</ bean >
</ beans >
< beans xmlns ="http://www.springframework.org/schema/beans"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation ="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd" >
< bean id ="dataSource" class ="org.springframework.jdbc.datasource.DriverManagerDataSource" >
< property name ="driverClassName" value ="com.mysql.jdbc.Driver" />
< property name ="url" value ="jdbc:mysql://localhost:3306/cie" />
< property name ="username" value ="root" />
< property name ="password" value ="123456" />
</ bean >
< bean id ="sessionFactory"
class ="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
destroy-method ="close" >
< property name ="dataSource" ref ="dataSource" />
< property name ="mappingResources" >
< list >
< value > officeautomatic/UserBean.hbm.xml </ value >
</ list >
</ property >
< property name ="hibernateProperties" >
< props >
< prop key ="hibernate.dialect" >
org.hibernate.dialect.MySQLDialect
</ prop >
</ props >
</ property >
</ bean >
< bean id ="userDAO" class ="officeautomatic.UserDAO" >
< property name ="sessionFactory" ref ="sessionFactory" ></ property >
</ bean >
</ beans >
8 UserBean.hbm.xml
<?
xml version="1.0" encoding="UTF-8"
?>
<! DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
< hibernate-mapping >
< class name ="officeautomatic.UserBean" table ="user" >
< id name ="id" column ="id" >
< generator class ="native" ></ generator >
</ id >
< property name ="name" column ="username" ></ property >
< property name ="pass" column ="password" ></ property >
</ class >
</ hibernate-mapping >
<! DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
< hibernate-mapping >
< class name ="officeautomatic.UserBean" table ="user" >
< id name ="id" column ="id" >
< generator class ="native" ></ generator >
</ id >
< property name ="name" column ="username" ></ property >
< property name ="pass" column ="password" ></ property >
</ class >
</ hibernate-mapping >
9 web.xml
<?
xml version="1.0" encoding="UTF-8"
?>
< web-app xmlns ="http://java.sun.com/xml/ns/javaee"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" version ="2.5"
xsi:schemaLocation ="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" >
< session-config >
< session-timeout > 30 </ session-timeout >
</ session-config >
< servlet >
< servlet-name > context </ servlet-name >
< servlet-class >
org.springframework.web.context.ContextLoaderServlet
</ servlet-class >
< load-on-startup > 1 </ load-on-startup >
</ servlet >
< listener >
< listener-class >
org.apache.myfaces.webapp.StartupServletContextListener
</ listener-class >
</ listener >
< context-param >
< param-name > contextConfigLocation </ param-name >
< param-value > /WEB-INF/applicationContext.xml </ param-value >
</ context-param >
< context-param >
< param-name > javax.faces.CONFIG_FILES </ param-name >
< param-value > /WEB-INF/faces-config.xml </ param-value >
</ context-param >
< servlet >
< servlet-name > Faces Servlet </ servlet-name >
< servlet-class > javax.faces.webapp.FacesServlet </ servlet-class >
< load-on-startup > 0 </ load-on-startup >
</ servlet >
< servlet-mapping >
< servlet-name > Faces Servlet </ servlet-name >
< url-pattern > *.faces </ url-pattern >
</ servlet-mapping >
</ web-app >
< web-app xmlns ="http://java.sun.com/xml/ns/javaee"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" version ="2.5"
xsi:schemaLocation ="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" >
< session-config >
< session-timeout > 30 </ session-timeout >
</ session-config >
< servlet >
< servlet-name > context </ servlet-name >
< servlet-class >
org.springframework.web.context.ContextLoaderServlet
</ servlet-class >
< load-on-startup > 1 </ load-on-startup >
</ servlet >
< listener >
< listener-class >
org.apache.myfaces.webapp.StartupServletContextListener
</ listener-class >
</ listener >
< context-param >
< param-name > contextConfigLocation </ param-name >
< param-value > /WEB-INF/applicationContext.xml </ param-value >
</ context-param >
< context-param >
< param-name > javax.faces.CONFIG_FILES </ param-name >
< param-value > /WEB-INF/faces-config.xml </ param-value >
</ context-param >
< servlet >
< servlet-name > Faces Servlet </ servlet-name >
< servlet-class > javax.faces.webapp.FacesServlet </ servlet-class >
< load-on-startup > 0 </ load-on-startup >
</ servlet >
< servlet-mapping >
< servlet-name > Faces Servlet </ servlet-name >
< url-pattern > *.faces </ url-pattern >
</ servlet-mapping >
</ web-app >