创建struts文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name = "p1" namespace="/" extends="struts-default">
<action name="users" class="com.hr.action.usersAction">
<result name="index">/index.jsp</result>
<result name="EDIT">/EDIT.jsp</result>
</action>
</package>
</struts>
以及jsp文件
配置实体类
package com.hr.entry;
public class users {
private int id;
private String name ;
private String pwd;
public users() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "users [id=" + id + ", name=" + name + ", pwd=" + pwd + "]";
}
public users(int id) {
super();
this.id = id;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
**配置Action文件**
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200920194812239.png#pic_center)
```java
package com.hr.action;
import java.util.List;
import com.hr.dao.CommonDao;
import com.hr.entry.users;
public class usersAction {
private List<users> list;
/**
* 查询所有
* @return
*/
public String selectAll(){
CommonDao cd = new CommonDao();
list = cd.selectAll(users.class);
// System.out.println(list);
return "index";
}
public List<users> getList() {
return list;
}
public void setList(List<users> list) {
this.list = list;
}
/**
* 增加
*/
private users ser;
public String add(){
CommonDao cd = new CommonDao();
cd.add(ser);
System.out.println(ser);
//返回查询所有
return selectAll();
}
/**
* 根据id查询所有
* @return
*/
public String selectBYID(){
CommonDao cd = new CommonDao();
ser = (users)cd.selectOne(ser);
return "EDIT";
}
public users getSer() {
return ser;
}
public void setSer(users ser) {
this.ser = ser;
}
public String update(){
CommonDao cd = new CommonDao();
System.out.println(ser);
cd.update(ser);
return selectAll();
}
/**
* 删除
* @return
*/
public String dele(){
CommonDao cd = new CommonDao();
cd.delete(ser);
return selectAll();
}
}
首页
记得改为utf-8,不然识别不来中文
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h1>账号管理</h1>
<table border="1px">
<tr>
<th>编号</th>
<th>账户</th>
<th>密码</th>
<th>操作</th>
</tr>
<c:forEach items="${list}" var="c">
<tr>
<td>${c.id}</td>
<td>${c.name}</td>
<td>${c.pwd}</td>
<td>
<a href="javascript:del(${c.id})">删除</a>
<a href="users!selectBYID?ser.id = ${c.id}">修改</a>
</td>
</tr>
</c:forEach>
</table>
<a href="Add.jsp">增加</a>
<script type="text/javascript">
function del(id){
if(confirm("是否删除?")){
location.href="users!dele?ser.id="+id;
}
}
</script>
</body>
</html>
修改页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'EDIT.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h1>修改密码信息表</h1>
<form action="users!update" method="post">
<table border="1px">
<tr>
<th>编号:</th>
<td>${ser.id}</td>
</tr>
<tr>
<th>账号:</th>
<td>
<input type="hidden" name="ser.id" value="${ser.id}"/>
<input type="text" name="ser.name" value="${ser.name}"/>
</td>
</tr>
<tr>
<th>密码:</th>
<td><input type="text" name="ser.pwd" value="${ser.pwd}"/>
</td>
</tr>
<tr >
<td colspan="2"><input type="submit" value="确认"/></td>
</tr>
</form>
</table>
</body>
</html>
增加页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'Add.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h1>增加账号</h1>
<form action="users!add" method="post">
账号:<input type="text" name="ser.name"></br>
密码:<input type="text" name="ser.pwd"></br>
<input type="submit" value="确认增加" />
</form>
</body>
</html>