SSH2 development
1. 項目文件列表
2. Bean 依賴關係:
3. 實現目標
實現對用戶的增刪查改
4. 開發步驟
創建ssh2 項目->增長hibernate capability,選擇annotation和core library,不要選擇創建session factory,而是讓spring來管理->增長spring capability,選擇aop,core,presistence core, persistence jdbc librara->在web-inf目錄下的web.xml中加入filter和listener以下:
- <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>
-
- <listener>
-
- <listener-class>
-
- org.springframework.web.context.ContextLoaderListener
-
- </listener-class>
-
- </listener>
<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>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
->在src目錄下新建struts.xml文件,dtd文件在struts2的demo中copy一個便可
->ok,準備工做已經完成。
->重啓服務器,會發現報錯,xxx.asm.xxx的錯誤,這是因爲spring中的asm.jar文件與hibernate中的asm.jar版本衝突,刪掉asm.2.x.x.jar便可。
從bean的依賴關係能夠看出,action依賴於service的實現,service又依賴於下一
層的userdao。
5. 創建cn.scau.ssh2.beans.User包
包下有User.java是user的屬性文件。
還有一個validation.xml待會再說。
6. 創建 cn.scau.ssh2.dao包
包下的UserDao.java是一個接口,規定了增刪查改的方法
- package cn.scau.ssh2.dao;
-
- import java.util.List;
-
- import cn.scau.ssh2.beans.User;
-
- public interface UserDao {
-
- public void saveUser(User user);
-
- public void removeUser(User user);
-
- public User findUserById(int id);
-
- public List<User> findAllUserser();
-
- public void updateUser(User user);
-
- }
-
- 7. 創建cn.scau.ssh2.dao.impl包
- 包下有UserDaoImpl.java爲具體的實現:
- package cn.scau.ssh2.dao.impl;
-
- import java.util.List;
-
- import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
-
- import cn.scau.ssh2.beans.User;
- import cn.scau.ssh2.dao.UserDao;
-
- public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
-
- @SuppressWarnings("unchecked")
- public List<User> findAllUserser() {
-
- String hql = "from User user order by user.id desc";
-
- List<User> list = this.getHibernateTemplate().find(hql);
-
- return list;
-
- }
-
- public User findUserById(int id) {
-
- User user = (User) this.getHibernateTemplate().get(User.class, id);
-
- return user;
- }
-
- public void removeUser(User user) {
-
- this.getHibernateTemplate().delete(user);
-
- }
-
- public void saveUser(User user) {
-
- this.getHibernateTemplate().save(user);
-
- }
-
- public void updateUser(User user) {
-
- this.getHibernateTemplate().update(user);
-
- }
-
- }
package cn.scau.ssh2.dao;
import java.util.List;
import cn.scau.ssh2.beans.User;
public interface UserDao {
public void saveUser(User user);
public void removeUser(User user);
public User findUserById(int id);
public List<User> findAllUserser();
public void updateUser(User user);
}
7. 創建cn.scau.ssh2.dao.impl包
包下有UserDaoImpl.java爲具體的實現:
package cn.scau.ssh2.dao.impl;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import cn.scau.ssh2.beans.User;
import cn.scau.ssh2.dao.UserDao;
//繼承HibernateDaoSupport,用到了getHibernateTemplate這個方法。
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
@SuppressWarnings("unchecked")
public List<User> findAllUserser() {
String hql = "from User user order by user.id desc";
List<User> list = this.getHibernateTemplate().find(hql);
return list;
}
public User findUserById(int id) {
User user = (User) this.getHibernateTemplate().get(User.class, id);
return user;
}
public void removeUser(User user) {
this.getHibernateTemplate().delete(user);
}
public void saveUser(User user) {
this.getHibernateTemplate().save(user);
}
public void updateUser(User user) {
this.getHibernateTemplate().update(user);
}
}
8. 創建cn.scau.ssh2.service包
包下的UserService.java其實現與UserDao差很少,由於UserService 依賴於UserDao的實現。
這裏就不貼代碼,具體看project裏的就行
9. 創建cn.scau.ssh2.service.impl包
包下的UserServiceImpl.java的實現就是調用UserDaoImpl中的方法。
詳見project。
10. 創建cn.scau.ssh2.action包
包下的各個action實現對頁面的控制。
Code:
- package cn.scau.ssh2.action;
-
- import java.util.Iterator;
- import java.util.Map;
- import java.util.Set;
-
- import cn.scau.ssh2.beans.User;
- import cn.scau.ssh2.service.UserService;
-
- import com.opensymphony.xwork2.ActionSupport;
-
- public class SaveUserAction extends ActionSupport {
-
- private static final long serialVersionUID = -4055521880257114396L;
-
- private User user;
-
- private UserService userService;
-
-
- public UserService getUserService() {
- return userService;
- }
-
- public void setUserService(UserService userService) {
- this.userService = userService;
- }
-
- public void setUser(User user) {
- this.user = user;
- }
-
- public User getUser() {
- return user;
- }
-
- @Override
- public String execute() throws Exception {
-
- this.userService.save(user);
-
- return SUCCESS;
-
- }
-
- }
package cn.scau.ssh2.action;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import cn.scau.ssh2.beans.User;
import cn.scau.ssh2.service.UserService;
import com.opensymphony.xwork2.ActionSupport;
public class SaveUserAction extends ActionSupport {
private static final long serialVersionUID = -4055521880257114396L;
private User user;
private UserService userService;
// get方法能夠不用寫,由於要用到的只是set方法
public UserService getUserService() {
return userService;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
public void setUser(User user) {
this.user = user;
}
public User getUser() {
return user;
}
@Override
public String execute() throws Exception {
this.userService.save(user);
return SUCCESS;
}
}
11. 其餘action相似實現,主要是在execute()中調用UserService對數據庫crud的方法。
此處要注意:能夠看到多了一個UpdatePUserAction,這是由於update用戶時,先要把用戶先preview,而後再update,因此多一個UpdatePUserAction,而後再掉轉到update.jsp頁面輸入須要update的數據。
12. 其實不是寫完全部action後才寫struts.xml和applicationContext.xml文件,而是每寫完一個action,在struts.xml和applicationContext.xml中對action進行配置。
13. struts.xml
code:
<?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="globalMessages">
</constant>
<constant name="struts.i18n.encoding" value="utf-8"></constant>
<package name="cn.scau.ssh2.action" extends="struts-default">
<action name="saveUser" class="saveUserAction">
<result name="success" type="redirect">
listUser.action
</result>
<result name="input">/save.jsp</result>
</action>
<action name="listUser" class="listUserAction">
<result name="success">/list.jsp</result>
<result name="input">/index.jsp</result>
</action>
<action name="deleteUser" class="removeUserAction">
<result name="success" type="redirect">
listUser.action
</result>
</action>
<action name="updatePUser" class="updatePUserAction">
<result name="success">/update.jsp</result>
</action>
<action name="updateUser" class="updateUserAction">
<result name="success" type="redirect">listUser.action</result>
<result name="input">/update.jsp</result>
</action>
</package>
</struts>
14. 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.5.xsd">
<!-- destroy-method="close"將鏈接放回鏈接池-->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url" value="jdbc:mysql://localhost:3306/ssh2"></property>
<property name="username" value="root"></property>
<property name="password" value="rick"></property>
<property name="maxActive" value="100"></property>
<property name="maxIdle" value="30"></property>
<property name="maxWait" value="500"></property>
<property name="defaultAutoCommit" value="true"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="show_sql">true</prop>
<!-- <prop key="hbm2ddl.auto">update</prop> -->
</props>
</property>
<property name="mappingResources">
<list>
<value>cn/scau/ssh2/beans/User.hbm.xml</value>
</list>
</property>
</bean>
<bean id="userDao" class="cn.scau.ssh2.dao.impl.UserDaoImpl"
scope="singleton">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="userService"
class="cn.scau.ssh2.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean>
<!-- scope="prototype"由於struts2中action是有狀態的,因此要將scope設爲"prototype",若是不設,能夠試一下效果,當輸入有誤信息時會出現錯誤提示重複輸出的狀況-->
<bean id="saveUserAction" class="cn.scau.ssh2.action.SaveUserAction"
scope="prototype">
<property name="userService" ref="userService"></property>
</bean>
<bean id="listUserAction" class="cn.scau.ssh2.action.ListUserAction"
scope="prototype">
<property name="userService" ref="userService"></property>
</bean>
<bean id="removeUserAction"
class="cn.scau.ssh2.action.RemoveUserAction" scope="prototype">
<property name="userService" ref="userService"></property>
</bean>
<bean id="updatePUserAction"
class="cn.scau.ssh2.action.UpdatePUserAction" scope="prototype">
<property name="userService" ref="userService"></property>
</bean>
<bean id="updateUserAction"
class="cn.scau.ssh2.action.UpdateUserAction" scope="prototype">
<property name="userService" ref="userService"></property>
</bean>
</beans>
15. 實現對輸入信息的校驗
在action包下能夠看到有*Action-validation.xml文件,實現校驗。由於咱們在action中定義的是對象類型,因此校驗時採用的validator爲visitor。使用了visitor則要在相應的bean包中加入user對象的基本屬性的校驗。
16. 此時完成ssh2整合,能夠在此基礎上實現對業務邏輯的開發。
END:ssh2 project是參照浪曦風中葉老師的視頻畫葫蘆出來的,能夠在vercd中找到相應的連接下載。