爲了更好地掌握SSH的用法,使用一個納稅服務系統來練手…..搭建SSH框架環境在上一篇已經詳細地說明了。http://blog.csdn.net/hon_3y/article/details/72630031javascript
擁有增刪改查和導入導出到EXCEL的功能:php
添加用戶:有了這個界面,咱們就知道實體表的屬性有什麼了。java
每一個模塊都應該有本身的配置文件,這樣的話就方便咱們管理模塊之間的功能,不用把全部的配置都寫在總文件中。spring
所以,咱們在user模塊建立了一個user包,下面又建立了config包來管理配置文件sql
根據上面需求要添加用戶的屬性,直接寫就好了。apache
public class User implements Serializable { private String id; private String dept; private String account; private String name; private String password; private String headImg; private boolean gender; private String state; private String mobile; private String email; private Date birthday; private String memo; public static String USER_STATE_VALID = "1";//有效, public static String USER_STATE_INVALID = "0";//無效 public User() { } public User(String id, String dept, String account, String name, String password, String headImg, boolean gender, String state, String mobile, String email, Date birthday, String memo) { this.id = id; this.dept = dept; this.account = account; this.name = name; this.password = password; this.headImg = headImg; this.gender = gender; this.state = state; this.mobile = mobile; this.email = email; this.birthday = birthday; this.memo = memo; } //各類setter和getter }
User.hbm.xml
映射文件也很是簡單,由於沒有關聯關係字段,直接寫屬性就好了。bash
<?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"> <hibernate-mapping> <class name="zhongfucheng.user.entity.User" table="user"> <id name="id" type="java.lang.String"> <column name="id" length="32" /> <generator class="uuid.hex" /> </id> <property name="name" type="java.lang.String"> <column name="name" length="20" not-null="true" /> </property> <property name="dept" type="java.lang.String"> <column name="dept" length="20" not-null="true" /> </property> <property name="account" type="java.lang.String"> <column name="account" length="50" not-null="true" /> </property> <property name="password" type="java.lang.String"> <column name="password" length="50" not-null="true" /> </property> <property name="headImg" type="java.lang.String"> <column name="headImg" length="100" /> </property> <property name="gender" type="java.lang.Boolean"> <column name="gender" /> </property> <property name="email" type="java.lang.String"> <column name="email" length="50" /> </property> <property name="mobile" type="java.lang.String"> <column name="mobile" length="20" /> </property> <property name="birthday" type="java.util.Date"> <column name="birthday" length="10" /> </property> <property name="state" type="java.lang.String"> <column name="state" length="1" /> </property> <property name="memo" type="java.lang.String"> <column name="memo" length="200" /> </property> </class> </hibernate-mapping>
寫完映射文件,記得要在Spring的總配置文件中讀取映射文件…值得注意的是,用戶模塊專門用一個user包來管理下面的代碼,這樣好管理!markdown
/** * UserDao接口,繼承着UserDao * */ public interface UserDao extends BaseDao<User> { }
把UserDaoImple對象添加到IOC容器中管理session
注意:由於咱們在BaseDao中使用的是HibernateDaoSupport這個API,所以咱們在UserDao中是須要在XML配置,注入SessionFactory的。app
在總配置文件中是有專門注入SessionFactory的bean配置的
<!-- 全部業務dao的parent --> <bean id="baseDao" abstract="true"> <property name="sessionFactory" ref="sessionFactory"></property> </bean>
/** * 繼承着BaseDaoImpl實現類,就有了CRUD的方法 * 又實現了UserDao接口,那麼UserDao接口就能夠對User模塊有相對應的補充 * * */ public class UserDaoImpl extends BaseDaoImpl<User> implements UserDao { }
<bean id="userDaoImpl" class="zhongfucheng.user.dao.impl.UserDaoImpl" parent="baseDao"></bean> <context:component-scan base-package="zhongfucheng.user"/>
<!--這是user模塊的配置文件--> <import resource="classpath:zhongfucheng/user/config/user-bean.xml"/>
/** * Created by ozc on 2017/5/23. */ public interface UserService { //新增 public void save(User user); //更新 public void update(User user); //根據id刪除O public void delete(Serializable id); //根據id查找 public User findObjectById(Serializable id); //查找列表 public List<User> findObjects(); }
package zhongfucheng.user.service.impl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; import zhongfucheng.user.dao.UserDao; import zhongfucheng.user.entity.User; import zhongfucheng.user.service.UserService; import java.io.Serializable; import java.util.List; /** * Created by ozc on 2017/5/23. */ @Service public class UserServiceImpl implements UserService { @Qualifier("userDaoImpl") @Autowired private UserDao userDaoImpl; @Override public void save(User user) { userDaoImpl.save(user); } @Override public void update(User user) { userDaoImpl.update(user); } @Override public void delete(Serializable id) { userDaoImpl.delete(id); } @Override public User findObjectById(Serializable id) { return userDaoImpl.findObjectById(id); } @Override public List<User> findObjects() { return userDaoImpl.findObjects(); } }
UserAction應該根據增刪改查應該有這麼幾個方法:
/** * 1.提供新增頁面 * 2.肯定新增用戶方法 * 3.提供修改頁面 * 4.肯定修改用戶方法 * 5.刪除用戶 * 6.批量刪除用戶 * 7.提供列表展現頁面 * * */
package zhongfucheng.user.action; import com.opensymphony.xwork2.ActionSupport; /** * Created by ozc on 2017/5/23. */ /** * 1.提供新增頁面 * 2.肯定新增用戶方法 * 3.提供修改頁面 * 4.肯定修改用戶方法 * 5.刪除用戶 * 6.批量刪除用戶 * 7.提供列表展現頁面 * * * */ public class UserAction extends ActionSupport { public String listUI() { return null; } public String addUI() { return null; } public String editUI() { return null; } public String edit() { return null; } public String delete() { return null; } public String add() { return null; } public String deleteSelect() { return null; } }
<?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> <package name="user-action" extends="struts-default" namespace="/user"> <action name="user_*" class="zhongfucheng.user.action.UserAction" method="{1}"> </action> </package> </struts>
<!--User模塊--> <include file="zhongfucheng/user/config/user-struts.xml"/>
導入到項目中:
咱們發如今JSP頁面中,如下的代碼是常常會出現的,所以咱們把它封裝一下:
建立一個公共文件,封裝常常用到的jsp頁面:
要使用的地方,直接導入就好了:
<head> <%@include file="/common/header.jsp"%> <title>用戶管理</title> </head>
接下來只要對各個功能進行填充邏輯代碼,就能夠了。
public String addUI() { return "addUI"; }
效果如圖所示:咱們的頭像和角色先不作,把其餘的先作了再看看。
寫上咱們請求的路徑:
/*************注入Service************************/ @Qualifier("userServiceImpl") @Autowired private UserService userServiceImpl; /************數據自動封裝,給出setter和getter*************************/ private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } /************獲得Service返回的數據*************************/ private List<User> userList; public String add() { userServiceImpl.save(user); //跳轉到列表顯示頁面 return "list"; }
<!--返回列表展現頁面,重定向到列表展現--> <result name="list" type="redirectAction"> <param name="actionName">user_listUI</param> </result>
/************獲得Service返回的數據*************************/ //這裏必定要給setter和getter方法,這樣JSP纔可以獲得屬性。否則就得不到了!!!我在這裏弄了好久!!!! private List<User> userList; public List<User> getUserList() { return userList; } public void setUserList(List<User> userList) { this.userList = userList; } public String listUI() { userList = userServiceImpl.findObjects(); return "listUI"; }
JSP經過iterator獲得數據,直接寫就好了。由於Action中該屬性有getter方法:
<s:iterator value="userList"> <tr bgcolor="f8f8f8"> <td align="center"><input type="checkbox" name="selectedRow" value="<s:property value="id"/> "/></td> <td align="center"><s:property value="name"/></td> <td align="center"><s:property value="account"/></td> <td align="center"><s:property value="dept"/></td> <td align="center"><s:property value="gender?'男':'女'"/></td> <td align="center"><s:property value="email"/></td> <td align="center"> <a href="javascript:doEdit(<s:property value="id"/>)">編輯</a> <a href="javascript:doDelete(<s:property value="id"/>)">刪除</a> </td> </tr> </s:iterator>
public String editUI() { //外邊已經傳了id過來了,咱們要找到id對應的User if (user.getId() != null && user != null) { //直接獲取出來,後面JSP會根據User有getter就能讀取對應的信息! user = userServiceImpl.findObjectById(this.user.getId()); } return "editUI"; }
<s:hidden name="user.id"/>
action="${basePath}user/user_edit.action"
public String edit() { //Struts2會自動把JSP帶過來的數據封裝到User對象上 if (user.getId() != null && user != null) { userServiceImpl.update(user); } //跳轉回列表展現 return "list"; }
delete方法就很是簡單了:
function doDelete(id) { document.forms[0].action = "${basePath}user/user_delete.action?user.id="+id; document.forms[0].submit(); }
public String delete() { if (user.getId() != null && user != null) { userServiceImpl.delete(user.getId());
}
return "list";
}
響應點擊事件:
function doDeleteAll() { document.forms[0].action = "${basePath}user/user_deleteSelect.action"; document.forms[0].submit(); }
private String[] selectedRow; public String[] getSelectedRow() { return selectedRow; } public void setSelectedRow(String[] selectedRow) { this.selectedRow = selectedRow; } public String deleteSelect() { for (String s : selectedRow) { userServiceImpl.delete(s); } return "list"; }