package com.handson.controller;
import java.io.IOException;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.handson.entity.Tally;
import com.handson.entity.User;
import com.handson.services.TallyService;
/**
*
* @ClassName: TallyController
* @Description: 控制器
* @author Tiankuan
* @date 2016-12-24 下午7:16:39
*
*/
public class TallyController extends HttpServlet {
/**
* @Fields serialVersionUID : 序列化
*/
private static final long serialVersionUID = 1L;
private TallyService taSer = new TallyService();
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
String url = request.getServletPath();
System.out.println(url);
if (url.equalsIgnoreCase("/index.do")) {
index(request, response);
} else if (url.equalsIgnoreCase("/delete.do")) {
delete(request, response);}
else if (url.equalsIgnoreCase("/update.do")) {
update(request, response);}
else if(url.equalsIgnoreCase("/add.do")){
add(request, response);}
else if(url.equalsIgnoreCase("/toEdit.do")){
toEdit(request, response);
}else if (url.equalsIgnoreCase("/login.do")) {
login(request, response);
}else if (url.equalsIgnoreCase("/logout.do")) {
logout(request, response);
}
else if (url.equalsIgnoreCase("/search.do")) {
search(request, response);
}
else {
request.getRequestDispatcher("/index.do")
.forward(request, response);
}
}
private void toEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
int id =Integer.parseInt(request.getParameter("id"));
System.out.println(id);
Tally t = taSer.selectByID(id);
System.out.println(t);
request.setAttribute("ts", t);
request.getRequestDispatcher("update.jsp").forward(request, response);
}
// private void login(HttpServletRequest request, HttpServletResponse response) {
// // TODO Auto-generated method stub
// String name = (String)request.getParameter("username");//获取用户名
// String pwd = (String)request.getParameter("password");//获取密码
// List<User> users=taSer.login();
// for (User user:users) {
// if (user.getPassword()==pwd&&user.getUsername()==name) {
// request.getRequestDispatcher("index.jsp").forward(request, response);
// }else{
// }
// }
//
// }
/**
* @throws IOException
* @throws ServletException
*
* @Title: search
* @Description: 根据关键字搜索
* @author Tiankuan
* @param request
* @param response
* @throws
*/
private void search(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String keyWord = (String)request.getParameter("keyWord");// 获取类型为前段搜索框字符串
System.out.println("获取前台关键字字符串:"+keyWord);
List<Tally> ts=taSer.search(keyWord);
System.out.println("搜索获取:"+ts);
request.setAttribute("ts", ts);
request.getRequestDispatcher("index.jsp").forward(request, response);
}
/**
* @throws IOException
* @throws ServletException
*
* @Title: add
* @Description: 增加单条账目
* @author Tiankuan
* @param request
* @param response
* @throws
*/
private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String type = request.getParameter("type");
float money = Float.parseFloat(request.getParameter("money"));
String date = request.getParameter("date");
String remark = request.getParameter("remark");
System.out.println(type);
System.out.println(money);
System.out.println(date);
System.out.println(remark);
System.out.println(date);
Tally t = new Tally();
t.setDate(date);
t.setMoney(money);
t.setRemark(remark);
t.setType(type);
boolean flag = taSer.insert(t);
response.sendRedirect("index.do");
// request.getRequestDispatcher("index.do").forward(request, response);
}
/**
* @throws IOException
* @throws ServletException
*
* @Title: updateStudent
* @Description: 修改学生信息
* @author Tiankuan
* @param request
* @param response
* @throws
*/
private void update(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
// 获取前端账目集合
int id =Integer.parseInt(request.getParameter("id"));
System.out.println("update"+id);
String type = (String)request.getParameter("type");
float money = Float.parseFloat(request.getParameter("money"));
String date = request.getParameter("date");
String remark = request.getParameter("remark");
System.out.println("修改 控制器获取参数:"+id+" "+type+" "+money+" "+date+" "+remark);
Tally t = new Tally();
t.setDate(date);
t.setMoney(money);
t.setRemark(remark);
t.setType(type);
t.setId(id);
boolean flag = taSer.update(t);
response.sendRedirect("index.do");
// request.getRequestDispatcher("index.do").forward(request, response);
}
/**
* @throws IOException
* @throws ServletException
*
* @Title: delete
* @Description: 删除账目
* @author Tiankuan
* @param request
* @param response
* @throws
*/
private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String sid = request.getParameter("id");// 获取前端ID
System.out.println(sid);
String[] item = sid.split(",");
for (int i = 0; i < item.length; i++) {
System.out.println(item[i]);
boolean flag = taSer.delete(Integer.parseInt(item[i]));
}
response.sendRedirect("index.do");
}
/**
* @throws IOException
* @throws ServletException
*
* @Title: index
* @Description: 用于跳转主页
* @author Tiankuan
* @param request
* @param response
* @throws
*/
private void index(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
List<Tally> t = taSer.select();
System.out.println(t);
request.setAttribute("ts", t);
request.getRequestDispatcher("index.jsp").forward(request, response);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req, resp);
// TODO Auto-generated method stub
}
private void login(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name = (String)request.getParameter("username");//获取用户名
String pwd = (String)request.getParameter("password");//获取密码
if(name != null && pwd != null && name.equals("admin") && pwd.equals("admin")){//通过验证
User u = new User(); //创建用户对象,记录登录用户信息
u.setId(1);
u.setLoginName(name);
u.setPassword(pwd);
HttpSession session = request.getSession();//获取会话对象
session.setAttribute("LOGIN_USER",u);//将登录用户信息保存到会话对象
request.getRequestDispatcher("/index.do").forward(
request, response);//转到登录页面
// index(request, response);//转到首页
}
else{//验证未通过
request.setAttribute("LOGIN_ERROR", "用户名密码都必须是 admin");