Spring3.x_Struts2.x_Hibernate3.x整合之用户管理例子笔记
2014-10-18 00:29
423 查看
在上面的文章基础上实现了用户管理的简单例子,例子代码保存下来留给多年后的自己!
<span style="font-size:14px;">package org.oms.spring.action;
import javax.annotation.Resource;
import org.oms.spring.model.User;
import org.oms.spring.service.IGroupService;
import org.oms.spring.service.IUserService;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import sun.print.resources.serviceui;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
@SuppressWarnings("serial")
/**
* 此时等于用Spring来创建了userAction对象,
* 在struts的配置文件中写Action的class的时候就不能写类,
* 而应该写userAction中对象
* @author sunlight
*
*/
@Controller("userAction")
@Scope("prototype")
public class UserAction extends ActionSupport implements ModelDriven<User>{
private User user;
private IUserService userService;
private IGroupService groupService;
private int gid;
@Override
public User getModel() {
if (user==null) {
user=new User();
}
return user;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public IUserService getUserService() {
return userService;
}
@Resource
public void setUserService(IUserService userService) {
this.userService = userService;
}
public IGroupService getGroupService() {
return groupService;
}
@Resource
public void setGroupService(IGroupService groupService) {
this.groupService = groupService;
}
public int getGid() {
return gid;
}
public void setGid(int gid) {
this.gid = gid;
}
public String list(){
ActionContext.getContext().put("us", userService.listAllUsers());
return SUCCESS;
}
public String delete() {
userService.delete(user.getId());
ActionContext.getContext().put("url", "/user_list.action");
return "redirect";
}
public String addInput(){
ActionContext.getContext().put("gs", groupService.list());
return SUCCESS;
}
public String add(){
userService.add(user, gid);
ActionContext.getContext().put("url", "/user_list.action");
return "redirect";
}
/**
* key表示表单,出差后找对应的表单显示
*/
public void validateAdd(){
if (user.getUsername()==null || "".equals(user.getUsername())) {
this.addFieldError("username", "用户姓名不能为空");
}
if (user.getPassword()==null || "".equals(user.getPassword())) {
this.addFieldError("password", "用户密码不能为空");
}
//如果有错误,得重新调用addInput方法把组的信息设置进去
if (this.hasFieldErrors()) {
addInput();
}
}
public String updateInput() {
ActionContext.getContext().put("gs", groupService.list());
User tu=userService.load(user.getId());
user.setUsername(tu.getUsername());
user.setPassword(tu.getPassword());
user.setAge(tu.getAge());
this.setGid(tu.getGroup().getId());
return SUCCESS;
}
public String update() {
User tu=userService.load(user.getId());
tu.setUsername(user.getUsername());
tu.setPassword(user.getPassword());
tu.setAge(user.getAge());
userService.update(tu,gid);
ActionContext.getContext().put("url", "/user_list.action");
return "redirect";
}
}
</span>
list.jsp
updateInput.jsp
修改了UserService的查询方法
<span style="font-size:14px;">@Override
public List<User> listAllUsers() {
String hql="from User u left join fetch u.group";
return userHibernateDao.list(hql);
}</span>
WebContent目录图:
<span style="font-size:14px;">package org.oms.spring.action;
import javax.annotation.Resource;
import org.oms.spring.model.User;
import org.oms.spring.service.IGroupService;
import org.oms.spring.service.IUserService;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import sun.print.resources.serviceui;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
@SuppressWarnings("serial")
/**
* 此时等于用Spring来创建了userAction对象,
* 在struts的配置文件中写Action的class的时候就不能写类,
* 而应该写userAction中对象
* @author sunlight
*
*/
@Controller("userAction")
@Scope("prototype")
public class UserAction extends ActionSupport implements ModelDriven<User>{
private User user;
private IUserService userService;
private IGroupService groupService;
private int gid;
@Override
public User getModel() {
if (user==null) {
user=new User();
}
return user;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public IUserService getUserService() {
return userService;
}
@Resource
public void setUserService(IUserService userService) {
this.userService = userService;
}
public IGroupService getGroupService() {
return groupService;
}
@Resource
public void setGroupService(IGroupService groupService) {
this.groupService = groupService;
}
public int getGid() {
return gid;
}
public void setGid(int gid) {
this.gid = gid;
}
public String list(){
ActionContext.getContext().put("us", userService.listAllUsers());
return SUCCESS;
}
public String delete() {
userService.delete(user.getId());
ActionContext.getContext().put("url", "/user_list.action");
return "redirect";
}
public String addInput(){
ActionContext.getContext().put("gs", groupService.list());
return SUCCESS;
}
public String add(){
userService.add(user, gid);
ActionContext.getContext().put("url", "/user_list.action");
return "redirect";
}
/**
* key表示表单,出差后找对应的表单显示
*/
public void validateAdd(){
if (user.getUsername()==null || "".equals(user.getUsername())) {
this.addFieldError("username", "用户姓名不能为空");
}
if (user.getPassword()==null || "".equals(user.getPassword())) {
this.addFieldError("password", "用户密码不能为空");
}
//如果有错误,得重新调用addInput方法把组的信息设置进去
if (this.hasFieldErrors()) {
addInput();
}
}
public String updateInput() {
ActionContext.getContext().put("gs", groupService.list());
User tu=userService.load(user.getId());
user.setUsername(tu.getUsername());
user.setPassword(tu.getPassword());
user.setAge(tu.getAge());
this.setGid(tu.getGroup().getId());
return SUCCESS;
}
public String update() {
User tu=userService.load(user.getId());
tu.setUsername(user.getUsername());
tu.setPassword(user.getPassword());
tu.setAge(user.getAge());
userService.update(tu,gid);
ActionContext.getContext().put("url", "/user_list.action");
return "redirect";
}
}
</span>
list.jsp
<span style="font-size:14px;"><%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h3>用户列表<a href="user_addInput.action">添加用户</a></h3> <s:iterator value="#us"> ${id} ----- <a href="user_show.action?gid=${id }" >${username} </a>--------${group.name }-----${password }------${createDate } <a href="user_delete.action?id=${id}">删除</a>|<a href="user_updateInput.action?id=${id }">更新</a><br/> </s:iterator> </body> </html></span>addInput.jsp
<span style="font-size:14px;"><%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h3>添加用户</h3> <form action="user_add.action" method="post" > <s:textfield label="用户姓名" name="username"/><br/> <s:password label="密码" name="password"/><br/> <s:select list="gs" listKey="id" listValue="name" name="gid" label="选择组"></s:select><br/> <s:textfield label="年龄" name="age"/><br/> <s:textfield label="生日" name="birthDay"/><br/> <s:submit label="添加用户" value="添加用户"/> </form> </body> </html></span>
updateInput.jsp
<span style="font-size:14px;"><%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h3>修改用户</h3> <form action="user_update.action" method="post" > <s:hidden name="id"/> <s:textfield label="用户姓名" name="username"/><br/> <s:password label="密码" name="password"/><br/> <s:select list="gs" listKey="id" listValue="name" name="gid" label="选择组"/><br/> <s:textfield label="年龄" name="age"/><br/> <s:textfield label="生日" name="birthDay"/><br/> <s:submit label="修改用户" value="修改用户"/> </form> </body> </html></span>
修改了UserService的查询方法
<span style="font-size:14px;">@Override
public List<User> listAllUsers() {
String hql="from User u left join fetch u.group";
return userHibernateDao.list(hql);
}</span>
WebContent目录图:
相关文章推荐
- Struts2+Spring3+Hibernate3.6整合之一:用户管理
- Spring3.x_Struts2.x_Hibernate3.x整合之个人备忘笔记
- struts2,hibernate,spring整合笔记(2)
- GXT2.2+GWT2.4+Struts2+Hibernate3.X+Spring2.5整合完成!!
- 在maven环境下 基于Struts2 spring hibernate用户登录及查询整合案例
- Spring3.x_Struts2.x_Hibernate3.x整合之声明式事务配置
- Spring3整合Hibernate3.6之二:用AOP管理事务处理
- 学习笔记:spring与hibernate整合(采用aop来管理事务来实现声明式事务)
- struts2,hibernate,spring整合笔记(3)
- SSH 整合例子 Struts2 Spring3 Hibernate3
- 1、Struts2和Hibernate的简单整合(带Session的管理方式)
- SSH整合例子 Struts2 Spring3 Hibernate3 注解
- Struts2.x、Spring3.x、Hibernate3.x整合开发
- 定时任务服务开发笔记(1)——Quartz2+Spring3+Struts2整合
- MySQL学习笔记-用户管理例子
- 懒得笔记5 spring aop 整合hibernate 事务管理
- jbpm4.3+struts2+spring3+hibernate3整合(二)之权限管理
- 基于整合了struts 和hibernate 的j2ee 架构的用户权限管理系统的设计与实现
- Spring3.x_Struts2.x_Hibernate3.x整合之OpenSessionInView的实现
- 整合Struts2,Hibernate和Spring的一个简单例子