基于Struts2+MySQL的多表出差明细表单
下载地址:http://download.csdn.net/detail/qq_33599520/9790629
项目结构:
UserAction
package com.mstf.action; import com.mstf.entity.Detail; import com.mstf.entity.User; import com.mstf.service.UserService; import com.opensymphony.xwork2.ActionContext; import java.sql.Timestamp; import java.util.List; import java.util.Map; public class UserAction { private String msg; // 登录失败提示信息 private User user; private Detail detail; private String sheng; private String shi; private List<User> userList; // 获得Session Map<String, Object> session = ActionContext.getContext().getSession(); public User getUser() { return user; } public void setUser(User user) { this.user = user; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public List<User> getUserList() { return userList; } public void setUserList(List<User> userList) { this.userList = userList; } public Detail getDetail() { return detail; } public void setDetail(Detail detail) { this.detail = detail; } public String getSheng() { return sheng; } public void setSheng(String sheng) { this.sheng = sheng; } public String getShi() { return shi; } public void setShi(String shi) { this.shi = shi; } // 根据账号查询用户基本信息 public String userInfo() throws Exception { Map<String, Object> session = ActionContext.getContext().getSession(); userList = UserService.userInfo((String) session.get("u_no")); return "infoSucc"; } // 登录 public String login() throws Exception { String u_no = user.getU_no(); String u_pwd = user.getU_pwd(); if (UserService.userLogin(u_no, u_pwd)) { session.put("u_no", u_no); userInfo(); return "loginSucc"; } else { msg = "您输入的账号或密码有误,请稍后重试!"; return "fail"; } } // 退出登录 public String outLogin() { session.remove("u_no"); msg = "退出成功"; return "outSucc"; } // 添加详情 public String add() throws Exception { // 获得Request String u_no = (String) session.get("u_no"); String[] d_city = detail.getD_city(); String d_whither = sheng + shi + ","; String[] d_whithers = d_whither.split(","); String[] d_type = detail.getD_type(); String[] d_intent = detail.getD_intent(); Timestamp[] d_start_date = detail.getD_start_date(); Timestamp[] d_stop_date = detail.getD_stop_date(); int[] d_business_number = detail.getD_business_number(); if ((null == d_city) || (null == d_whithers) || (null == d_intent) || (null == d_business_number)) { session.put("addMsg", "带有*号的是必填项!"); return "addFail"; } int result = UserService.add(d_city, d_whithers, d_type, d_intent, d_start_date, d_stop_date, d_business_number, u_no); if (result > 0) { session.put("addMsg", "添加成功!"); return "addSucc"; } else { session.put("addMsg", "添加失败!"); return "addFail"; } } }
BaseDao
package com.mstf.dao; import com.mstf.db.DbHelper; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; /** * 增删改查公共方法 * @author wangzheng * */ public class BaseDao { private static ResultSet ret; private static int result; // 查询通用方法 public static ResultSet query(String sql, Connection conn, PreparedStatement pst) throws Exception { conn = DbHelper.getConnection(); pst = conn.prepareStatement(sql); ret = pst.executeQuery(); return ret; } // 添加,删除,修改公共方法 public static int add_update_del(Connection conn, PreparedStatement pst, String sql) throws Exception { conn = DbHelper.getConnection(); pst = conn.prepareStatement(sql); result = pst.executeUpdate(); return result; } }
UserDao
package com.mstf.dao; import com.mstf.entity.Detail; import com.mstf.entity.User; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; public class UserDao extends BaseDao { private static List<User> users; private static List<Detail> details; private static User user; private static Detail detail; private static Connection conn; private static String sql; private static PreparedStatement pst; private static ResultSet ret; private static int result; // 登录查询 public static boolean queryLogin(User user) throws Exception { sql = "SELECT * FROM `user` WHERE `u_no` = '" + user.getU_no() + "' AND `u_pwd` = '" + user.getU_pwd() + "'"; ret = BaseDao.query(sql, conn, pst); if (ret.next()) { return true; } return false; } // 查询对应用户基本信息 public static List<User> queryUserInfo(User user) throws Exception { users = new ArrayList<User>(); sql = "SELECT * FROM `user` WHERE `u_no` = '" + user.getU_no() + "'"; ret = BaseDao.query(sql, conn, pst); while (ret.next()) { user = new User(); user.setU_name(ret.getString("u_name")); user.setU_number(ret.getString("u_number")); user.setU_post(ret.getString("u_post")); user.setU_lv(ret.getString("u_lv")); user.setU_position(ret.getString("u_position")); user.setU_company(ret.getString("u_company")); users.add(user); } return users; } // 添加出差详情 public static int add(Detail detail) throws Exception { if ((detail != null) && !detail.equals("")) { for (int i = 0; i < detail.getD_city().length; i++) { sql = "INSERT INTO `detail` VALUES(NULL,'" + detail.getD_city()[i] + "','" + detail.getD_whither()[i] + "','" + detail.getD_type()[i] + "','" + detail.getD_intent()[i] + "','" + detail.getD_start_date()[i] + "','" + detail.getD_stop_date()[i] + "'," + detail.getD_business_number()[i] + ",(SELECT `u_id` FROM `user` WHERE `u_no` = '" + detail.getUser().getU_no() + "'))"; result = BaseDao.add_update_del(conn, pst, sql); } } return result; } }
DbHelper
package com.mstf.db; import java.sql.Connection; import java.sql.DriverManager; public class DbHelper { private static String url = "jdbc:mysql://127.0.0.1:3306/list_hotel"; // 数据库地址 private static String userName = "root"; // 数据库用户名 private static String passWord = "root"; // 数据库密码 private static Connection conn; private DbHelper() { } public static Connection getConnection() { if (null == conn) { try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection(url, userName, passWord); } catch (Exception e) { e.printStackTrace(); } } return conn; } public static void main(String[] args) { // 测试数据库是否连通 System.out.println(getConnection()); } }
Detail
package com.mstf.entity; import java.sql.Timestamp; public class Detail { // 模型类 private int d_id; private String[] d_city; private String[] d_whither; private String[] d_type; private String[] d_intent; private Timestamp[] d_start_date; private Timestamp[] d_stop_date; private int[] d_business_number; private User user; public int getD_id() { return d_id; } public void setD_id(int d_id) { this.d_id = d_id; } public String[] getD_city() { return d_city; } public void setD_city(String[] d_city) { this.d_city = d_city; } public String[] getD_whither() { return d_whither; } public void setD_whither(String[] d_whither) { this.d_whither = d_whither; } public String[] getD_type() { return d_type; } public void setD_type(String[] d_type) { this.d_type = d_type; } public String[] getD_intent() { return d_intent; } public void setD_intent(String[] d_intent) { this.d_intent = d_intent; } public Timestamp[] getD_start_date() { return d_start_date; } public void setD_start_date(Timestamp[] d_start_date) { this.d_start_date = d_start_date; } public Timestamp[] getD_stop_date() { return d_stop_date; } public void setD_stop_date(Timestamp[] d_stop_date) { this.d_stop_date = d_stop_date; } public int[] getD_business_number() { return d_business_number; } public void setD_business_number(int[] d_business_number) { this.d_business_number = d_business_number; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } }
User
package com.mstf.entity; public class User { // 模型类 private int u_id; private String u_no; private String u_pwd; private String u_name; private String u_number; private String u_post; private String u_lv; private String u_position; private String u_company; public int getU_id() { return u_id; } public void setU_id(int u_id) { this.u_id = u_id; } public String getU_no() { return u_no; } public void setU_no(String u_no) { this.u_no = u_no; } public String getU_pwd() { return u_pwd; } public void setU_pwd(String u_pwd) { this.u_pwd = u_pwd; } public String getU_name() { return u_name; } public void setU_name(String u_name) { this.u_name = u_name; } public String getU_number() { return u_number; } public void setU_number(String u_number) { this.u_number = u_number; } public String getU_post() { return u_post; } public void setU_post(String u_post) { this.u_post = u_post; } public String getU_lv() { return u_lv; } public void setU_lv(String u_lv) { this.u_lv = u_lv; } public String getU_position() { return u_position; } public void setU_position(String u_position) { this.u_position = u_position; } public String getU_company() { return u_company; } public void setU_company(String u_company) { this.u_company = u_company; } }
UserService
package com.mstf.service; import com.mstf.dao.UserDao; import com.mstf.entity.Detail; import com.mstf.entity.User; import java.sql.Timestamp; import java.util.List; public class UserService { private static User user; private static Detail detail; // 登录 public static boolean userLogin(String u_no, String u_pwd) throws Exception { user = new User(); user.setU_no(u_no); user.setU_pwd(u_pwd); return UserDao.queryLogin(user); } // 查询用户基本信息 public static List<User> userInfo(String u_no) throws Exception { user = new User(); user.setU_no(u_no); return UserDao.queryUserInfo(user); } // 添加详情 public static int add(String[] d_city, String[] d_whither, String[] d_type, String[] d_intent, Timestamp[] d_start_date, Timestamp[] d_stop_date, int[] d_business_number, String u_no) throws Exception { user = new User(); user.setU_no(u_no); detail = new Detail(); detail.setD_city(d_city); detail.setD_whither(d_whither); detail.setD_type(d_type); detail.setD_intent(d_intent); detail.setD_start_date(d_start_date); detail.setD_stop_date(d_stop_date); detail.setD_business_number(d_business_number); detail.setUser(user); return UserDao.add(detail); } }
struts.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <!-- 热部署 --> <constant name="struts.devMode" value="true"></constant> <!-- 指定语言 --> <constant name="struts.locale" value="zh_CN"></constant> <!-- 指定编码 --> <constant name="struts.i18n.encoding" value="utf-8"></constant> <!-- 是否支持动态方法调用 --> <constant name="struts.enable.DynamicMethodInvocation" value="true"/> <package name="mstf" extends="struts-default" namespace="/"> <!-- 默认界面 --> <!--<default-action-ref name="login"></default-action-ref>--> <action name="login"> <result>/login.jsp</result> </action> <action name="UserAction" class="com.mstf.action.UserAction"> <!-- 登录 --> <result name="loginSucc">/index.jsp</result> <result name="fail">/login.jsp</result> <!-- 退出登录 --> <result name="outSucc">/login.jsp</result> <!-- 用户信息 --> <result name="infoSucc">/index.jsp</result> <!-- 添加出差详情 --> <result name="addSucc" type="redirectAction"> <param name="actionName">UserAction!userInfo</param> </result> <result name="addFail" type="redirectAction"> <param name="actionName">UserAction!userInfo</param> </result> </action> </package> </struts>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <!-- 过滤器 用于初始化struts2 --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <!-- 用于struts2 的过滤器映射 --> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
index.css
.center{text-align: center;} .login-page { width: 360px; padding: 8% 0 0; margin: auto; } .form { position: relative; z-index: 1; background: #FFFFFF; max-width: 360px; margin: 0 auto 100px; padding: 45px; text-align: center; box-shadow: 0 0 20px 0 rgba(0, 0, 0, 0.2), 0 5px 5px 0 rgba(0, 0, 0, 0.24); } .form input { font-family: "Roboto", sans-serif; outline: 0; background: #f2f2f2; width: 100%; border: 0; margin: 0 0 15px; padding: 15px; box-sizing: border-box; font-size: 14px; } .form button { font-family: "Microsoft YaHei","Roboto", sans-serif; text-transform: uppercase; outline: 0; background: #4CAF50; width: 100%; border: 0; padding: 15px; color: #FFFFFF; font-size: 14px; -webkit-transition: all 0.3 ease; transition: all 0.3 ease; cursor: pointer; } .form button:hover,.form button:active,.form button:focus { background: #43A047; } .form .message { margin: 15px 0 0; color: #b3b3b3; font-size: 12px; } .form .message a { color: #4CAF50; text-decoration: none; } .form .register-form { display: none; } .container { position: relative; z-index: 1; max-width: 300px; margin: 0 auto; } .container:before, .container:after { content: ""; display: block; clear: both; } .container .info { margin: 50px auto; text-align: center; } .container .info h1 { margin: 0 0 15px; padding: 0; font-size: 36px; font-weight: 300; color: #1a1a1a; } .container .info span { color: #4d4d4d; font-size: 12px; } .container .info span a { color: #000000; text-decoration: none; } .container .info span .fa { color: #EF3B3A; } body { background: #76b852; /* fallback for old browsers */ background: -webkit-linear-gradient(right, #76b852, #8DC26F); background: -moz-linear-gradient(right, #76b852, #8DC26F); background: -o-linear-gradient(right, #76b852, #8DC26F); background: linear-gradient(to left, #76b852, #8DC26F); font-family: "Roboto", sans-serif; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; } .shake_effect{ -webkit-animation-name: shake; animation-name: shake; -webkit-animation-duration: 1s; animation-duration: 1s; } @-webkit-keyframes shake { from, to { -webkit-transform: translate3d(0, 0, 0); transform: translate3d(0, 0, 0); } 10%, 30%, 50%, 70%, 90% { -webkit-transform: translate3d(-10px, 0, 0); transform: translate3d(-10px, 0, 0); } 20%, 40%, 60%, 80% { -webkit-transform: translate3d(10px, 0, 0); transform: translate3d(10px, 0, 0); } } @keyframes shake { from, to { -webkit-transform: translate3d(0, 0, 0); transform: translate3d(0, 0, 0); } 10%, 30%, 50%, 70%, 90% { -webkit-transform: translate3d(-10px, 0, 0); transform: translate3d(-10px, 0, 0); } 20%, 40%, 60%, 80% { -webkit-transform: translate3d(10px, 0, 0); transform: translate3d(10px, 0, 0); } } p.center{ color: #fff;font-family: "Microsoft YaHei"; }
index.js
var index=0; function del_detail(val){ if(index==0){ alert("最后一个不能删"); }else{ if(index>0){ $(val).parent().parent().parent().parent().parent().remove(); index--; } } } function add_detail(){ if(index==1){ alert("最多只能添加2个"); }else{ if(index<1){ $("#business_detail").clone().insertAfter("#business_detail_new"); index++; } } } /* 以下为省市级联 */ var array = new Array(); array["湖北省"] = ["潜江","荆门","荆州","武汉","孝感","十堰","襄阳","黄冈","恩施","天门"]; array["湖南省"] = ["海南","海口"]; function a(){ for (var index in array) { var opt = document.createElement("option"); opt.value = index; opt.text = index; document.getElementById("sel").appendChild(opt); } c(); } function b(){ document.getElementById("selone").innerHTML=""; var sheng = document.getElementById("sel").value; for (var i=1;i<sheng.length;i++) { var opt = document.createElement("option"); opt.value = array[sheng][i-1]; opt.text = array[sheng][i-1]; document.getElementById("selone").appendChild(opt); } } function c(){ document.getElementById("selone").innerHTML=""; var sheng = document.getElementById("sel").value; var shi=array[sheng]; for (var i=0;i<shi.length;i++) { var opt=document.createElement("option"); opt.value=shi[i]; opt.text=shi[i]; document.getElementById("selone").appendChild(opt); } }
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>出差申请表</title> <script src="js/jquery-2.1.1.min.js" type="text/javascript"></script> <script src="js/index.js" type="text/javascript"></script> <style type="text/css"> .showText{ border: none; margin-left: 5px; margin-bottom: 2px; } </style> </head> <body onload="a()"> <center> <h1>出差申请表</h1> <h4> <font color="red">当前登录用户:${sessionScope.u_no}</font> <a href="UserAction!outLogin">退出登录</a> </h4> <fieldset style="width: 600px"> <legend> <h2>申请人信息</h2> </legend> <table> <tr> <td> </td> </tr> <!--个人信息--> <c:forEach items="${userList}" var="user"> <tr> <td>申请人: <input value="${user.u_name}" class="showText" readonly="readonly"> </td> <td>工号: <input value="${user.u_number}" class="showText" readonly="readonly"> </td> </tr> <tr> <td>部门: <input value="${user.u_post}" class="showText" readonly="readonly"> </td> <td>等级: <input value="${user.u_lv}" class="showText" readonly="readonly"> </td> </tr> <tr> <td>职位: <input name="user_post_name" value="${user.u_position}" class="showText" readonly="readonly"> </td> <td>所属公司: <input name="user_company" value="${user.u_company}" class="showText" readonly="readonly"> </td> </tr> </c:forEach> </table> </fieldset> <br /> <!--出差明细--> <form action="UserAction!add" method="post"> <fieldset style="width: 600px" id="business_detail"> <legend> <h2>出差明细</h2> </legend> <table> <tr> <td> <font color="red">*</font> 出发城市: <input name="detail.d_city"> </td> <td> <input type="button" onclick="del_detail(this)" value="删除"> </td> </tr> <tr> <td> <font color="red">*</font>出差地点: 省份:<select id="sel" onchange="c()" name="sheng"></select> 城市:<select id="selone" name="shi"></select> </td> </tr> <tr> <td> <font color="red">*</font> 出差类型: <select name="detail.d_type"> <option value="出差">出差</option> </select> </td> </tr> <tr> <td colspan="2"> <font color="red">*</font> 出差目的: <textarea name="detail.d_intent" placeholder="目的" style="width: 500px; height: 100px;"></textarea> </td> </tr> <tr> <td> <font color="red">*</font> 预计时间: <input type="datetime-local" value="2015-09-24T13:00:00" name="detail.d_start_date"/> </td> <td> <input type="datetime-local" value="2015-09-24T13:00:00" name="detail.d_stop_date"/> </td> </tr> <tr> <td> 预计出差天数 :<input name="detail.d_business_number" placeholder="预计出差天数" /> </td> </tr> </table> </fieldset> <div id="business_detail_new"></div> <br> <input type="button" onclick="add_detail()" value="增加"/> <input type="submit" value="提交" /> <font color="red">${sessionScope.addMsg}</font> </form> </center> </body> </html>
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>登录</title> <link href="css/index.css" rel="stylesheet" type="text/css"/> <script src="js/jquery-2.1.1.min.js" type="text/javascript"></script> <script src="js/index.js" type="text/javascript"></script> </head> <body> <div class="htmleaf-container"> <div id="wrapper" class="login-page"> <div id="login_form" class="form"> <form class="login-form" action="UserAction!login" method="post"> <input type="text" placeholder="帐号" id="user_name" name="user.u_no"/> <input type="password" placeholder="密码" id="password" name="user.u_pwd"/> <input type="submit" value="登录" id="login"> <p class="message"> 还没账号? <a href="#">立即注册</a> </p> <p> <font color="red">${requestScope.msg}</font> </p> </form> </div> </div> </div> </body> </html>
我们有两个方法来进行软件设计:一个是让其足够的简单以至于让BUG无法藏身;另一个就是让其足够的复杂,让人找不到BUG。前者更难一些。