SpringMVC和Struts2差别感觉还是很大的,Struts存到request中个的属性,都是field,给出对应的set和get就可以了,而在SpringMVC中,需要存到ModelAndView中,或者调用Request。
给action指定页面,SpringMVC也是存在ModelAndView中,或者用String(需要Request时候,参数指定),而在Struts中,直接返回字符串,就可以了。
都说SpringMVC写ajax请求的方法比较容易。。但是我没想到怎么弄。。。所以还是用了以前的方法,用response取得Write对象,然后写入。
在这里用到的最多的注解就是@Controller和@RequestMapping,写链接的时候要注意路径经常需要用../回到上一级目录(尤其是菜单这块)
碰到的一个问题就是配置了前缀和后缀,如果我这次去的地址的网页不在前缀目录了,就不知道怎么弄了,现在用的重定向。。。。。(没法传值了。。哎。。)
附下代码:
package cn.edu.bjut.zj.tims.book.web;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import cn.edu.bjut.zj.base.page.page.Page;
import cn.edu.bjut.zj.tims.book.biz.BookBiz;
import cn.edu.bjut.zj.tims.entity.BookExtend;
import cn.edu.bjut.zj.tims.entity.BookVo;
@Controller
@RequestMapping("/book")
public class BookController {
@Autowired
private BookBiz biz;
@RequestMapping("toAddBook")
public ModelAndView toAdd() {
ModelAndView mav = new ModelAndView("book/bookAdd");
mav.addObject("alist", biz.queryAllAuthor(null));
mav.addObject("tlist", biz.queryAllType());
mav.addObject("plist", biz.queryAllPublisher());
return mav;
}
@RequestMapping("doAddBook")
public ModelAndView doAdd(BookExtend book) {
ModelAndView mav = new ModelAndView("redirect:listBook.action");
biz.addBook(book);
return mav;
}
@RequestMapping("toUpdateBook")
public ModelAndView toUpdate(int id) {
ModelAndView mav = new ModelAndView("book/bookUpdate");
mav.addObject("alist", biz.queryAllAuthor(null));
mav.addObject("tlist", biz.queryAllType());
mav.addObject("plist", biz.queryAllPublisher());
mav.addObject("book", biz.queryBookById(id));
return mav;
}
@RequestMapping("doUpdateBook")
public ModelAndView doUpdate(BookExtend book) {
ModelAndView mav = new ModelAndView("redirect:listBook.action");
biz.updateBook(book);
return mav;
}
@RequestMapping("deleteBook")
public ModelAndView delete(int id) {
ModelAndView mav = new ModelAndView("redirect:listBook.action");
biz.deleteBook(id);
return mav;
}
@RequestMapping("listBook")
public ModelAndView list(BookVo vo) {
ModelAndView mav = new ModelAndView("book/bookList");
mav.addObject("alist", biz.queryAllAuthor(null));
mav.addObject("tlist", biz.queryAllType());
mav.addObject("blist", biz.queryAllBook(vo));
Page page = vo.getPage();
page.setTotalItems(biz.queryBookCount(vo));
mav.addObject("page", page);
return mav;
}
@RequestMapping("adminBook")
public ModelAndView admin(BookVo vo) {
ModelAndView mav = new ModelAndView("book/adminBook");
mav.addObject("alist", biz.queryAllAuthor(null));
mav.addObject("tlist", biz.queryAllType());
mav.addObject("blist", biz.queryAllBook(vo));
Page page = vo.getPage();
page.setTotalItems(biz.queryBookCount(vo));
mav.addObject("page", page);
return mav;
}
@RequestMapping("detailBook")
public ModelAndView detail(int id) {
ModelAndView mav = new ModelAndView("book/bookDetail");
mav.addObject("book", biz.queryBookById(id));
return mav;
}
}
另外我发现@Autowire真的是好用的注解啊,@Service都不需要指定名称了,就可以自动注入了~~以前都是用@Resource还要指定名称才能注入,好麻烦= =