一.删除功能

之前跳转用户列表的时候把用户id,用户名,用户密码存入了ActionContext

1.userlist.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用户列表</title>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/style.css">
<script type="text/javascript" src="${pageContext.request.contextPath}/jslib/jquery-1.11.1.js"></script>
<script type="text/javascript">
$(function(){
//删除用户
$(".delete").click(function(){
var str=this.lang.split("!");
if(!confirm("你确定要删除"+ str[1] +"这个用户吗?"))
{
return;
}
//jquery ajax方式请求action
$.post("${pageContext.request.contextPath}/user/delete",{"user.id":str[0]},function(){
location.href="${pageContext.request.contextPath}/user/list";
});
});
});
</script>
</head>
<body>
用户列表
<br>
<br>
<table class="bordered">
<thead>
<tr><th>序号</th><th>用户名</th><th>密码</th><th>照片</th><th>删除</th><th>修改</th></tr>
</thead>
<!-- USERLIST,cuser,s存入的Stack Context -->
<s:iterator value="#USERLIST" id="cuser" status="s">
<tr>
<td><s:property value="#s.index+1"/></td>
<td><s:property value="#cuser.userName"/></td>
<td><s:property value="#cuser.pwd"/></td>
<td><a href="#" class="picture" lang="<s:property value="#cuser.id"/>">照片</a></td>
<td><a href="#" class="delete" lang="<s:property value="#cuser.id"/>!<s:property value="#cuser.userName"/>">删除</a></td>
<td><a href="#" class="modify" lang="<s:property value="#cuser.id"/>">修改</a></td>
</tr>
</s:iterator>
</table>
<br>
<br>
<a href="${pageContext.request.contextPath}/main.jsp">返回主页面</a>
</body>
<!-- 调试
Value Stack 访问时不用加#
Stack Context 访问时要加#
-->
<s:debug></s:debug>
</html>

点击确认删除,把用户id利用domain model的方式存入action

2.UserAction

   /**
* 删除用户
* @return
* @throws ClassNotFoundException
* @throws SQLException
* @throws NamingException
*/
public String delete() throws ClassNotFoundException, SQLException, NamingException
{
UserDAO dao=new UserDAO();
dao.deleteUser(user);
return null;
}

3.UserDAO

  /**
* 删除用户
* @param user
* @throws SQLException
*/
public void deleteUser(User user) throws SQLException
{
sql="delete from users where id=?";
ps=conn.prepareStatement(sql);
ps.setInt(1, user.getId());
ps.execute();
conn.close();
}

二.修改功能

1.userlist.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用户列表</title>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/style.css">
<script type="text/javascript" src="${pageContext.request.contextPath}/jslib/jquery-1.11.1.js"></script>
<script type="text/javascript">
$(function(){
//删除用户
$(".delete").click(function(){
var str=this.lang.split("!");
if(!confirm("你确定要删除"+ str[1] +"这个用户吗?"))
{
return;
}
//jquery ajax方式请求action
$.post("${pageContext.request.contextPath}/user/delete",{"user.id":str[0]},function(){
location.href="${pageContext.request.contextPath}/user/list";
});
});   //修改用户
$(".modify").click(function(){
//为保证跳转的控制权在action中
location.href="${pageContext.request.contextPath}/user/modify?user.id=" + this.lang;
});
});
</script>
</head>
<body>
用户列表
<br>
<br>
<table class="bordered">
<thead>
<tr><th>序号</th><th>用户名</th><th>密码</th><th>照片</th><th>删除</th><th>修改</th></tr>
</thead>
<!-- USERLIST,cuser,s存入的Stack Context -->
<s:iterator value="#USERLIST" id="cuser" status="s">
<tr>
<td><s:property value="#s.index+1"/></td>
<td><s:property value="#cuser.userName"/></td>
<td><s:property value="#cuser.pwd"/></td>
<td><a href="#" class="picture" lang="<s:property value="#cuser.id"/>">照片</a></td>
<td><a href="#" class="delete" lang="<s:property value="#cuser.id"/>!<s:property value="#cuser.userName"/>">删除</a></td>
<td><a href="#" class="modify" lang="<s:property value="#cuser.id"/>">修改</a></td>
</tr>
</s:iterator>
</table>
<br>
<br>
<a href="${pageContext.request.contextPath}/main.jsp">返回主页面</a>
</body>
<!-- 调试
Value Stack 访问时不用加#
Stack Context 访问时要加#
-->
<s:debug></s:debug>
</html>

