jsf+spring+hibernate

<!--

鼓励转载,请务必注明出处!

最近项目中用到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>

-->
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
  “蝙蝠在线考试系统”是由蝙蝠软件工作室历经一年半有余而自主研发的一款B/S结构软件,经过上千次的测试与BUG修补,在线下实体中已经广泛应用于各科研院所与高校中。蝙蝠在线考试系统基于JSF2、Spring3、JPA2等JAVA技术构建,系统使用MYSQL数据库,可以部署于WINDOWS、LINUX、UNIX等各种操作系统,内置了无处不在的AJAX能力,简洁易用,支持单选题、多选题、填空题、判断题、问答题、文件题六种题型。功能涵盖了试题模块管理、试题管理、题库导入导出、公式编辑、图片上传、考试管理、成绩管理、班级管理、考场管理、用户管理、信息发布与讨论区管理、管理员操作日志管理、用户个人文件管理等绝大多数在线考试管理功能。考试分为模拟考试与正式考试两种;选择题的选项既可随机也可以不随机;前台用户可以直接参加考试,根据后台设置自动生成试卷,考试页面倒计时,时间到自动提交试卷,前台用户还可以查询考试成绩和试卷的详细得分情况及试题解析;管理员还可以发布各类公告信息或设置讨论区,公告信息中可方便地发布文件和视频,可由用户回复与讨论,也可以直接通过讨论区组织小型培训。本系统可以运用于各类考试领域,适用性、扩展性、稳定性良好。 二、特色功能   相比一般的在线考试系统,本系统具有以下特色:   1、支持公式编辑与图片插入。本系统内置了公式编辑器,支持各类复杂数学公式的编辑,可以方便地添加和编辑各类包含数学公式的题目;本系统的题目中还可以插入PNG、GIF、JPEG格式的图片,适用于各类复杂的题目编辑场景。   2、支持题库的导入与导出。本系统中的题库可以导出成一定格式的Excel文件,方便备份保存;也可以从符合格式规范的Excel文件中直接将题库导入到系统中,大大方便题库的快速编辑。   3、企业级技术性能,支持大并发数据访问。本系统采用JAVA技术构建,中间层与数据访问层采用了SPRING框架和EclipseLink框架,采用了数据库二级缓存技术、大并发数据访问支持能力卓越,这种能力在多用户同时考试生成试卷和同时提交试卷并计算得分时非常重要,出现卡死的可能性较小。众所周知,JAVA卓越的企业级服务性能远远领先其它技术,绝非用PHP和ASP等解释型语言和技术构建的系统可比。若有需要,本系统可以采用WEBLOGIC服务器和ORACLE数据库部署集群应用,支持千万级用户访问。   4、更加人性化和科学化的考试界面。生成的试卷中具有标记功能,用户在考试时如果某些题目拿不准,可选择标记,题目会自动变为红色,方便用户考试;试卷头部会倒计时,时间到自动提交试卷;试卷在新窗口中打开,屏蔽了刷新按钮和快捷键,避免了多次生成试卷;考生关闭考试窗口时会提示是否确认退出。   5、重视与用户互动。内置了功能类似论坛的讨论区,可以用于信息发布、小型在线培训、话题讨论。后台中可以设置信息版面,既可以设置由用户自主发帖的版面,也可以设置只准用户回复但只允许版主发贴的版面;系统还自带了内部邮件功能,用户可以互相发站内信。任何系统都不可能完美,考试中出现的试题问题或系统问题可由管理人员和用户互动解决。   6、独立用户文件管理模块。本系统内置了独立的用户个人文件管理系统,功能类似于网易网盘,用户在发贴与讨论时可以方便地插入个文件系统中的图片和文件。   7、界面风格动态切换。本系统内置了39种CSS界面风格,实现了多种不同的个性化显示效果,39种总有一种适合,满足了多样化的用户需求,可以在后台中一键动态切换系统的显示风格。   8、高度可配置的管理权限。本系统后台功能模块的操作许可被分配在10种权限中,通过给管理员分配各种权限,可以方便地定制出命题教师、阅卷教师、监考教师、系统维护员等各种系统管理角色。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值