Java實戰(zhàn)之火車票預(yù)訂系統(tǒng)的實現(xiàn)
一、項目運行
環(huán)境配置:
Jdk1.8 + Tomcat8.5 + mysql + Eclispe(IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持)
項目技術(shù):
JSP + Servlert + html+ css + JavaScript + JQuery + Ajax 等等;
二、效果圖







三、核心代碼
個人中心Controller
/**
* 個人中心Controller
*/
@Controller
public class UserInforController {
@Autowired
private UserInforServiceImpl userInforService = null;
/**
* 修改密碼操作
* @param oldPassword
* @param newPassword
* @param rePassword
* @param httpSession
* @return
*/
@RequestMapping("changePassword.do")
@ResponseBody
public Map<String, String> changePassword(String oldPassword, String newPassword,
String rePassword, HttpSession httpSession){
HashMap<String, String> map = new HashMap<String, String>();
if (newPassword.equals(rePassword)){
SystemManager admin = (SystemManager) httpSession.getAttribute("admin");
String encodeByMD5 = MD5Utils.encodeByMD5(oldPassword);
if (encodeByMD5.equals(admin.getSmPassword())){
String newPasswords = MD5Utils.encodeByMD5(newPassword);
admin.setSmPassword(newPasswords);
userInforService.updateSystemManagePassword(admin.getSmId(),admin);
map.put("type","success");
map.put("msg","密碼修改成功");
return map;
}else{
map.put("type","error");
map.put("msg","原密碼錯誤");
return map;
}
}else{
map.put("type","error");
map.put("msg","兩次密碼不一致");
return map;
}
}
/**
* 員工修改個人密碼
* @param oldPassword
* @param newPassword
* @param rePassword
* @param httpSession
* @return
*/
@RequestMapping("changeEmployeePassword.do")
@ResponseBody
public Map<String, String> changeEmployeePassword(String oldPassword, String newPassword,
String rePassword, HttpSession httpSession){
HashMap<String, String> map = new HashMap<String, String>();
if (newPassword.equals(rePassword)){
Integer eid = (Integer) httpSession.getAttribute("employeeId");
try {
userInforService.updateEmployeePassword(eid, oldPassword, newPassword);
map.put("type","success");
map.put("msg","密碼修改成功");
return map;
} catch (CustomException e) {
map.put("type","error");
map.put("msg","原密碼錯誤");
return map;
}
}else{
map.put("type","error");
map.put("msg","兩次密碼不一致");
return map;
}
}
/**
* 查看個人信息
* @param httpSession
* @return
*/
@RequestMapping("inforEmployee.do")
public @ResponseBody EmployeeCustomVo getInforEmployee(HttpSession httpSession){
Integer id = (Integer) httpSession.getAttribute("employeeId");
EmployeeCustomVo employeeCustomVo = userInforService.getInforEmployee(id);
return employeeCustomVo;
}
/**
* 修改個人信息
* @param httpSession
* @param employee
* @return
*/
@ResponseBody
@RequestMapping("updateInforEmployee.do")
public Message updateInforEmployee(HttpSession httpSession, Employee employee){
Integer id = (Integer) httpSession.getAttribute("employeeId");
employee.seteId(id);
if(userInforService.updateEmploueeById(id,employee)<=0) {
return Message.error("修改信息失敗");
}
return Message.success();
}
/**
* 個人工資信息
* @param pageNum
* @param limit
* @param year
* @param httpSession
* @return
* @throws Exception
*/
@RequestMapping("employeeSalaryList.do")
@ResponseBody
public EmployeeSalaryVO findSelective(
@RequestParam(value="page", defaultValue="1")int pageNum,
@RequestParam(value="limit", defaultValue="10") int limit,
@RequestParam(value="year", defaultValue="1") String year,
HttpSession httpSession) throws Exception {
Integer eId = (Integer) httpSession.getAttribute("employeeId");
//pageNum:起始頁面 pageSize:每頁的大小
PageHelper.startPage(pageNum,limit);
//查找條件,一定要緊跟在startPage后
List<Salary> salaryList = userInforService.getEmployeeSalaryList(eId, year);
PageInfo pageResult = new PageInfo(salaryList);
//設(shè)置前臺需要的數(shù)據(jù)
EmployeeSalaryVO employeeSalaryVO = new EmployeeSalaryVO();
employeeSalaryVO.setCode(0);
employeeSalaryVO.setMsg("");
employeeSalaryVO.setCount((int) pageResult.getTotal());
employeeSalaryVO.setData(pageResult.getList());
return employeeSalaryVO;
}
}管理員和員工登陸控制
/**
* @Author: admin
* @Description: 管理員和員工登陸控制
**/
@Controller
public class LoginController {
@Autowired
private LoginServiceImpl loginService = null;
/**
* @Author: admin
* @Description: 驗證碼變更
* @Date: 14:33 2021/10/5
* @Param: [request, response]
* @Return: void
**/
@RequestMapping(value = "/changeCode.do")
@ResponseBody
public void getIdentifyingCode(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
// 驗證碼存儲在session的identifyingCode,屬性中
CaptchaUtil.outputCaptcha(request, response);
}
// 獲取員工登陸界面
@RequestMapping("/")
public String getLoginPage(){
return "employee/login.html";
}
// 獲取管理員登陸界面
@RequestMapping("/admin.do")
public String getAdminLoginPage(HttpServletRequest request){
String realPath = request.getServletContext().getRealPath("/");
request.getSession().setAttribute("realPath", realPath);
return "admin/adminLogin.html";
}
/**
* 員工登錄操作
* @param model
* @param httpSession
* @param username
* @param password
* @param identifyingcode
* @return
*/
@RequestMapping(value = "/employeeLogin.do")
@ResponseBody
public Message employeeLogin(HttpSession httpSession, String username,
String password, String identifyingcode)
{
if(StringUtils.isEmpty(username)) {
return Message.error("請?zhí)顚懝ぬ?);
}
if(StringUtils.isEmpty(password)) {
return Message.error("請?zhí)顚懨艽a");
}
if(StringUtils.isEmpty(identifyingcode)) {
return Message.error("請?zhí)顚戲炞C碼");
}
String code = (String) httpSession.getAttribute("identifyingCode");
if(!identifyingcode.equalsIgnoreCase(code)){
return Message.error("驗證碼錯誤");
}
Employee employee = loginService.findEmployeeByIdAndPassword(username, password);
if(employee==null) {
return Message.error("工號或密碼錯誤");
}
httpSession.setAttribute("employeeId",employee.geteId());
return Message.success("員工登錄成功");
}
@RequestMapping(value = "/loginSuccess.do")
public String loginSucceses(Model model) throws Exception
{
return "employee/index.html";
}
/**
* 管理員登錄操作
* @param model
* @param httpSession
* @param username
* @param password
* @param identifyingcode
* @return
*/
@RequestMapping(value = "/adminLogin.do")
@ResponseBody
public Message adminLogin(HttpSession httpSession, String username,
String password, String identifyingcode)
{
if(StringUtils.isEmpty(username)) {
return Message.error("請?zhí)顚戀~號");
}
if(StringUtils.isEmpty(password)) {
return Message.error("請?zhí)顚懨艽a");
}
if(StringUtils.isEmpty(identifyingcode)) {
return Message.error("請?zhí)顚戲炞C碼");
}
String code = (String) httpSession.getAttribute("identifyingCode");
if(identifyingcode.equalsIgnoreCase(code)){
SystemManager manager = loginService.findSystemManagerByIdAndPassword(username, password);
if(manager==null) {
return Message.error("賬號或密碼錯誤");
}
// 保存到session
httpSession.setAttribute("admin",manager);
return Message.success("登錄成功");
}else {
return Message.error("驗證碼錯誤");
}
}
@RequestMapping(value = "/getAdminAccount.do")
@ResponseBody
public String getAdminAccount(HttpSession httpSession){
SystemManager systemManager = (SystemManager) httpSession.getAttribute("admin");
// SystemManager manager = loginService.findSystemManagerById(id);
return systemManager.getSmAccount();
}
@RequestMapping(value = "/getEmployeeAccount.do")
@ResponseBody
public Map<String,String> getEmployeeAccount(HttpSession httpSession){
Integer id = (Integer) httpSession.getAttribute("employeeId");
Employee employee = loginService.findEmployeeById(id);
HashMap<String, String> map = new HashMap<String, String>();
map.put("account",employee.geteAccount());
map.put("name",employee.geteName());
return map;
}
@RequestMapping(value = "/logout.do")
public String logout(HttpSession httpSession){
httpSession.removeAttribute("employeeId");
return "redirect:/";
}
@RequestMapping(value = "/logoutAdmin.do")
public String logoutAdmin(HttpSession httpSession){
httpSession.removeAttribute("admin");
return "redirect:/admin.do";
}
}用戶管理操作
/**
* 用戶管理操作
*/
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
/**
* 用戶添加頁面
* @return
*/
@GetMapping("/add")
public String create() {
return "user/add";
}
/**
* 用戶添加操作
* @param user
* @return
*/
@PostMapping("/add")
@ResponseBody
public Map<String, Object> add(@RequestBody User user) {
if(StringUtils.isEmpty(user.getUserName())){
return MapControl.getInstance().error("請?zhí)顚懹脩裘?).getMap();
}
if(StringUtils.isEmpty(user.getName())){
return MapControl.getInstance().error("請?zhí)顚懨Q").getMap();
}
if(StringUtils.isEmpty(user.getUserPwd())){
return MapControl.getInstance().error("請?zhí)顚懨艽a").getMap();
}
int result = userService.create(user);
if (result <= 0) {
return MapControl.getInstance().error().getMap();
}
return MapControl.getInstance().success().getMap();
}
/**
* 根據(jù)id刪除
* @param id
* @return
*/
@PostMapping("/delete/{id}")
@ResponseBody
public Map<String, Object> delete(@PathVariable("id") Integer id) {
int result = userService.delete(id);
if (result <= 0) {
return MapControl.getInstance().error().getMap();
}
return MapControl.getInstance().success().getMap();
}
//批量刪除
@PostMapping("/delete")
@ResponseBody
public Map<String, Object> delete(String ids) {
int result = userService.delete(ids);
if (result <= 0) {
return MapControl.getInstance().error().getMap();
}
return MapControl.getInstance().success().getMap();
}
/**
* 編輯用戶信息操作
* @param user
* @return
*/
@PostMapping("/edit")
@ResponseBody
public Map<String, Object> edit(@RequestBody User user) {
if(StringUtils.isEmpty(user.getUserName())){
return MapControl.getInstance().error("請?zhí)顚懹脩裘?).getMap();
}
if(StringUtils.isEmpty(user.getName())){
return MapControl.getInstance().error("請?zhí)顚懨Q").getMap();
}
if(StringUtils.isEmpty(user.getUserPwd())){
return MapControl.getInstance().error("請?zhí)顚懨艽a").getMap();
}
int result = userService.update(user);
if (result <= 0) {
return MapControl.getInstance().error().getMap();
}
return MapControl.getInstance().success().getMap();
}
/**
* 根據(jù)id查詢,跳轉(zhuǎn)修改頁面
* @param id
* @param modelMap
* @return
*/
@GetMapping("/edit/{id}")
public String edit(@PathVariable("id") Integer id, ModelMap modelMap) {
User user = userService.detail(id);
modelMap.addAttribute("user", user);
return "user/edit";
}
//查詢所有
@PostMapping("/query")
@ResponseBody
public Map<String, Object> query(@RequestBody User user) {
List<User> list = userService.query(user);
Integer count = userService.count(user);
return MapControl.getInstance().success().page(list, count).getMap();
}
//跳轉(zhuǎn)列表頁面
@GetMapping("/list")
public String list() {
return "user/list";
}
}以上就是Java實戰(zhàn)之火車票預(yù)訂系統(tǒng)的實現(xiàn)的詳細內(nèi)容,更多關(guān)于Java火車票預(yù)訂系統(tǒng)的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Spring?Security實現(xiàn)統(tǒng)一登錄與權(quán)限控制的示例代碼
這篇文章主要介紹了Spring?Security實現(xiàn)統(tǒng)一登錄與權(quán)限控制,本文通過示例代碼重點看一下統(tǒng)一認證中心和業(yè)務(wù)網(wǎng)關(guān)的建設(shè),需要的朋友可以參考下2022-03-03
Java Mybatis框架增刪查改與核心配置詳解流程與用法
MyBatis 是一款優(yōu)秀的持久層框架,它支持自定義 SQL、存儲過程以及高級映射。MyBatis 免除了幾乎所有的 JDBC 代碼以及設(shè)置參數(shù)和獲取結(jié)果集的工作。MyBatis 可以通過簡單的 XML 或注解來配置和映射原始類型、接口和 Java POJO為數(shù)據(jù)庫中的記錄2021-10-10
Spring?Boot使用Hibernate-Validator校驗參數(shù)時的長度校驗方法詳解
這篇文章主要給大家介紹了關(guān)于Spring?Boot使用Hibernate-Validator校驗參數(shù)時的長度校驗方法的相關(guān)資料,在Spring Boot中可以使用Spring Boot Validation來對參數(shù)名稱進行校驗,需要的朋友可以參考下2023-08-08
SpringBoot 二維碼生成base64并上傳OSS的實現(xiàn)示例
本文主要介紹了SpringBoot 二維碼生成base64并上傳OSS的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-05-05
SpringCloud Gateway鑒權(quán)和跨域解決方案
網(wǎng)關(guān)是介于客戶端和服務(wù)器端之間的中間層,所有的外部請求都會先經(jīng)過 網(wǎng)關(guān)這一層,也就是說,API 的實現(xiàn)方面更多的考慮業(yè)務(wù)邏輯,而安全、性能、監(jiān)控可以交由 網(wǎng)關(guān)來做,這樣既提高業(yè)務(wù)靈活性又不缺安全性,本文給大家介紹SpringCloud Gateway鑒權(quán)和跨域解決方案,一起看看吧2023-11-11