2.UserAciton

点击修改用户按钮跳转到修改用户界面

    /**
* 为用户准备照片,以便在modify.jsp中显示
* @return
* @throws SQLException
* @throws ClassNotFoundException
* @throws NamingException
*/
public String modify() throws SQLException, ClassNotFoundException, NamingException
{
UserDAO dao=new UserDAO();
//这个user会在Value Stack中出现
user=dao.getUserById(user.getId());
   return "modify";
}

这里的user存入了Value Stack中,在modify页面中可以调用

3.UserDAO

  /**
* 通过id得到用户
* @param id
* @return
* @throws SQLException
*/
public User getUserById(int id) throws SQLException
{
sql="select * from users where id=?";
User user=new User();
ps=conn.prepareStatement(sql);
ps.setInt(1, id);
ResultSet rs=ps.executeQuery();
rs.next();
user.setId(rs.getInt(1));
user.setUserName(rs.getString(2));
user.setPwd(rs.getString(3));
conn.close();
return user;
}

4.user.xml

<result name="modify">/WEB-INF/user/modify.jsp</result>

5.modify.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>修改</title>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/style.css">
<script type="text/javascript" src="${pageContext.request.contextPath}/jslib/jquery-1.11.1.js"></script>
</head>
<body>
修改用户信息
<br>
<br>
<!-- 修改用户信息 -->
<form method="post" action="${pageContext.request.contextPath}/user/save">
<table>
<tr><td>用户名</td><td><input type="text" name="user.userName" value="<s:property value="user.userName"/>"/></td></tr>
<tr><td>密码</td><td><input type="text" name="user.pwd" value="<s:property value="user.pwd"/>"/></td></tr>
<tr><td><input type="hidden" name="user.id" value="<s:property value="user.id"/>"/></td></tr>
<tr><td colspan="2"><input type="submit" value="修改"></td></tr>
</table>
</form>
<br>
<br>
<a href="${pageContext.request.contextPath}/main.jsp">返回主页面</a>
<s:debug></s:debug>
</body>
</html>

6.UserAction

通过domain model的方式将modify.jsp中修改的user信息传入

   /**
* 修改用户
* @return
* @throws ClassNotFoundException
* @throws SQLException
* @throws NamingException
*/
public String save() throws ClassNotFoundException, SQLException, NamingException
{
UserDAO dao=new UserDAO();
dao.modifyUser(user);
//跳转到list这个action重新加载userlist.jsp
return "userlist";
}

最后跳转到list这个action重新加载userlist,修改完成

7.UserDAO

  /**
* 修改用户信息
* @param user
* @throws SQLException
*/
public void modifyUser(User user) throws SQLException
{
sql="update users set userName=?,pwd=? where id=?";
ps=conn.prepareStatement(sql);
ps.setString(1, user.getUserName());
ps.setString(2, user.getPwd());
ps.setInt(3, user.getId());
ps.execute();
conn.close();
}

