您的位置:首页 > 编程语言 > Java开发

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
<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目录图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息