ssm框架下的网上书城项目的开发-controller层

现在我们开发controller层

RegisterController.java

package com.bookstore.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.bookstore.dao.User;
import com.bookstore.service.RegisterService;

@Controller
public class RegisterController {
	//授权服务层
	@Autowired
	private RegisterService registerService;
	
	//跳转到注册页面
	@RequestMapping("toregister")
	public ModelAndView toregister() {
		ModelAndView mv = new ModelAndView("register");//跳转到注册页面,没有数据读取,就单纯的跳转
		return mv;
	}
	
	//进行注册操作
	@RequestMapping(value="register",method=RequestMethod.POST)
	public ModelAndView register(User user) {//获取页面post提交的参数,如果页面提交的参数和实体类User定义的参数一样,可以直接写User对象接受
		ModelAndView mv;
		int m = registerService.getUserByName(user);//通过user对象的用户名,调用服务层方法去查询这个用户是否已经存在
		if(m == 0) {//返回的m等于0,表示用户不存在
			int n = registerService.register(user);//调用服务层方法,进行添加用户到数据库
			if(n != 0) {//n不等0,表示添加成功,成功了就行跳转到登录页面
				mv = new ModelAndView("redirect:tologin");
			}else {//n等于0,表示添加失败,重新跳转到注册页面,进行重新注册
				mv = new ModelAndView("toregister");
			}
			
		}else {//m不等于0,表示用户存在,直接重定向到登录页面,进行登录操作
			mv = new ModelAndView("redirect:tologin");
		}
		return mv;
	}
}

LoginController.java

package com.bookstore.controller;

import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.bookstore.dao.User;
import com.bookstore.service.LoginService;

@Controller
public class LoginController {
	//授权服务层
	@Autowired
	private LoginService loginService;
	
	//跳转到登录页面
	@RequestMapping("tologin")
	public ModelAndView tologin(){
		ModelAndView mv = new ModelAndView("login");
		return mv;
	}
	//登录操作
	@RequestMapping(value="login",method = RequestMethod.POST)
	public ModelAndView login(String username,String password,HttpSession session) {
		ModelAndView mv;
		User user = loginService.login(username,password);//执行登录操作,并将登录对象返回
		if(user != null) {//判断对象是否存在
			//不存在就跳往主页面,并设置对象的会话
			mv = new ModelAndView("redirect:main");
			
			session.setAttribute("user", user);
		}else {
			//存在:就跳到登录页面重新登陆
			mv = new ModelAndView("redirect:tologin");
		}
		return mv;
	}
}

MainController.java

package com.bookstore.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.bookstore.dao.Book;
import com.bookstore.dao.Cart;
import com.bookstore.service.BookService;
import com.bookstore.service.CartService;

@Controller
public class MainController {
	//授权服务层
	@Autowired
	private BookService bookService;
	@Autowired
	private CartService cartService;
	
	//跳往主页面
	@RequestMapping("main")
	public ModelAndView main(){
		List bookList = bookService.getAllNewBooks();//查询最新的书籍(取前12本)
		ModelAndView mv = new ModelAndView("main");
		mv.addObject("bookList", bookList);//将数据传递到前台页面
		return mv;
	}
	
	//查询得到所有的书籍
	@RequestMapping("allbook")
	public ModelAndView allbook() {
		List bookList = bookService.getAllBooks();//调用服务层方法得到所有书籍用List结果集存起来
		ModelAndView mv = new ModelAndView("main");
		mv.addObject("bookList", bookList);//将List集合返回给前端页面,进行呈现
		return mv;
	}
	
	//退出登录
	@RequestMapping("cancel")
	public ModelAndView cancel(HttpSession session){
		session.removeAttribute("user");//销毁用户登录的会话
		ModelAndView mv = new ModelAndView("redirect:main");//重定向到主页面
		return mv;
	}
	
	//按类型查询书籍(type传递过来的分类参数)
	@RequestMapping("typebook")
	public ModelAndView typebook(int type) {
		List bookList = bookService.getBooksByBooktype(type);//调用服务层方法按分类参数查询不同种类的书籍(例如Java,type=1)
		ModelAndView mv = new ModelAndView("main");
		mv.addObject("bookList",bookList);//将数据传递到前台页面
		return mv;
	}
	
	//跳转到书籍页面,根据书籍的id来判断是选中的那本书进行跳转,并查询对应的书籍数据进行显示数据
	@RequestMapping("tobook")
	public ModelAndView tobook(int id) {
		Book book = bookService.getBookById(id);//调用服务层方法,通过书籍id查询出选中书的属性
		ModelAndView mv = new ModelAndView("book");
		mv.addObject("book",book);//将数据传递到前台页面
		return mv;
	}
	
	
	//选好书籍数量,添加到购物车
	@RequestMapping(value="incart",method=RequestMethod.POST)
	public ModelAndView incart(Cart cart,HttpSession session) {
		ModelAndView mv = null;
		if(session.getAttribute("user") == null) {//通过判断会话中是否有用户来进行不同的操作
			//用户不是登录状态跳转到登录界面,进行登录操作
			mv = new ModelAndView("redirect:tologin");
		}else {
			//用户是登录状态,直接调用服务层方法把购物记录添加到数据库
			int n = cartService.incart(cart,session);
			//并通过返回值n来判断添加时候成功,成功就跳转到购物车页面
			if(n != 0) {
				mv = new ModelAndView("redirect:tocart");//从定向跳转到购物车
			}                                                                                    
		}
		return mv;
	}
	
	//跳转到购物车页面
	@RequestMapping("tocart")
	public ModelAndView tocart(HttpSession session) {
		ModelAndView mv;
		if(session.getAttribute("user") != null) {//判断用户是否登录
			mv = new ModelAndView("cart");//登录状态下,跳转到购物车页面
			List cartList = cartService.getAllOrders(session);//查询出来的购物车记录保存到List集合里面
			mv.addObject("cartList",cartList);//将数据传递到前台页面
		}else {
			mv = new ModelAndView("redirect:tologin");//不是登录状态下,跳转到登录页面
		}
		return mv;
	}
	
	
	//通过购物记录id删除这条记录
	@RequestMapping(value="cancelcart",method=RequestMethod.GET)
	public ModelAndView cancelcart(int id) {//得到选中记录的id
		ModelAndView mv = null;
		int n = cartService.deleteCartById(id);//调用服务层进行删除操作,并返回n
		if(n != 0) {//n不为0,说明删除成功
			mv = new ModelAndView("redirect:tocart");//重定向到购物车页面,进行刷新显示
		}
		return mv;
	}
}

 

你可能感兴趣的:(ssm网上书城项目)