为了更好地掌握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"; }