折騰了好幾天,終於把SSH2的框架搭起來,期間經歷的興奮,痛苦,猶豫...最終是領悟:太完美的要求,其實就是拖延...所以在以下的jar包的選擇上,跟前面幾篇的原則有了很大的區別:
- Struts包保持原樣。
- Spring 和hibernate的包按最大的導入,因爲少了個jar文件導致的錯誤查找,實在是這兩天最痛苦的事情。
- Hibernate從4.0降到3.6.8,原因是Hibernate4改變太大,我又不想在期初用Maven來更新Jar包.
上一篇中提到怎麼整合Struts2和hibernate,接下來,到了最最最關鍵的時刻,要把Spring融合進來...
Spring在MVC中的作用應該是C的功能,提供了一個對象的容器...
言歸正傳. 我們需要開始引入幾個Spring的包.
1. 下載Hibernate3.6.8的包,解壓後copy requred目錄下的所有的文件到lib目錄下,注意這裏有一個antlr2.7.7.jar的, 要替換掉原先struts引入的那個低版本(2.7.2)的jar文件。保險一點把jpa目錄下的那個jpa的jar包也copy(hibernate-jpa-2.0-api-1.0.1.Final.jar)
2. 導入struts2-spring-plugin-2.2.3.1.jar (這個在Struts2的包裏面找),copy到WEB-INF/lib目錄下
3. 導入Spring的一些Jar包 : 導入spring 的dist 目錄下的所有的jar文件 接下來我們比較一下加入Spring有什麼變化:
- 配置web.xml, -- 一些Struts和Spring的啓動項會在這裏配置
- 配置ApplicationContext.xml -- 刪除hibernate.cfg.xml, 將配置挪到這裏來做(增加SessionFactory和dataSource的設置),也就是由Spring來接管
- 增加POJO類和Hibernate的ORM映射文件(xxx.hbm.xml) -- 個人建議,如果Table間的關係比較簡單,用annotation來避免重複維護ORM映射文件. 但是這裏還是按照傳統的做法。
- 編寫DAO接口和DAO實現類 -- 將接口和實現類分離的好處就不用我講了,(OO設計原則:將類的行爲和實現分離,面向接口編程)
- 將DAO的實現類的配置增加到ApplicationContext.xml中 -- 注意,Application中的Bean都是class,而不是interface.
- 增加業務類接口和實現類 -- (個人理解:DAO類只對應一個Table,但是業務類對應的可能是一個業務流程...歡迎扔磚)
- 增加Client端Jsp頁面
- 增加Action類,將jsp的請求轉發給業務處理類(其實這裏是轉給業務接口類)
- 修改struts.xml, 增加action的調用,這裏用了struts的僞調用,實際上action的請求和轉發由spring接管了 --- (struts2-spring-plugin-2.2.3.1.jar )會幹這個活。
- 修改applicationContext.xml, 增加action bean
馬上開始:
1. 先把需要的jar包列一下:
2. web.xml文件的配置,增加struts2和spring的啓動項
<?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"
id="WebApp_ID" version="2.5">
<!-- Spring configuration -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Struts2 configuration -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
3. 配置ApplicationContext.xml -- 刪除hibernate.cfg.xml, 將配置挪到這裏來做(增加SessionFactory和dataSource的設置),也就是由Spring來接管,該文件放在src目錄下:
注意這裏我將一個applicationContext.xml分成了3部分,
- 數據庫的連接串單獨放一個文件(dataSource.properties)
- 平時不大會動的數據庫配置放到database.xml中
- applicationContext.xml會導入上面的內容,我們可以集中在bean的設置上
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" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- Hibernate configuration -->
<import resource="classpath:database.xml"/>
</beans>
database.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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="configBean" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:dataSource.properties</value>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${dataSource.driverClassName}" />
<property name="url" value="${dataSource.url}" />
<property name="username" value="${dataSource.username}" />
<property name="password" value="${dataSource.password}" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/test/bean/User.hbm.xml</value>
</list>
</property>
</bean>
</beans>
dataSource.properties
dataSource.driverClassName=com.mysql.jdbc.Driver
dataSource.url=jdbc:mysql://localhost/test?characterEncoding=utf-8
dataSource.username=root
dataSource.password=root
4. 增加一個User的POJO類和Hibernate對應的映射文件.package com.test.bean;
public class User {
private Integer id;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
private String name;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
User.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.test.bean.User" table="User" catalog="test">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="name" length="40" />
</property>
<property name="password" type="java.lang.String">
<column name="password" length="40" />
</property>
</class>
</hibernate-mapping>
5. 編寫DAO接口和DAO實現
package com.test.dao;
import com.test.bean.User;
public interface UserDao {
public User find(String name, String password);
public User find(User user);
}
package com.test.daoImpl;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.test.bean.User;
import com.test.dao.UserDao;
public class UserDaoImpl implements UserDao {
private SessionFactory sessionFactory;
@Override
public User find(String name, String password) {
Session session = sessionFactory.openSession();
String hql = "FROM User AS u WHERE u.name = :name AND u.password = :password";
Query q = session.createQuery(hql);
q.setString("name", name);
q.setString("password", password);
List<User> list = q.list();
session.close();
if (list.size()==0)
return null;
else
return list.get(0);
}
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public User find(User user) {
return find(user.getName(),user.getPassword());
}
}
注意上面的sessionFactory是由Spring來負責注入和管理的,因此一定要有一個set的方法來加載sessionFactory對象。6, 編寫業務接口和實現類
UserService接口
package com.test.service;
import com.test.bean.User;
public interface UserService {
public boolean isLogin(User user);
public boolean isLogin(String name, String password);
}
UserService實現類package com.test.serviceImpl;
import com.test.bean.User;
import com.test.dao.UserDao;
import com.test.service.UserService;
public class UserServiceImpl implements UserService {
private UserDao userDao;
public boolean isLogin(User user) {
return isLogin(user.getName(),user.getPassword());
}
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public boolean isLogin(String name, String password) {
boolean isLogin = false;
try
{
User u = userDao.find(name, password);
if (u != null)
{
isLogin = true;
}
}
catch (Exception e)
{
System.out.println("isLogin error\n" + e.getMessage());
}
return isLogin;
}
}
6. 在applicatonContext.xml中配置bean
<?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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- Hibernate configuration -->
<import resource="classpath:database.xml"/>
<!-- struts beans -->
<bean id="LoginAction" class="com.test.action.LoginAction" scope="prototype">
<property name="userService" ref="userService" />
</bean>
<bean id="userDao" class="com.test.daoImpl.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="userService" class="com.test.serviceImpl.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean>
</beans>
7. 增加jsp頁面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath%>">
<title><s:text name="home.title" /></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<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="easyTalk">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<s:form name="loginFrm" action="login">
<s:textfield name="username" key="username"></s:textfield>
<s:textfield name="password" key="password"></s:textfield>
<s:submit label="submit"></s:submit>
</s:form>
<s:actionerror/>
</body>
</html>
8. 配置struts.xml, 注意這裏有兩個改動,a. 增加了一個常量,聲明objectFactory由Spring接管. b. action 的class屬性中,維護的是Spring的bean對象,而不是具體的class.
<?xml version="1.0" encoding="UTF-8" ?>
<!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.custom.i18n.resources" value="message"></constant>
<constant name="struts.objectFactory" value="spring"></constant>
<constant name="struts.devMode" value="true" />
<package name="struts2" extends="struts-default">
<action name="login" class="LoginAction">
<result name="success" >loginresult.jsp</result>
<result name="input">login.jsp</result>
<result name="error">login.jsp</result>
</action>
</package>
</struts>
9. 增加action類,接受客戶端的響應
package com.test.action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.test.service.UserService;
public class LoginAction extends ActionSupport {
public String username;
public String password;
private UserService userService;
public LoginAction()
{
System.out.println("initialize LoginAction......");
}
public void setUserService(UserService userService)
{
this.userService = userService;
}
public String execute()
{
if (true == this.userService.isLogin(username, password))
{
ActionContext.getContext().getSession().put("username", username);
return SUCCESS;
}else{
super.addActionError(super.getText("loginfailed"));
return ERROR;
}
}
public void validate()
{
if ((null == username) || (0==username.length()))
{
super.addActionError(super.getText("warning.empty",new String[] {getText("username")}));
}
if ((null == password) || (0 == password.length()))
{
super.addActionError(super.getText("warning.empty",new String[] {getText("password")}));
}
}
}
10. 在applicationContext.xml中增加action的bean,請參考上面的xml文件,這裏略過。11. 最後整個代碼的架構如下:
12. 測試登錄功能
Login 頁面:
登錄成功:
系統日誌:
測試通過............................................................
12. 總結:
- 初期階段不要過分追求jar包的最小配置,否則會出現很多意料之外的錯誤.
- Jar包的版本要小心,最後我無奈將Hibernate從4.0降回3.6.8, 因爲差異太大.
- spring中bean的配置要小心,注入的時候是實現類,不是接口,名字也要注意,property name是引用對象的名字,而不是類的名字,這裏我就吃了一次虧,login的時候怎麼都說LoginAction對象找不到,其實是配置文件寫得有問題,結果Spring無法生成bean實例,這個錯誤很像Struts無法調用Spring的容器...但是其實是Spring容器無法生成Bean對象。
源代碼已上傳,請點擊此處下載