黑马旅游网--》后台
1、登录页面与index页面
login.html
index.html
在index中做好用户名的回显 以及退出销毁session
2、用户管理
2.1后台servlet
package com.project.travelbg.web.servlet;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.project.travelbg.domain.ResultInfo;
import com.project.travelbg.domain.User;
import com.project.travelbg.service.Impl.UserSerivceImpl;
import com.project.travelbg.service.UserSerivce;
import org.apache.commons.beanutils.BeanUtils;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;
/**
* @description:user的servlet去继承baseservlet
* @author: Sw_Ljb
* @PACKAGE_NAME:cn.itcast.travel.web.servlet
* @time: 2022/5/29 上午10:52
* @version: 1.0
*/
@WebServlet("/user/*")
public class UserServlet extends BaseServlet{
UserSerivce userSerivce = new UserSerivceImpl();
/**
* 注册方法
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
public void regist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("regist...");
//获取参数
Map<String, String[]> registMap = request.getParameterMap();
//封装数据
User user = new User();
try {
BeanUtils.populate(user, registMap);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
//调用service 去想数据库中insert一条数据
Boolean flag = userSerivce.regist(user);
ResultInfo info = new ResultInfo();
if (flag) {
//给前台响应 并且重定向到 登录页面
System.out.println("注册成功");
info.setFlag(true);
} else {
//给前台响应 并且重定向到 登录页面
System.out.println("该用户名存在,注册失败,请刷新验证码!");
info.setFlag(false);
info.setErrorMsg("用户名存在,注册失败!!");
}
ObjectMapper mapper = new ObjectMapper();
String s = mapper.writeValueAsString(info);
response.setContentType("application/json;charset=utf-8");
response.getWriter().write(s);
}
/**
* 激活用户方法
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
public void active(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取激活码
String code = request.getParameter("code");
//判断激活码是否为空
if (code!=null){
UserSerivce userSerivce = new UserSerivceImpl();
//根据code去完成激活
boolean flag = userSerivce.activeUser(code);
//判断激活状态
String msg = "";
if (flag){
//msg="激活成功!请点击<a href='http://localhost/travel/login.html'>登录</a>";
msg="激活成功";
}else {
msg="激活失败";
}
//数据响应回前台
response.setContentType("text/html;charset=utf-8");
response.getWriter().write(msg);
}
}
/**
* 查询所有的用户
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
public void findAllUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//没有参数处理
//调用service返回一个list<user>的数据
List<User> userList = userSerivce.findAllUser();
//将这个集合写会前台
writerValue(userList,response);
}
/**
* 根绝用户名来修改用户的姓名、密码、联系方式
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
public void updateUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取参数
/* String username = request.getParameter("username");
String password = request.getParameter("password");
String name = request.getParameter("name");
String telephone = request.getParameter("telephone");*/
Map<String, String[]> userMap = request.getParameterMap();
//封装数据
User user = new User();
try {
BeanUtils.populate(user,userMap);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
//ceshi
System.out.println(user.getUsername());
System.out.println(user.getPassword());
System.out.println(user.getTelephone());
System.out.println(user.getStatus());
//调用service进行修改操作 ResultInfo
ResultInfo info = userSerivce.modifyUserByUserName(user);
//将这个返回值返回前端 序列化回前端
writerValue(info,response);
}
/**
* 根据用户名删除user
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
public void deleteUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//接受参数
String username = request.getParameter("username");
//System.out.println(username);
//调用service去删除用户 返回一个ResultInfo
ResultInfo info = userSerivce.deleteUserByUserName(username);
//将这个info写会到前台
writerValue(info,response);
}
}
2.2前台显示与请求
2.2.1查询所有用户
2.2.1.1前台显示:
页码问题是由于 这些页面是通过前台插件实现的
2.2.1.2前台异步请求
<script>
$.post('user/findAllUser',{},function (data) {
//data数据的处理
var trs;//所有的行 一次循环 解决一行
for (var i = 0; i < data.length; i++) {
var tr ='<tr class="gradeX">'
tr+='<td>'+data[i].uid+'</td>';
tr+='<td>'+data[i].username+'</td>';
tr+='<td>'+data[i].password+'</td>';
tr+='<td>'+data[i].name+'</td>';
tr+='<td>'+data[i].birthday+'</td>';
tr+='<td>'+data[i].sex+'</td>';
tr+='<td>'+data[i].telephone+'</td>';
tr+='<td>'+data[i].email+'</td>';
tr+='<td>'+data[i].status+'</td>';
tr+='<td>'+data[i].code+'</td>';
tr+='</tr>'
trs+=tr;
}
$("#tbody_tb").html(trs);
})
</script>
2.2.2 用户新增
2.2.2.1 异步请求
<script>
$(function () {
$("#sub_btn").click(function () {
//现将表单中的数据转为json jquery 提供了 一个方法
$.post("user/regist", $($("#signupForm")).serialize(), function (data) {
//根据返回回来的data的返回值去判断 是否不存在
if (data.flag) {
alert("注册成功!!")
location.href = "stu_list.html";
} else {
alert(data.errorMsg);
}
})
})
})
</script>
2.2.3 用户修改
2.2.3.1 异步请求
<script>
$(function () {
$("#sub_btn").click(function () {
//现将表单中的数据转为json jquery 提供了 一个方法
$.post("user/updateUser", $($("#signupForm")).serialize(), function (data) {
//根据返回回来的data的返回值去判断 是否不存在
if (data.flag) {
//修改成功
alert("修改成功");
location.href='usermodify.html';
} else {
//修改失败
alert(data.errorMsg);
}
})
})
})
</script>
2.2.4 用户删除
2.2.4.1 异步请求
<script>
$(function () {
$("#sub_btn").click(function () {
//现将表单中的数据转为json jquery 提供了 一个方法
$.post("user/deleteUser", $($("#signupForm")).serialize(), function (data) {
//根据返回回来的data的返回值去判断 是否不存在
if (data.flag) {
//修改成功
alert(data.errorMsg);
location.href='stu_list.html';
} else {
//修改失败
alert(data.errorMsg);
}
})
})
})
</script>
3、酒店管理
3.1 后台处理servlet
package com.project.travelbg.web.servlet;
import com.project.travelbg.domain.Hotel;
import com.project.travelbg.domain.ResultInfo;
import com.project.travelbg.service.HotelService;
import com.project.travelbg.service.Impl.HotelServiceImpl;
import org.apache.commons.beanutils.BeanUtils;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;
/**
* @description:
* @author: Sw_Ljb
* @PACKAGE_NAME:${PACKAGE_NAME}
* @time: 2022/6/1 下午6:48
* @version: 1.0
*/
@WebServlet("/hotel/*")
public class HotelServlet extends BaseServlet {
//酒店信息的业务逻辑的实现方式
private HotelService hotelService = new HotelServiceImpl();
/**
* 查询所有酒店信息的方法
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
public void findHotel(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//不用接受参数
//直接调用service进行处理
List<Hotel> hotelList = hotelService.findAllHotel();
//返回给前台
writerValue(hotelList,response);
}
/**
* 查询莫一酒店id是否存在
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
public void findHotelByHid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ResultInfo info = new ResultInfo();
//1、接受hid参数 处理参数
String _hid = request.getParameter("hid");
if (_hid.length()==0||_hid==null||"null".equals(_hid)){
info.setFlag(false);
info.setErrorMsg("输入的hid不可为空!!");
}else {
//参数处理
int hid = Integer.parseInt(_hid);
//2、调用service进行操作 返回一个resultInfo()
info = hotelService.findHotelByHid(hid);
}
//3、写回前台
writerValue(info,response);
}
/**
* 根据hid删除一条酒店信息
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
public void delHotelByHidOrHhame(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//接受参数 并处理参数
int hid;
String _hid = request.getParameter("hid");
if (_hid==null||_hid.length()==0||"null".equals(_hid)){
hid=0;
}else hid = Integer.parseInt(_hid);
String hname = request.getParameter("hname");
//调用service进行删除操作 返回一个resultInfo对象
ResultInfo info = hotelService.deleteHotelByHidOrHhame(hid,hname);
//将info返回给前台
writerValue(info,response);
}
/**
* 根据hid修改一条酒店信息 酒店名 价格
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
public void modifyHotelByHid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//接受参数
//接受参数 并处理参数
int hid;
String _hid = request.getParameter("hid");
if (_hid==null||_hid.length()==0||"null".equals(_hid)){
hid=0;
}else hid = Integer.parseInt(_hid);
String hname = request.getParameter("hname");
double price;
String _price = request.getParameter("price");
if (_price==null||_price.length()==0||"null".equals(_price)){
price=0.0;
}else price = Double.parseDouble(_price);
//调用service进行处理
ResultInfo info = hotelService.modifyHotelDetailByHid(hid,hname,price);
//返回前端
writerValue(info,response);
}
/**
* 增加一条酒店信息
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
public void addHotel(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//封装数据
Hotel hotel = new Hotel();
//获取参数
Map<String, String[]> hotelMap = request.getParameterMap();
//封装数据
try {
BeanUtils.populate(hotel,hotelMap);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
//调用service进行增加操作resultInfo对象
ResultInfo info = hotelService.addHotelDetails(hotel);
//将resultinfo返回给前台显示
writerValue(info,response);
}
}
3.2 前台显示
3.2.1 查询所有酒店
3.2.1.1 异步提交
<script>
$.post('hotel/findHotel',{},function (data) {
//data数据的处理
var trs;//所有的行 一次循环 解决一行
for (var i = 0; i < data.length; i++) {
var tr ='<tr class="gradeX">'
tr+='<td>'+data[i].hid+'</td>';
tr+='<td>'+data[i].hname+'</td>';
tr+='<td>'+data[i].price+'</td>';
tr+='<td>'+data[i].hoteIntroduce+'</td>';
tr+='<td>'+data[i].hflag+'</td>';
tr+='<td>'+data[i].hdate+'</td>';
tr+='<td>'+data[i].isThemeTour+'</td>';
tr+='<td>'+data[i].count+'</td>';
tr+='<td>'+data[i].cid+'</td>';
//tr+='<td>'+data[i].himage+'</td>';
tr+='<td><img src="'+data[i].himage+'" width="50px" height="50px"></td>';
tr+='<td>'+data[i].sid+'</td>';
tr+='<td>'+data[i].sourceId+'</td>';
tr+='</tr>'
trs+=tr;
}
$("#tbody_tb").html(trs);
})
</script>
3.2.2 酒店新增
3.2.2.1 异步提交
<script>
$(function () {
$("#sub_btn").click(function () {
//现将表单中的数据转为json jquery 提供了 一个方法
$.post("hotel/addHotel", $($("#signupForm")).serialize(), function (data) {
//根据返回回来的data的返回值去判断 是否不存在
if (data.flag) {
alert("增加成功!!")
location.href = "hotel_list.html";
} else {
alert(data.errorMsg);
location.href = "hotel_add.html";
}
})
})
})
</script>
3.2.3 酒店修改
3.2.3.1 异步提交
<script>
$(function () {
$("#sub_btn").click(function () {
//现将表单中的数据转为json jquery 提供了 一个方法
$.post("hotel/modifyHotelByHid", $($("#signupForm")).serialize(), function (data) {
//根据返回回来的data的返回值去判断 是否不存在
if (data.flag) {
//修改成功
alert("修改成功");
location.href='hotel_list.html';
} else {
//修改失败
alert(data.errorMsg);
location.href='hotel_modify.html';
}
})
})
})
</script>
3.2.4 酒店删除
3.2.4.1异步提交
<script>
$(function () {
$("#sub_btn").click(function () {
//现将表单中的数据转为json jquery 提供了 一个方法
$.post("hotel/delHotelByHidOrHhame", $($("#signupForm")).serialize(), function (data) {
//根据返回回来的data的返回值去判断 是否不存在
if (data.flag) {
//修改成功
alert(data.errorMsg);
location.href='hotel_list.html';
} else {
//修改失败
alert(data.errorMsg);
location.href='hotel_del.html';
}
})
})
})
</script>
4、酒店订单
3.1后台servlet
package com.project.travelbg.web.servlet;
import com.project.travelbg.domain.ResultInfo;
import com.project.travelbg.domain.bookhotel;
import com.project.travelbg.service.BookHotelService;
import com.project.travelbg.service.Impl.BookHotelServiceImpl;
import org.apache.commons.beanutils.BeanUtils;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;
/**
* @description:图书订单的serlvet
* @author: Sw_Ljb
* @PACKAGE_NAME:${PACKAGE_NAME}
* @time: 2022/6/6 下午3:38
* @version: 1.0
*/
@WebServlet("/bookhotel/*")
public class BookHotelServlet extends BaseServlet {
//酒店信息的service
private BookHotelService bookHotelService = new BookHotelServiceImpl();
/**
* 查询所有的酒店预定订单信息
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
public void findAllBookHotelDetails(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//无参数 只是html发送了一个请求==》服务器去处理这个查询所有订单信息的请求
//1、直接调用service去查询所有的订单信息 返回一个list<bookhotel> 集合
List<bookhotel> bookHotelList = bookHotelService.fingAllBookHotel();
//返回给将这个集合 转为json 返回给前台
writerValue(bookHotelList,response);
}
/**
* 通过bhid查询一条预定酒店信息
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
public void findBookHotelByBhId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1、获取参数 、处理参数
ResultInfo info = new ResultInfo();
String _Bhid = request.getParameter("Bhid");
if (_Bhid.length()==0||_Bhid==null||"null".equals(_Bhid)){
info.setFlag(false);
info.setErrorMsg("输入的hid不可为空!!");
}else {
//参数处理
int Bhid = Integer.parseInt(_Bhid);
//2、调用service进行操作 返回一个resultInfo()
info = bookHotelService.findHotelByBhid(Bhid);
}
//3、写回前台
writerValue(info,response);
}
/**
* 管理员新增一条酒店预定信息
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
public void addBookHotelInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1、接受参数
Map<String, String[]> bookhotelMap = request.getParameterMap();
bookhotel bh = new bookhotel();
//2、封装数据
try {
BeanUtils.populate(bh,bookhotelMap);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
//3、调用service处理
ResultInfo info = bookHotelService.addBookHotelByAdmin(bh);
//4、返回给前台
writerValue(info,response);
}
/**
* 通过订单id去修改订单状态
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
public void modifyBookHotelByBhid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ResultInfo info = new ResultInfo();
//1、接受参数 bhid status
String _Bhid = request.getParameter("bhid");
String status = request.getParameter("status");
//2、处理参数
if ((_Bhid.length()!=0&&_Bhid!=null&&!"null".equals(_Bhid) )&& (status.length()!=0&&status!=null&&!"null".equals(status))){
//两个参数都不为空
//3、调用service进行处理 返回一个resuilinfo对象
info = bookHotelService.modifyBookHotelStatus(_Bhid,status);
}else {
info.setFlag(false);
info.setErrorMsg("输入不可为空!!");
}
//4、将info对象写回前台
writerValue(info,response);
}
/**
* 通过bhid驳回一条订单id
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
public void rejectedBookHotelByBhid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ResultInfo info = new ResultInfo();
//1、接受参数bhid 并处理
String _Bhid = request.getParameter("bhid");
if (_Bhid.length()==0||_Bhid==null||"null".equals(_Bhid)){
info.setFlag(false);
info.setErrorMsg("输入的hid不可为空!!");
}else {
//参数处理
int Bhid = Integer.parseInt(_Bhid);
//2、调用service进行操作 返回一个resultInfo()
info = bookHotelService.rejectedBookHotelByBhid(Bhid);
}
//3、写回前台
writerValue(info,response);
}
}
3.2 前台
3.2.1 查询所有酒店订单信息
3.2.1.1异步提交参数
<script>
$.post('bookhotel/findAllBookHotelDetails',{},function (data) {
//data数据的处理
var trs;//所有的行 一次循环 解决一行
for (var i = 0; i < data.length; i++) {
var tr ='<tr class="gradeX">'
tr+='<td>'+data[i].bhid+'</td>';
tr+='<td>'+data[i].hid+'</td>';
tr+='<td>'+data[i].date+'</td>';
tr+='<td>'+data[i].uid+'</td>';
tr+='<td>'+data[i].status+'</td>';
tr+='</tr>';
trs+=tr;
}
$("#tbody_tb").html(trs);
})
</script>
3.2.2 新增预定酒店信息
3.2.2.1异步提交参数
<script>
$(function () {
//输入hid后 绑定一个鼠标离开时间 存不存在 返回一个值
$("#firstname").blur(function () {
var hid = $("#firstname").val();
$.post("hotel/findHotelByHid",{hid:hid},function (data) {
if (data.flag){
//存在
alert("存在");
}else {
//不存在
alert("不存在");
}
})
})
$("#sub_btn").click(function () {
//现将表单中的数据转为json jquery 提供了 一个方法
$.post("bookhotel/addBookHotelInfo", $($("#signupForm")).serialize(), function (data) {
//根据返回回来的data的返回值去判断 是否不存在
if (data.flag) {
alert("增加成功!!")
location.href = "bookhotel_list.html";
} else {
alert(data.errorMsg);
location.href = "bookhotel_add.html";
}
})
})
})
</script>
3.2.3 修改酒店订单信息
3.2.3.1异步提交数据
<script>
$(function () {
$("#bhid").blur(function () {
var Bhid = $("#bhid").val();
$.post("bookhotel/findBookHotelByBhId",{Bhid:Bhid},function (data) {
if (data.flag){
//存在
alert("存在");
}else {
//不存在
alert("不存在");
}
})
})
$("#sub_btn").click(function () {
//现将表单中的数据转为json jquery 提供了 一个方法
$.post("bookhotel/modifyBookHotelByBhid", $($("#signupForm")).serialize(), function (data) {
//根据返回回来的data的返回值去判断 是否不存在
if (data.flag) {
//修改成功
alert("修改成功");
location.href='bookhotel_list.html';
} else {
//修改失败
alert(data.errorMsg);
location.href='bookhotel_modify.html';
}
})
})
})
</script>
3.2.4 酒店订单确认与驳回
3.2.4.1确认
订单确认 就是将订单状态设置为Y(相当于修改)
3.2.4.2驳回
驳回就是删除 这条信息
3.2.4.2.1异步提交
<script>
$(function () {
$("#bhid").blur(function () {
var Bhid = $("#bhid").val();
$.post("bookhotel/findBookHotelByBhId",{Bhid:Bhid},function (data) {
if (data.flag){
//存在
alert("存在");
}else {
//不存在
alert("不存在");
}
})
})
$("#sub_btn").click(function () {
//现将表单中的数据转为json jquery 提供了 一个方法
$.post("bookhotel/rejectedBookHotelByBhid", $($("#signupForm")).serialize(), function (data) {
//根据返回回来的data的返回值去判断 是否不存在
if (data.flag) {
//驳回成功
alert(data.errorMsg);
location.href='bookhotel_list.html';
} else {
//驳回失败
alert(data.errorMsg);
location.href='bookhotel_del.html';
}
})
})
})
</script>
4项目自提(数据库自建)
https://gitee.com/sw-ljb/travel-bk