效果图:
部分代码展示
1、index.html
//<!--需要源码+wx:yi3219727434 -->
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首页</title>
<link rel="stylesheet" th:href="@{/CSS/index_css.css}">
<style>
/*body {*/
/* background-image: url("/img/R-2.jpg");*/
/* background-size: cover;*/
/* background-position: center center;*/
/* background-repeat: no-repeat;*/
/*}*/
input::placeholder {
color: #2b2b2b;
}
</style>
</head>
<body>
<!--index -->
<div class="vid-container" style="background-color: #2bc2d5">
<!-- <img th:href="@{/img/R-2.jpg}" style="height: 300px;width: 300px"/>-->
<video class="bgvid" autoplay="autoplay" muted="muted" preload="auto" loop>
<source src="/uploads/141102/mountain.webm?1410742112" type="video/webm">
</video>
<div class="inner-container">
<video class="bgvid inner" autoplay="autoplay" muted="muted" preload="auto" loop>
<source src="/uploads/141102/mountain.webm?random=1" type="video/webm">
</video>
<div class="box">
<h1>Login</h1>
<form th:action="@{/UserLogon}" method="post">
<input type="text" placeholder="UserId" name="userid"/>
<input type="password" placeholder="Password" name="password"/>
<input type="submit" value="Login" style="background-color: #1ebbee">
</form>
<p>Not a member? <span><a th:href="@{UserRegister}">Sign Up</a></span>
</p>
</div>
</div>
</div>
</body>
</html>
2、UserController
//<!--需要源码+wx:yi3219727434 -->
package zjy.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import zjy.pojo.Commodity;
import zjy.pojo.User;
import zjy.service.CommodityService;
import zjy.service.UserService;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;
@Controller
public class UserController {
@Autowired
private UserService userService;
@Autowired
private User user;
@Autowired
private CommodityService commodityService;
@Autowired
private Commodity commodity;
@RequestMapping(value = "/index")
public String index() {
return "index";
}
@RequestMapping(value = "/fail1")
public String fail() {
return "index";
}
/**
* 登录判断
* @param userid
* @param password
* @param session
* @param response
* @return
* @throws IOException
*/
@RequestMapping(value = "/UserLogon")
public String UserLogon(@RequestParam("userid") String userid,
@RequestParam("password") String password, HttpSession session,HttpServletResponse response) throws IOException {
//登录判断
boolean flag = userService.UserLoginDetermine(userid, password);
if (flag == true) {
//获取用户全部信息
user = null;
user = userService.selectUserByUserid(userid);
//保存在session域中,键为userid 键值为user
session.setAttribute("user", user);
//获取全部商品列表
List<Commodity> slist = commodityService.getAllCommodity();
session.setAttribute("slist", slist);
//登陆成功,跳转到首页
response.setContentType("text/html;charset=utf-8");
response.getWriter().write("<script>alert('登录成功!');</script>");
return "redirect:/commodities/page/1";
} else {
//登陆失败
response.setContentType("text/html;charset=utf-8");
response.getWriter().write("<script>alert('登陆失败!');</script>");
return "fail";
}
}
/**
* 跳转到个人中心
*
* @return
*/
@RequestMapping("/usercenters")
public String UserCenter() {
return "usercenter";
}
/**
* 从登陆页面跳转到注册页面
*
* @return
*/
@RequestMapping(value = "/UserRegister")
public String UserRegister() {
return "register";
}
/**
* 开始注册
*
* @param userid
* @param username
* @param password
* @param model
* @return
*/
@RequestMapping(value = "/UserRegister1")
public String UserRegister1(@RequestParam("userid") String userid,
@RequestParam("username") String username, @RequestParam("password") String password,
@RequestParam("file") MultipartFile file, Model model,HttpServletResponse response) throws IOException {
response.setContentType("text/html;charset=utf-8");
user.setUserid(userid);
user.setUsername(username);
user.setPassword(password);
user.setCcount(0);
// 上传头像
// 判断文件是否为空
if (!file.isEmpty()) {
try {
// 获取文件名
String fileName = file.getOriginalFilename();
user.setUfile(fileName);
// 指定文件保存路径
String path = "D:\\ClassHelpFile\\SSM1\\";
String filePath = path + fileName;
// 创建文件对象
File dest = new File(filePath);
// 将上传的文件保存到指定的路径中
file.transferTo(dest);
// 文件上传成功
boolean flag = userService.AddUser(user);
if (flag == true) {
response.getWriter().write("<script>alert('注册成功!');</script>");
return "index";
} else {
response.getWriter().write("<script>alert('注册失败!');</script>");
return "register_fail";
}
} catch (IOException e) {
response.getWriter().write("<script>alert('注册失败!');</script>");
return "register_fail";
// throw new RuntimeException(e);
}
} else {
// 文件上传失败
return "register_fail";
}
}
/**
* 跳转到修改用户密码界面
*
* @return
*/
@RequestMapping("/updateuserpassword")
public String Updateuserpassword() {
return "updatepassword";
}
/**
* 进行更新密码
*
* @return
*/
@RequestMapping("/Updateuserpassword1")
public String Updateuserpassword1(HttpSession session, @RequestParam("password1") String password1,
@RequestParam("password2") String password2,HttpServletResponse response) throws IOException {
// 获取用户id
User user = (User) session.getAttribute("user");
// 更新密码
int res = userService.UpdateUserPassword(user.getUserid(), password1, password2);
if (res == 2) {
response.setContentType("text/html;charset=utf-8");
response.getWriter().write("<script>alert('修改成功!');</script>");
return "index";
} else {
response.setContentType("text/html;charset=utf-8");
response.getWriter().write("<script>alert('修改失败!');</script>");
return "fail";
}
}
/**
* 跳转到修改用头像页面
*
* @return
*/
@RequestMapping("/updateuserufile")
public String UpdateUserUfile() {
return "updateufile";
}
//进行更新头像操作
@RequestMapping("updateuserufile1")
public String updateuserufile1(@RequestParam("file") MultipartFile file, HttpSession session, HttpServletResponse response)throws Exception {
// 上传头像
// 判断文件是否为空
response.setContentType("text/html;charset=utf-8");
if (!file.isEmpty()) {
try {
// // 设置响应头,禁止浏览器缓存
// response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
// response.setHeader("Pragma", "no-cache");
// response.setHeader("Expires", "0");
// 获取用户id
User user1 = (User) session.getAttribute("user");
// 获取文件名
String fileName = file.getOriginalFilename();
//修改用户头像
boolean flag1 = userService.UpdateUfile(user1.getUserid(), fileName);
if (flag1 == true) {
// 指定文件保存路径
String path = "D:\\ClassHelpFile\\SSM1\\";
String filePath = path + fileName;
// 创建文件对象
File dest = new File(filePath);
// 将上传的文件保存到指定的路径中
file.transferTo(dest);
// 更新session域中的user
user1.setUfile(fileName);
session.setAttribute("user",user1);
response.getWriter().write("<script>alert('修改成功!');</script>");
return "homepage";
}else {
response.getWriter().write("<script>alert('修改失败!');</script>");
return "fail";
}
} catch (IOException e) {
response.getWriter().write("<script>alert('修改失败!');</script>");
return "register_fail";
// throw new RuntimeException(e);
}
} else {
// 文件上传失败
return "register_fail";
}
}
}
3、CutController
//<!--需要源码+wx:yi3219727434 -->
package zjy.controller;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import zjy.pojo.Commodity;
import zjy.pojo.Cut;
import zjy.pojo.Randoms;
import zjy.pojo.User;
import zjy.service.CommodityService;
import zjy.service.CutService;
import zjy.service.UserService;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
@Controller
public class CutController{
@Autowired
private CutService cutService;
@Autowired
private Cut cut;
@Autowired
private UserService userService;
@Autowired
private Randoms randoms;
@Autowired
private CommodityService commodityService;
@Autowired
private Commodity commodity;
/**
* 跳转到购物车页面,并分页展示
* @return
*/
@RequestMapping("/showmycut/page/{pageNum}")
public String ShowMyCut(@PathVariable("pageNum") Integer pageNum, Model model, HttpSession session){
try {
//获得该用户的购物车中的所有商品,并分页显示
User user1 = (User) session.getAttribute("user");
User user = userService.selectUserByUserid(user1.getUserid());
session.setAttribute("user",user);
PageInfo<Cut> clist = cutService.getCutPage(user.getUserid(), pageNum);
model.addAttribute("clist",clist);
//获取该用户购物车总数
Integer ccount = user.getCcount();
model.addAttribute("ccount",ccount);
return "mycut";
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 删除购物车中的一个商品
* @param cid
* @return
*/
@RequestMapping("/deletemycut/{cid}")
public String DeleteAddress(@PathVariable("cid") String cid, HttpSession session, HttpServletResponse response){
response.setContentType("text/html;charset=utf-8");
try {
//删除购物车中的一个商品
User user = (User) session.getAttribute("user");
boolean flag = cutService.DeleteCut( cid,user.getUserid());
System.out.println("------cid-----"+cid);
System.out.println("------user-----"+user);
System.out.println("------flag-----"+flag);
if(flag == true){
//更新域中的user
User user1 = userService.selectUserByUserid(user.getUserid());
session.setAttribute("user",user1);
response.getWriter().write("<script>alert('删除成功!');</script>");
return "redirect:/showmycut/page/1";
}else{
response.getWriter().write("<script>alert('删除失败!');</script>");
return "fail";
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 添加购物车
* @param sid
* @param session
* @return
*/
@RequestMapping("/addcut/{sid}")
public String AddCut(@PathVariable("sid") String sid,HttpSession session,HttpServletResponse response){
User user = null;
response.setContentType("text/html;charset=utf-8");
try {
//添加购物车
user = (User) session.getAttribute("user");
cut.setCid(randoms.getRandomCid());
commodity = commodityService.getCommodityBySid(sid);
cut.setSname(commodity.getSname());
cut.setSprice(commodity.getSprice());
cut.setUserid(user.getUserid());
cut.setCtime(randoms.getData());
boolean flag = cutService.AddCut(user.getUserid(),cut);
System.out.println("addcut中的cut:"+cut);
if(flag == true){
//更新域中的user
User user1 = userService.selectUserByUserid(user.getUserid());
session.setAttribute("user",user1);
response.getWriter().write("<script>alert('添加成功!');</script>");
return "redirect:/showmycut/page/1";
}
else {
response.getWriter().write("<script>alert('添加失败!');</script>");
return "fail";
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}