1.新建一个WEB工程
2.导入所需要的包,包的目录结构如下所示:
![]()
3.web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<!-- The bare minimum needed for JSF 2.0 is a servlet 2.5
declaration and the mapping for the FacesServlet.
Setting PROJECT_STAGE to Development is highly recommended
during initial development so that you get more helpful
error messages.
From JSF 2.0 tutorial at http://www.coreservlets.com/JSF-Tutorial/jsf2/
-->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<!-- Start the Spring listener that loads the application context
when the Web app starts up. It assumes the context is named
WEB-INF/applicationContext.xml unless you set a context param
called contextConfigLocation to override it. -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- Lets the bean definition file specify scopes of request
and session. -->
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
4.application.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config />
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<!-- 指定连接数据库的驱动 -->
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<!-- 指定连接数据库的 URL -->
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test" />
<!-- 指定连接数据库的用户名 -->
<property name="user" value="root" />
<!-- 指定连接数据库的密码 -->
<property name="password" value="123" />
<!-- 指定连接数据库连接池的最大连接数 -->
<property name="maxPoolSize" value="40" />
<!-- 指定连接数据库连接池的最小连接数 -->
<property name="minPoolSize" value="1" />
<!-- 指定连接数据库连接池的初始化连接数 -->
<property name="initialPoolSize" value="1" />
<!-- 指定连接数据库连接池的连接最大空闲时间 -->
<property name="maxIdleTime" value="20" />
</bean>
<!-- 定义 Hibernate 的 SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 依赖注入数据源,正是上文定义的 dataSource -->
<property name="dataSource" ref="dataSource" />
<!-- mappingResources 属性用来列出全部映射文件 -->
<property name="mappingResources">
<list>
<!-- 以下用来列出所有的 PO 映射文件 -->
<value>coreservlets/Person.hbm.xml</value>
</list>
</property>
<!-- 定义 Hibernate 的 SessionFactory 属性 -->
<property name="hibernateProperties">
<props>
<!-- 指定 Hibernate 的连接方言 -->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<!-- 配置启动应用时,是否根据 Hibernate 映射自动创建数据表 -->
<!-- <prop key="hibernate.hbm2ddl.auto">update</prop>-->
</props>
</property>
</bean>
<bean id="personDao" class="coreservlets.PersonDaoImpl" />
<bean id="formBean" class="coreservlets.CustomerBackingBean" scope="request"/>
</beans>
5.faces-config.xml配置
<?xml version="1.0"?>
<faces-config xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
version="2.0">
<application>
<variable-resolver>
org.springframework.web.jsf.DelegatingVariableResolver
</variable-resolver>
</application>
</faces-config>
6.页面文件customer-lookup.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html">
<h:head><title>Spring Bank: Balance Lookup</title>
</h:head>
<h:body >
<h:form >
<h:inputText value="#{formBean.id}" />
<h:commandButton value="Show Person"
action="#{formBean.findBalance}"/>
</h:form>
</h:body>
</html>
show-personName.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html">
<h:head><title>Spring Bank: Your Balance</title>
<link href="./css/styles.css"
rel="stylesheet" type="text/css"/>
</h:head>
<h:body style="#{formBean.colorPreferences.style}">
<table border="5" align="center">
<tr><th class="title">Spring Bank: Your Balance</th></tr>
</table>
<p/>
<ul>
<li>person:#{formBean.person.name}</li>
</ul>
</h:body>
</html>
7.java文件CustomerBackingBean.java
package coreservlets;
import javax.annotation.Resource;
public class CustomerBackingBean {
@Resource
private PersonDao personDao;
private Person person;
private int id;
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public PersonDao getPersonDao() {
return personDao;
}
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}
public String findBalance() {
person = personDao.get(id);
return ("show-balance");
}
}
Person.java
package coreservlets;
/**
* Person entity. @author MyEclipse Persistence Tools
*/
public class Person implements java.io.Serializable {
// Fields
/**
*
*/
private static final long serialVersionUID = 1L;
private Integer id;
private String name;
// Constructors
/** default constructor */
public Person() {
}
/** full constructor */
public Person(String name) {
this.name = name;
}
// Property accessors
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
PersonDao.java
package coreservlets;
public interface PersonDao {
public Person get(int id);
}
PersonDaoImpl.java
package coreservlets;
import javax.annotation.Resource;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;
public class PersonDaoImpl implements PersonDao {
private HibernateTemplate ht = null;
@Resource
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public HibernateTemplate getHibernateTemplate() {
if (ht == null) {
ht = new HibernateTemplate(sessionFactory);
}
return ht;
}
/* (non-Javadoc)
* @see lee.PersonDao#get(int)
*/
public Person get(int id){
Person p = (Person)getHibernateTemplate().get(Person.class,id);
return p;
}
}
8.Person.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">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="coreservlets.Person" table="person" catalog="test">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="increment" />
</id>
<property name="name" type="java.lang.String">
<column name="name" not-null="true" />
</property>
</class>
</hibernate-mapping>
一个很简单的功能,实现了JSF2.0+Spring+Hibernate的环境搭建
分享到:
相关推荐
通过整合JSF 2.0、Spring和Hibernate,开发者可以构建出具有强大功能、良好分层架构的Web应用,实现MVC模式,并有效地管理数据库操作和业务逻辑。这种组合在企业级项目中非常常见,因为它们各自都提供了许多开箱即用...
标题“JSF1.2+Spring2.0+Hibernate3.2的一个登陆实例”涉及到的是一个集成使用JavaServer Faces(JSF)、Spring框架和Hibernate ORM的登录应用示例。这个项目可能是一个教学资源或者开发者用来学习如何在旧版本的...
### Struts2.0 + Spring2.0 + Hibernate3.1 Web 应用知识点解析 #### 一、项目概述 本项目是一个基于Struts2.0、Spring2.0及Hibernate3.1的Web应用程序。它不仅实现了基本的CRUD(创建、读取、更新、删除)操作,...
在IT行业中,Web开发是至关重要的领域,而JavaServer Faces(JSF)、Spring和Hibernate是构建企业级Java Web应用程序的三大支柱。这个“JSF2.2.6+Spring + Hibernate整合可运行”项目,旨在提供一个完整的、可运行的...
在这个例子中,Spring主要负责管理JSF和Hibernate之间的协作,提供服务层的实现,如通过@Autowired注解自动注入依赖,以及通过@Transactional管理事务。 **Hibernate** Hibernate是一个强大的对象关系映射(ORM)...
在这个场景中,我们关注的是将JavaServer Faces (JSF),Spring,以及Hibernate这三大框架进行整合。这三者分别是用于构建用户界面、管理应用上下文和服务、以及处理持久化的强大工具。以下是对"JSF+Spring+Hibernate...
**Ajax、JSF、Spring和Hibernate是四种在Java Web开发中广泛应用的技术,它们共同构建了高效、灵活且功能强大的Web应用程序。** **Ajax(Asynchronous JavaScript and XML)** 是一种在无需重新加载整个网页的情况...
本主题将深入探讨如何使用JavaServer Faces (JSF)、Spring框架和Hibernate ORM工具来实现SQL Server数据库中的数据分页显示。 **JavaServer Faces (JSF)** JSF是一个Java标准,用于构建企业级的Web应用程序。它提供...
1. `src/main/java`:Java源代码,包括JSF Managed Beans、Spring配置和服务接口及实现、Hibernate实体类。 2. `src/main/resources`:资源文件,如Spring配置文件、Hibernate映射文件(XML或注解)、数据库连接配置...
6. **开发流程**:在这样的架构下,开发者通常会首先定义业务实体类(用Hibernate的注解或XML配置),然后创建Spring Bean来实现业务逻辑,接着在JSF页面上设计用户界面,通过Managed Beans与Spring Bean交互,最后...
在IT领域,JavaScript Server Faces(JSF)、Hibernate和Spring是三个非常重要的技术,它们分别用于构建用户界面、处理持久化和实现应用框架。本集成案例将深入探讨这三者的结合,帮助开发者理解如何在实际项目中...
**JCatalog电子商务系统案例**是基于Java技术栈的电商应用,它巧妙地融合了三个核心框架:JavaServer Faces (JSF)、Spring 和 Hibernate。这三个框架在Java开发领域都有着广泛的应用,它们各自承担着不同的职责,...
标题 "JSF+Spring+Hibernate 配置" 涉及到的是三个非常重要的Java Web开发框架的集成:JavaServer Faces (JSF),Spring框架,以及Hibernate ORM。这些技术组合在一起,可以创建出一个功能强大、灵活性高的企业级应用...
在IT行业中,`JSF (JavaServer Faces)`、`Spring`和`Hibernate`是三个非常重要的框架,它们分别用于构建用户界面、管理应用程序上下文和服务层数据持久化。本篇文章将详细探讨这三个技术的整合使用及其核心概念。 *...
【标题】"jsf+spring+hibernate示例,使用MyEclipse工具"涉及到的是一个集成开发环境MyEclipse中的Web应用项目,这个项目利用了JavaServer Faces(JSF)、Spring框架和Hibernate ORM技术来构建后端业务逻辑。JSF是...
总的来说,"jsf2+spring sample"项目展示了如何在同一个应用中利用JSF2的强大组件化UI特性和Spring的灵活业务处理能力,实现高效且易于维护的Web应用。通过学习这个示例,开发者可以更好地理解两者的协同工作方式,...
在JSF+Spring整合中,Spring可以管理Hibernate SessionFactory,并在需要时注入到Service层,实现透明的数据访问。 4. 分页查询: 在处理大量数据时,分页查询是提高用户体验和系统性能的重要手段。在JSF中,可以...
3. **数据访问对象(DAO)**:Spring的DAO模式与Hibernate的ORM结合,创建DAO接口和实现,用于执行数据库查询和事务。这些DAO可以通过Spring的@Autowired注解注入到JSF Managed Beans中。 4. **事务管理**:Spring...
集成这三个框架需要添加相应的JAR包,包括JSF的实现库(如Mojarra)、Spring的核心库、Hibernate的JAR以及它们的依赖库。开发者需要确保所有必要的库都已添加到项目的类路径中。 5. 应用流程: 用户通过JSF页面...