Struts2(七.删除和修改用户功能的实现)的更多相关文章

  1. Django实现简单的用户添加、删除、修改等功能

    一. Django必要的知识点补充 1. templates和static文件夹及其配置 1.1 templates文件夹 所有的HTML文件默认都放在templates文件夹下. 1.2 stati ...

  2. 限制oracle用户创建、删除、修改用户对象

    在sys用户下执行: CREATE OR REPLACE TRIGGER lms2014BEFORE create or DROP OR ALTER ON databaseDECLAREBEGINIF ...

  3. MVC5 网站开发之七 用户功能 3用户资料的修改和删除

    这次主要实现管理后台界面用户资料的修改和删除,修改用户资料和角色是经常用到的功能,但删除用户的情况比较少,为了功能的完整性还是坐上了.主要用到两个action "Modify"和& ...

  4. linux下添加删除,修改,查看用户和用户组

    一.组操作 1.创建组: groupadd test #增加一个test组 2.修改组 groupmod -n test2 test #将test组的名子改成test2 3.删除组 groupdel ...

  5. linux创建用户、设置密码、修改用户、删除用户

    创建用户.设置密码.修改用户.删除用户:useradd testuser 创建用户testuserpasswd testuser 给已创建的用户testuser设置密码说明:新创建的用户会在/home ...

  6. Hibernate+Struts2+jsp 修改用户信息

    在用户列表页面点击修改,进入修改页面 修改薪酬为555,点击提交,重新跳回该页面 修改成功 关键代码如下 基层的代码,这里增加了一个根据用户id查询的方法 dao层 //修改 public USer ...

  7. oracle创建用户,修改用户,删除用户等关于用户的

    --直接修改底层表 USER$ 更换用户名 1.windows 平台下运行 cmd 2.sqlplus /nolog 3.SQL> conn SYSTEM/123@ORCL as sysdba ...

  8. useradd adduser linux创建用户、设置密码、修改用户、删除用户

    创建用户.设置密码.修改用户.删除用户: useradd testuser 创建用户testuser passwd testuser 给已创建的用户testuser设置密码 说明:新创建的用户会在/h ...

  9. 为VisualSVN Server增加在线修改用户密码的功能

    原文:为VisualSVN Server增加在线修改用户密码的功能 附件下载:点击下载 VisualSVN Server是一个非常不错的SVN Server程序,方便,直观,用户管理也异常方便. 不过 ...

随机推荐

  1. PAT1089. Insert or Merge

    PAT1089. Insert or Merge 题目大意 给定一个初始序列src, 一个排序当中的序列tar, 问排序方式是 Insert Sort, 或者 Merge Sort. 并输出下一次迭代 ...

  2. Zookeeper watch参照表

    Watcher 设置是开发中最常见的,需要搞清楚watcher的一些基本特征,对于exists.getdata.getchild对于节点的不同操 作会收到不同的 watcher信息.对父节点的变更以及 ...

  3. Ueditor插入script标签

    对于这个问题.我想有的人会遇到有的人不会遇到,后面说为什么. 有的人会百度解决问题.百度官方文档这样回答 然而你去editor_config.js搜索根本找不到这个配置.(百度你该更新了.....) ...

  4. Unity】Socket 同步与异步

    http://blog.csdn.net/ldy597321444/article/details/51519157

  5. 1080Ti+ubuntu14.04

    我来回折腾了几天,从装了好几次系统,后来问了我同学才知道原来是驱动版本的问题,唉,第一次跑去nvidia看他们的online doc.我是相当的郁闷,敢不敢弄得简单点啊,我是电脑小白啊,硬件一窍不通啊 ...

  6. 还在占用存储的进程lsof grep deleted;

    查看僵尸进程 lsof grep deleted; 用于查看已经停止但还在占用存储的进程

  7. view添加毛玻璃效果两种方法

    第一种方法: UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]; UIVisualEffectV ...

  8. poj_1091_跳蚤

    Z城市居住着很多只跳蚤.在Z城市周六生活频道有一个娱乐节目.一只跳蚤将被请上一个高空钢丝的正中央.钢丝很长,可以看作是无限长.节目主持人会给该跳蚤发一张卡片.卡片上写有N+1个自然数.其中最后一个是M ...

  9. mongo复制集脑裂问题如何处理

    mongo replication 脑裂问题如何处理: 一.问题描述:一套mongo replication有4个节点.1个仲裁节点.在停止实例(或实例毁坏)的时候,导致所有节点都变为SECONDAR ...

  10. mysql 导出数据字典

    使用Navicat工具 查询: SELECT TABLE_SCHEMA AS '数据库', TABLE_NAME AS '表名', COLUMN_NAME AS '字段名', COLUMN_TYPE ...