struts2增删改查---layer---iframe层
在这里写一下struts2中的简单的增删改查
struts.xml中的配置
1 <?xml version="1.0" encoding="UTF-8" ?>
2 <!DOCTYPE struts PUBLIC
3 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
4 "http://struts.apache.org/dtds/struts-2.0.dtd">
5
6 <struts>
7
8 <package name="aa" namespace="/user" extends="struts-default">
9
10 <action name="abc" class="com.zr.Struts2Text" method="showstuent">
11 <result name="ok110">/show.jsp</result>
12 </action>
13 <action name="add" class="com.zr.Struts2Text" method="add">
14 <result name="ok110" type="redirectAction">abc.action?ye=1</result>
15 </action>
16 <action name="deletee" class="com.zr.Struts2Text" method="deletee">
17 <result name="ok110" type="redirectAction">abc.action?ye=1</result>
18 </action>
19 <action name="toupdate" class="com.zr.Struts2Text" method="toupdate">
20 <result name="ok110" >/update.jsp</result>
21 </action>
22 <action name="update" class="com.zr.Struts2Text" method="update">
23 <result name="ok110" type="redirectAction">abc.action?ye=1</result>
24 </action>
25
26 </package>
27
28
29 <package name="bb" namespace="/class" extends="struts-default">
30
31 <action name="abc" class="com.zr.ClassStruts2Text" method="showstuent">
32 <result name="ok110">/Classshow.jsp</result>
33 </action>
34 <action name="add" class="com.zr.ClassStruts2Text" method="add">
35 <result name="ok110" type="redirectAction">abc.action?ye=1</result>
36 </action>
37 <action name="deletee" class="com.zr.ClassStruts2Text" method="deletee">
38 <result name="ok110" type="redirectAction">abc.action?ye=1</result>
39 </action>
40 <action name="toupdate" class="com.zr.ClassStruts2Text" method="toupdate">
41 <result name="ok110" >/Classupdate.jsp</result>
42 </action>
43 <action name="update" class="com.zr.ClassStruts2Text" method="update">
44 <result name="ok110" type="redirectAction">abc.action?ye=1</result>
45 </action>
46
47 </package>
48
49 </struts>
web.xml配置
1 <?xml version="1.0" encoding="UTF-8"?>
2 <web-app version="2.5"
3 xmlns="http://java.sun.com/xml/ns/javaee"
4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
6 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
7 <welcome-file-list>
8 <welcome-file>index.jsp</welcome-file>
9 </welcome-file-list>
10
11
12 <filter>
13 <filter-name>filter</filter-name>
14 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
15 </filter>
16 <filter-mapping>
17 <filter-name>filter</filter-name>
18 <url-pattern>/*</url-pattern>
19 </filter-mapping>
20
21
22 </web-app>
index.jsp页面
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
2 <%
3 String path = request.getContextPath();
4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
5 %>
6
7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
8 <html>
9 <head>
10 <base href="<%=basePath%>">
11
12 <title>My JSP 'index.jsp' starting page</title>
13 <meta http-equiv="pragma" content="no-cache">
14 <meta http-equiv="cache-control" content="no-cache">
15 <meta http-equiv="expires" content="0">
16 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
17 <meta http-equiv="description" content="This is my page">
18 <!--
19 <link rel="stylesheet" type="text/css" href="styles.css">
20 -->
21 </head>
22
23 <body>
24
25 <button><a href="user/abc.action?ye=1"> 查询users表 </a></button><br/>
26 <button><a href="class/abc.action?ye=1"> 查询class表 </a></button>
27
28 </body>
29 </html>
Struts2Text.ava
1 package com.zr;
2
3 import java.util.ArrayList;
4 import java.util.List;
5 import java.util.Map;
6
7 import com.opensymphony.xwork2.ActionSupport;
8
9 public class Struts2Text extends ActionSupport{
10 Jdbc jdbc=new Jdbc();
11
12 List<Student> list = new ArrayList<Student>();
13 Student student = new Student();
14 Service service = new Service();
15
16 String username = student.getUsername();
17 String password = student.getPassword();
18 int id = student.getId();
19 int count = student.getCount();
20 String ye = student.getYe();
21
22
23
24 public int getCount() {
25 return count;
26 }
27 public void setCount(int count) {
28 this.count = count;
29 }
30 public String getYe() {
31 return ye;
32 }
33 public void setYe(String ye) {
34 this.ye = ye;
35 }
36 public Student getStudent() {
37 return student;
38 }
39 public void setStudent(Student student) {
40 this.student = student;
41 }
42 public int getId() {
43 return id;
44 }
45 public void setId(int id) {
46 this.id = id;
47 }
48 public String getUsername() {
49 return username;
50 }
51 public void setUsername(String username) {
52 this.username = username;
53 }
54 public String getPassword() {
55 return password;
56 }
57 public void setPassword(String password) {
58 this.password = password;
59 }
60
61
62
63 public List<Student> getList() {
64 return list;
65 }
66 public void setList(List<Student> list) {
67 this.list = list;
68 }
69
70
71 //查
72 public String showstuent(){
73 if(ye==null){
74 ye="1";
75 }
76 list = service.show(ye);
77 count=service.tablecount();
78 return "ok110";
79 }
80 //增
81 public String add(){
82 service.add(username,password);
83 return "ok110";
84 }
85 //删
86 public String deletee(){
87 service.deletee(id);
88 return "ok110";
89 }
90
91 //改
92 public String toupdate(){
93 student = service.toupdate(id);
94 return "ok110";
95 }
96 public String update(){
97 System.out.println(id);
98 service.update(id,username,password);
99 return "ok110";
100 }
101 }
jdbc搭建
1 package com.zr;
2
3 import java.sql.Connection;
4 import java.sql.DriverManager;
5 import java.sql.ResultSet;
6 import java.sql.SQLException;
7 import java.sql.Statement;
8
9 public class Jdbc {
10 Connection conn;
11 Statement stem;
12 ResultSet re;
13 /*
14 * jdbc五步走:
15 * 1:加载驱动
16 * 2:创建连接
17 * 2.1:地址
18 * 2.2:用户名 root
19 * 2.3:密码 123
20 * 3:创建发送执行sql语句对象
21 * 4:发送执行sql语句
22 * 5:操作结果集
23 */
24
25 private void lianjie() {
26 try {
27 Class.forName("com.mysql.jdbc.Driver");
28 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/zrxz", "root", "root");
29 stem = conn.createStatement();
30 } catch (Exception e) {
31 e.printStackTrace();
32 }
33 }
34
35 private void guanbi() {
36 try {
37 if (re!=null) {
38 re.close();
39 }
40
41 if (stem!=null) {
42 stem.close();
43 }
44 if (conn!=null) {
45 conn.close();
46 }
47 } catch (SQLException e) {
48 // TODO Auto-generated catch block
49 e.printStackTrace();
50 }
51 }
52
53 /*
54 * 创建一个应用于任何查询的show方法。
55 * 但凡查询功能一定返回一个结果集
56 */
57 public ResultSet show ( String sql ){
58 this.lianjie();
59 try {
60 re=stem.executeQuery(sql);
61 } catch (SQLException e) {
62 }
63 return re;
64 }
65
66 public int update ( String sql ){
67 this.lianjie();
68 int i;
69 try {
70 i = stem.executeUpdate(sql);
71 this.guanbi();
72 return i;
73 } catch (SQLException e) {
74 }
75 return 0;
76 }
77
78 }
service代码
1 package com.zr;
2
3 import java.sql.ResultSet;
4 import java.sql.SQLException;
5 import java.util.ArrayList;
6 import java.util.List;
7
8 public class Service {
9 Jdbc jdbc = new Jdbc();
10 int page=4;
11 public List<Student> show(String ye) {
12 int yee = Integer.parseInt(ye);
13 yee=(yee-1)*page;
14 List<Student> list = new ArrayList<Student>();
15 ResultSet rs = jdbc.show("select * from users limit "+yee+","+page+" ");
16 try {
17 while (rs.next()) {
18 Student student = new Student();
19 student.setId(rs.getInt("id"));
20 student.setUsername(rs.getString("username"));
21 student.setPassword(rs.getString("password"));
22 list.add(student);
23 }
24 } catch (SQLException e) {
25 }
26 return list;
27 }
28
29 public void add(String username, String password) {
30 jdbc.update("insert into users (username,password)values('"+username+"','"+password+"')");
31
32 }
33
34 public void deletee(int id) {
35 jdbc.update("delete from users where id='"+id+"'");
36 }
37
38 public Student toupdate(int id) {
39 Student student=new Student();
40 ResultSet re = jdbc.show("select * from users where id='"+id+"'");
41 try {
42 while (re.next()) {
43 student.setId(id);
44 student.setUsername(re.getString("username"));
45 student.setPassword(re.getString("password"));
46 }
47 } catch (SQLException e) {
48 // TODO Auto-generated catch block
49 e.printStackTrace();
50 }
51 return student;
52 }
53
54 public void update(int id, String username, String password) {
55 jdbc.update("update users set username='"+username+"',password='"+password+"' where id='"+id+"'");
56
57 }
58
59 public int tablecount() {
60 int tablecount=0;
61 int count=0;
62 ResultSet re = jdbc.show("select count(*) from users ");
63 try {
64 while (re.next()) {
65 tablecount = re.getInt("count(*)");
66 }
67 } catch (SQLException e) {
68 // TODO Auto-generated catch block
69 e.printStackTrace();
70 }
71 if(tablecount%page==0){
72 count = tablecount/page;
73 }
74 if (tablecount%page!=0) {
75
76 count = tablecount/page+1;
77 }
78 return count;
79 }
80
81 }
实体类
1 package com.zr;
2
3 public class Student {
4
5
6 private int id;
7 private String username;
8 private String password;
9 private String ye;
10 private int count;
11
12
13
14 public String getYe() {
15 return ye;
16 }
17 public void setYe(String ye) {
18 this.ye = ye;
19 }
20 public int getCount() {
21 return count;
22 }
23 public void setCount(int count) {
24 this.count = count;
25 }
26 public int getId() {
27 return id;
28 }
29 public void setId(int id) {
30 this.id = id;
31 }
32 public String getUsername() {
33 return username;
34 }
35 public void setUsername(String username) {
36 this.username = username;
37 }
38 public String getPassword() {
39 return password;
40 }
41 public void setPassword(String password) {
42 this.password = password;
43 }
44
45 }
add.jsp页面
1 1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
2 2 <%
3 3 String path = request.getContextPath();
4 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
5 5 %>
6 6
7 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
8 8 <html>
9 9 <head>
10 10 <base href="<%=basePath%>">
11 11
12 12 <title>My JSP 'add.jsp' starting page</title>
13 13
14 14 <meta http-equiv="pragma" content="no-cache">
15 15 <meta http-equiv="cache-control" content="no-cache">
16 16 <meta http-equiv="expires" content="0">
17 17 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18 18 <meta http-equiv="description" content="This is my page">
19 19 <!--
20 20 <link rel="stylesheet" type="text/css" href="styles.css">
21 21 -->
22 22
23 23 <script type="text/javascript" src="layer/layer.js"></script>
24 24 <script type="text/javascript">
25 25 function add(a){
26 26 a.submit();
27 27 var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
28 28 parent.ReloadData();
29 29 parent.layer.close(index);
30 30 }
31 31
32 32 </script>
33 33
34 34
35 35 </head>
36 36
37 37 <body>
38 38 <form action="user/add.action" >
39 39 <input type="hidden" name="id">
40 40 <input type="hidden" name="method" value="add">
41 41 <input type="text" name="username" placeholder=" 请输入姓名"><br/>
42 42 <input type="text" name="password" placeholder=" 请输入密码"><br/>
43 43 <input type="button" onclick="javascript:add(this.form)" value="确定添加"><br/>
44 44 </form>
45 45 </body>
46 46 </html>
show.jsp页面
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
2 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
3 <%
4 String path = request.getContextPath();
5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
6 %>
7
8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
9 <html>
10 <head>
11 <base href="<%=basePath%>">
12
13 <title>My JSP 'show.jsp' starting page</title>
14
15 <meta http-equiv="pragma" content="no-cache">
16 <meta http-equiv="cache-control" content="no-cache">
17 <meta http-equiv="expires" content="0">
18 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
19 <meta http-equiv="description" content="This is my page">
20 <!--
21 <link rel="stylesheet" type="text/css" href="styles.css">
22 -->
23 <script type="text/javascript" src="layer/jquery-1.8.2.min.js"></script>
24 <script type="text/javascript" src="layer/layer.js"></script>
25 <script type="text/javascript">
26 function func10() {
27 //iframe层
28 layer.open({
29 type: 2,
30 title: 'layer mobile页',
31 closeBtn: 1, //不显示关闭按钮
32 shadeClose: true,
33 shade: 0.8,
34 area: ['380px', '90%'],
35 content: 'add.jsp' //iframe的url
36 });
37 }
38
39 function func11(id) {
40 //iframe层
41 layer.open({
42 type: 2,
43 title: 'layer mobile页',
44 closeBtn: 1, //不显示关闭按钮
45 shadeClose: true,
46 shade: 0.8,
47 area: ['380px', '90%'],
48 content: 'user/toupdate.action?id='+id //iframe的url
49 });
50 }
51
52
53 function ReloadData(){
54 window.location.reload();
55 }
56 </script>
57 </head>
58
59 <body>
60
61 <table border="1" bordercolor="gray" >
62 <tr>
63 <th>编号</th>
64 <th>姓名</th>
65 <th>密码</th>
66 <th>删除</th>
67 <th>修改</th>
68 </tr>
69 <c:forEach items="${list}" var="a">
70 <tr>
71 <td>${a.id}</td>
72 <td>${a.username}</td>
73 <td>${a.password}</td>
74 <td> <a href="user/deletee.action?id=${a.id}">删除</a></td>
75 <td> <a href="javascript:func11(${a.id})">修改</a></td>
76 </tr>
77 </c:forEach>
78 </table>
79
80 <c:if test="${param.ye>1}">
81 <a href="user/abc?ye=1">首页</a>
82 </c:if>
83 <c:if test="${param.ye>1}">
84 <a href="user/abc?ye=${param.ye-1}">上一页</a>
85 </c:if>
86 <c:forEach begin="1" end="${count}" varStatus="a">
87 <a href="user/abc?ye=${a.index }">${a.index }</a>
88 </c:forEach>
89 <c:if test="${param.ye<count}">
90 <a href="user/abc?ye=${param.ye+1}">下一页</a>
91 </c:if>
92 <c:if test="${param.ye<count}">
93 <a href="user/abc?ye=${count}">尾页</a>
94 </c:if>
95
96
97 <a href="javascript:func10()"> 添加 </a><br/>
98
99
100 </body>
101 </html>
update.jsp页面
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
2 <%
3 String path = request.getContextPath();
4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
5 %>
6
7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
8 <html>
9 <head>
10 <base href="<%=basePath%>">
11
12 <title>My JSP 'update.jsp' starting page</title>
13
14 <meta http-equiv="pragma" content="no-cache">
15 <meta http-equiv="cache-control" content="no-cache">
16 <meta http-equiv="expires" content="0">
17 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18 <meta http-equiv="description" content="This is my page">
19 <!--
20 <link rel="stylesheet" type="text/css" href="styles.css">
21 -->
22 <script type="text/javascript" src="layer/layer.js"></script>
23 <script type="text/javascript">
24 function update(a){
25 a.submit();
26 var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
27 parent.ReloadData();
28 parent.layer.close(index);
29 }
30 </script>
31 </head>
32
33 <body>
34 <form action="user/update.action">
35 <input type="text" name="id" value="${student.id}"><br/>
36 请修改姓名<input type="text" name="username" value="${student.username}"><br/>
37 请修改密码<input type="text" name="password" value="${student.password}"><br/>
38 <input type="button" onclick="javascript:update(this.form)" value="确定修改"><br/>
39
40 </form>
41 </body>
42 </html>
另一张表的添加页面classadd.jsp
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
2 <%
3 String path = request.getContextPath();
4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
5 %>
6
7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
8 <html>
9 <head>
10 <base href="<%=basePath%>">
11
12 <title>My JSP 'add.jsp' starting page</title>
13
14 <meta http-equiv="pragma" content="no-cache">
15 <meta http-equiv="cache-control" content="no-cache">
16 <meta http-equiv="expires" content="0">
17 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18 <meta http-equiv="description" content="This is my page">
19 <!--
20 <link rel="stylesheet" type="text/css" href="styles.css">
21 -->
22 <script type="text/javascript" src="layer/layer.js"></script>
23 <script type="text/javascript">
24 function add(a){
25 a.submit();
26 var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
27 parent.ReloadData();
28 parent.layer.close(index);
29 }
30 </script>
31
32 </head>
33
34 <body>
35 <form action="class/add.action" >
36 <input type="hidden" name="id">
37 <input type="hidden" name="method" value="add">
38 <input type="text" name="studentName" placeholder=" 请输入姓名"><br/>
39 <input type="text" name="color" placeholder=" 请输入颜色"><br/>
40 <input type="button" onclick="javascript:add(this.form)" value="确认添加"><br/>
41 </form>
42 </body>
43 </html>
另一张表修改页面classupdate.jsp
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
2 <%
3 String path = request.getContextPath();
4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
5 %>
6
7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
8 <html>
9 <head>
10 <base href="<%=basePath%>">
11
12 <title>My JSP 'update.jsp' starting page</title>
13
14 <meta http-equiv="pragma" content="no-cache">
15 <meta http-equiv="cache-control" content="no-cache">
16 <meta http-equiv="expires" content="0">
17 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18 <meta http-equiv="description" content="This is my page">
19 <!--
20 <link rel="stylesheet" type="text/css" href="styles.css">
21 -->
22
23 <script type="text/javascript" src="layer/layer.js"></script>
24 <script type="text/javascript">
25 function update(a){
26 a.submit();
27 var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
28 parent.ReloadData();
29 parent.layer.close(index);
30 }
31 </script>
32 </head>
33
34 <body>
35 <form action="class/update.action">
36 <input type="text" name="id" value="${class10.id}"><br/>
37 请修改姓名<input type="text" name="studentName" value="${class10.studentName}"><br/>
38 请修改颜色<input type="text" name="color" value="${class10.color}"><br/>
39 <input type="button" onclick="javascript:update(this.form)" value="确认修改"><br/>
40
41 </form>
42 </body>
43 </html>
另一张表的显示页面classshow.jsp
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
2 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
3 <%
4 String path = request.getContextPath();
5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
6 %>
7
8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
9 <html>
10 <head>
11 <base href="<%=basePath%>">
12
13 <title>My JSP 'show.jsp' starting page</title>
14
15 <meta http-equiv="pragma" content="no-cache">
16 <meta http-equiv="cache-control" content="no-cache">
17 <meta http-equiv="expires" content="0">
18 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
19 <meta http-equiv="description" content="This is my page">
20 <!--
21 <link rel="stylesheet" type="text/css" href="styles.css">
22 -->
23
24 <script type="text/javascript" src="layer/jquery-1.8.2.min.js"></script>
25 <script type="text/javascript" src="layer/layer.js"></script>
26 <script type="text/javascript">
27 function func10() {
28 //iframe层
29 layer.open({
30 type: 2,
31 title: 'layer mobile页',
32 closeBtn: 1, //不显示关闭按钮
33 shadeClose: true,
34 shade: 0.8,
35 area: ['380px', '90%'],
36 content: 'Classadd.jsp' //iframe的url
37 });
38 }
39
40 function func11(id) {
41 //iframe层
42 layer.open({
43 type: 2,
44 title: 'layer mobile页',
45 closeBtn: 1, //不显示关闭按钮
46 shadeClose: true,
47 shade: 0.8,
48 area: ['380px', '90%'],
49 content: 'class/toupdate.action?id='+id //iframe的url
50 });
51 }
52
53
54 function ReloadData(){
55 window.location.reload();
56 }
57 </script>
58
59 </head>
60
61 <body>
62
63 <table border="1" bordercolor="gray" >
64 <tr>
65 <th>编号</th>
66 <th>姓名</th>
67 <th>颜色</th>
68 <th>删除</th>
69 <th>修改</th>
70 </tr>
71 <c:forEach items="${list}" var="a">
72 <tr>
73 <td>${a.id}</td>
74 <td>${a.studentName}</td>
75 <td>${a.color}</td>
76 <td> <a href="class/deletee.action?id=${a.id}">删除</a></td>
77 <td> <a href="javascript:func11(${a.id})">修改</a></td>
78 </tr>
79 </c:forEach>
80 </table>
81
82 <c:if test="${param.ye>1}">
83 <a href="class/abc?ye=1">首页</a>
84 </c:if>
85 <c:if test="${param.ye>1}">
86 <a href="class/abc?ye=${param.ye-1}">上一页</a>
87 </c:if>
88 <c:forEach begin="1" end="${count}" varStatus="a">
89 <a href="class/abc?ye=${a.index }">${a.index }</a>
90 </c:forEach>
91 <c:if test="${param.ye<count}">
92 <a href="class/abc?ye=${param.ye+1}">下一页</a>
93 </c:if>
94 <c:if test="${param.ye<count}">
95 <a href="class/abc?ye=${count}">尾页</a>
96 </c:if>
97
98
99 <a href="javascript:func10()"> 添加 </a><br/>
100
101
102 </body>
103 </html>
效果图
第一次总结可能有不尽如意的地方
逻辑什么的可能有一点乱
慢慢来
慢慢改进