您的位置:首页 > 编程语言 > Java开发

JSF2.0+Spring+Hibernate采用注解方式实现

2012-07-18 14:14 369 查看
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://hi
a898
bernate.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的环境搭建

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息