趁着测试空闲,学习struts2.0+spring2.0+hibernate3.2的整合,对这3种技术都只有最基本的了解,网上查了N多资料,找到一篇介绍整合的文章,不过用的是hibernate3.1。
资料链接如下:hibernate3.1http://blog.sina.com.cn/s/blog_4c5ce20701000b7m.html
1. 新建web工程:
[img]http://shiningwu.iteye.com/upload/picture/pic/12421/2c42b467-8f44-3220-aed1-6b642db43162.bmp [/img]
点击“finish”。
2. 加入spring2组件
[img]http://shiningwu.iteye.com/upload/picture/pic/12437/7123801b-f9f2-3249-a9e9-ce09ee652037.bmp [/img]
选择Spring 2.0 Core Lobraries、Spring 2.0 Testing Support Libraries、Spring 2.0 Web Libraries、Hibernate 3.2 Core Libraries、Hibernate 3.2 Annotations & Entity Manager、Hibernate 3.2 Advanced Support Libraries包 。
选择Copy checked Library contents to project folder(TLDs always copied),点击“next”;[img]http://shiningwu.iteye.com/upload/picture/pic/12435/43107a35-b0e5-31d1-8134-bd4e9674bc32.bmp [/img]
点击“finish”。
3. 手工加入需要的jar文件。
需要手工添加一些jar文件到WEB-INF/lib里面,不加这些文件,加入hibernate组件后会报错。
commons-dbcp.jar
spring-hibernate.jar
spring-dao.jar
spring-jdbc.jar
commons-pool.jar
commons-collections.jar
struts2-spring-plugin-2.0.11.1.jar
spring-orm.jar
同时删除commons-collections2.11.jar
4. 加入hibernate组件。
[img]http://shiningwu.iteye.com/upload/picture/pic/12433/d8e96d08-07d5-3f18-b0cd-14dbe2fcac2b.bmp [/img]
由于eclipse6.0不支持hibernate3.2,刚刚在添加spring2.0组件时已加入hibernate3.2的jar文件,在这里就不添加hibernate3.1的jar文件。选择Copy checked Library contents to project folder(TLDs always copied),点击“next”;
[img]http://shiningwu.iteye.com/upload/picture/pic/12431/55d58d8f-bde5-354e-99e3-f10711230e5e.bmp [/img]
选择Srping configuration file(applicationContext.xml),使用spring管理hibernate,点击“next”;
[img]http://shiningwu.iteye.com/upload/picture/pic/12429/1083558e-001e-3076-aede-f29f5103500c.bmp [/img]
选择Existing Spring configuration file,填写SessionFactory ID,点击“next”;
[img]http://shiningwu.iteye.com/upload/picture/pic/12447/f1bd99c7-6f3b-33e0-97b0-6d11972381ae.bmp [/img]
填写DataSource名称,选择DB Driver,点击“next”;
[img]http://shiningwu.iteye.com/upload/picture/pic/12445/b343f79f-078d-3afc-a8e0-31e4ff07a40a.bmp [/img]
取消Create SessionFactory class选项,点击“finish”;
applicationContext.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.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url"
value="jdbc:mysql://192.168.151.238:3306/chgl">
</property>
<property name="username" value="vcom"></property>
<property name="password" value="vcom_8968888"></property>
</bean>
<bean id="SessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="DataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
</bean>
</beans>
5. 加入struts2.0组件
加入struts2.0的jar文件
commons-logging-1.0.4.jar
freemarker-2.3.8.jar
ognl-2.6.11.jar
struts2-core-2.0.8.jar
xwork-2.0.3.jar
6. 修改web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
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-app_2_5.xsd">
<display-name>test</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
7. 建立映射
选择Window-Open perspective-MyEclipse Hibernate,选择使用的DB Driver,右键点击,选择“open connection…”
[img]http://shiningwu.iteye.com/upload/picture/pic/12443/06f3b3b8-617a-37f0-bc79-b1dc67da2e22.bmp [/img]
选择要使用的表,右键点击,选择“Hibernate Reverse Engineering”;
[img]http://shiningwu.iteye.com/upload/picture/pic/12441/be787f3e-3a4e-36e8-977f-e6b40da97d04.bmp [/img]
点击“next“
[img]http://shiningwu.iteye.com/upload/picture/pic/12439/9f34bedb-db53-3b49-95fc-971260a81ac4.bmp [/img]
也可填写“sequence “ or ”assigned“,点击“next“
[img]http://shiningwu.iteye.com/upload/picture/pic/12453/97b3852c-a42d-34fc-9277-11e912cf96a2.bmp [/img]
点击“finish“。
到这里,完成了struts2.0+spring2.0+hibernate3.2的基本配置。
8. 编写action及struts.xml
package com.vcom.login;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.opensymphony.xwork2.ActionSupport;
import com.vcom.hibernate.XtYhxx;
import com.vcom.hibernate.XtYhxxDAO;
public class Login extends ActionSupport{
private XtYhxxDAO xtyhxxDAO;
private String username;
private int rowid;
private String password;
private String message;
private ApplicationContext context;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public XtYhxxDAO getXtyhxxDAO() {
return xtyhxxDAO;
}
public void setXtyhxxDAO(XtYhxxDAO xtyhxxDAO) {
this.xtyhxxDAO = xtyhxxDAO;
}
public int getRowid() {
return rowid;
}
public void setRowid(int rowid) {
this.rowid = rowid;
}
public ApplicationContext getContext() {
return context;
}
public void setContext(ApplicationContext context) {
this.context = context;
}
public String execute() {
XtYhxx xtyhxx = new XtYhxx();
//XtYhxxDAO xtyxxdao=new XtYhxxDAO();
//context=new ClassPathXmlApplicationContext("applicationContext.xml");
//XtYhxxDAO xtyxxdao=(XtYhxxDAO)context.getBean("XtYhxxDAO");
xtyhxx.setYhdm(username);
//xtyhxx.setYhmm(password);
xtyhxx.setRowid(rowid);
message ="Welcome, "+ xtyhxx.getRowid()+","+xtyhxx.getYhdm();
Session session=xtyhxxDAO.getHibernateTemplate().getSessionFactory().openSession();
try{
//xtyhxxDAO.save(xtyhxx);
//xtyhxxDAO.delete(xtyhxx);
//XtYhxx aa=xtyhxxDAO.findById(rowid);
//System.out.println("------"+aa.getYhdm());
//Example example = Example.create(xtyhxx);
//List list=xtyxxdao.findByExample(xtyhxx);
//Criteria criteria = session.createCriteria(XtYhxx.class).add(example);
//List list = criteria.list();
Map map = new HashMap();
map.put("yhdm", username);
map.put("yhmm", password);
map.put("rowid", rowid);
List list=session.createCriteria(XtYhxx.class).add(Restrictions.allEq(map)).list();
//System.out.println("------"+list.size());
if(list.size()==0) message ="Invalid user or password";
}catch (Exception ex){
ex.printStackTrace();
return ActionSupport.INPUT;
}finally {
if (session.isOpen()) {
session.close();
}
}
return SUCCESS;
}
}
struts.xml
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- <constant name="struts.objectFactory" value="spring" /> 是在后面的spring2配置中用到意思是把控制交给spring2 -->
<constant name="struts.objectFactory" value="spring" />
<include file="struts-default.xml"></include>
<package name="com.vcom.login" extends="struts-default">
<!-- <action name="Login" class="Login"> 这里用class来关联spring2配置文件中的配置关联 -->
<action name="Login" class="LoginAction">
<result>/login/login.jsp</result>
<result name="INPUT">/login/login.jsp</result>
</action>
</package>
</struts>
9. 修改applicationContext.xml
加入
<bean id="LoginAction" class="com.vcom.login.Login" >
<property name="xtyhxxDAO">
<ref bean="XtYhxxDAO" />
</property>
</bean>
10. 编写login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<table width="300" border="0">
<s:form action="Login" theme="simple">
<tr>
<td width=50%>用户名:</td>
<td width=50%><s:textfield name="username" /></td>
</tr>
<tr>
<td width=50%>密码:</td>
<td width=50%><s:textfield name="password" /></td>
</tr>
<tr><td colspan=2 align=center width=100%><s:submit /></td></tr>
<tr>
<td colspan=2 align=center width=100%><s:property value ="message"/></td>
</tr>
</s:form>
</table>
</body>
</html>
11. 新建DB Driver
选择Window-Open perspective-MyEclipse Hibernate,在左边DB Browser中点击右键-new
[img]http://shiningwu.iteye.com/upload/picture/pic/12451/64572ad8-e483-3ab0-86be-8d8f31a21dc0.bmp [/img]
点击“next“;
[img]http://shiningwu.iteye.com/upload/picture/pic/12449/4cd6e96d-7960-322e-a7a0-edee730ab986.bmp [/img]
点击“finish“。
12. 需要注意的事项
1. WEB-INF中
<context-param>
<param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
applicationContext.xml的路径必须和实际一致,applicationContext.xml建议放在classes下,这样在实际引用时不用写绝对路径。
2. struts.xml中
<action name="Login" class="LoginAction">
<result>/login/login.jsp</result>
<result name="INPUT">/login/login.jsp</result>
</action>
Action中成功返回success,在struts.xml中不能写成<result name=”success”>/login/login.jsp</result>,会报找不到result success的异常。