1.接收
1.1以javabean的名字接受,需要和javabean保持一致
//1.放到()里面的任何对象会自动实例化(NEW)
//2.自动DI/IOC(相当于以前SERVLET中写的request.getParameter("bookid"))
//3.会自动共享数据(request.setAttribute("bookinfo",bookinfo);)
// 但是,这个共享只会共享页面中输入进来的数据,比如你只输入了两个字段,其它字段是不共享的
1.2 接受页面数据,以变量的方式接收页面的参数的名字与变量一致
1.3 接受页面数据,以原生的request,response接收
1.4 接收页面的数据,更改名字(RequestParam(""))
public String page(@RequestParam("abc") int pageIndex) 不改的话,即为pageIndex
2.发送数据
2.1 给对象重命名(ModelAttribute(""))
public String add4(@ModelAttribute("abc") Bookinfo bookinfo) 不改的话,即传过去的对象名为bookinfo
2.2 session共享
可以将数据上升为session全局,需要在该类的最上方加 @SessionAttributes("bookinfo")
public String add5(Bookinfo bookinfo)
package com.lanou.test.controller;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import com.lanou.test.domain.Bookinfo;
import com.lanou.test.service.BookService;
@Controller
@RequestMapping("/bookinfo")
@SessionAttributes("bookinfo")
public class BookinfoController {
//1.接收页面数据
//1.1以对象方式接 页面中参数的名字与JAVABEAN中保持一致
@RequestMapping("/add")
public String add(Bookinfo bookinfo) {
//1.放到()里面的任何对象会自动实例化(NEW)
//2.自动DI/IOC(相当于以前SERVLET中写的request.getParameter("bookid"))
//3.会自动共享数据(request.setAttribute("bookinfo",bookinfo);)
// 但是,这个共享只会共享页面中输入进来的数据,比如你只输入了两个字段,其它字段是不共享的
//调用业务层添加bookSerice.save(bookinfo);
System.out.println(bookinfo.getBookid());
System.out.println(bookinfo.getBookname());
System.out.println(bookinfo.getBookprice());
System.out.println(bookinfo.getBookimg());
return "index";
}
//1.2.接收页面数据,以变量的方式接 页面中参数的名字与变量中保持一致
@RequestMapping("/add2")
public String add2(String bookid,String bookname,double bookprice,String bookimg) {
//调用业务层添加bookSerice.save(bookid,bookname,bookprice,bookimg);
System.out.println(bookid);
System.out.println(bookname);
System.out.println(bookprice);
System.out.println(bookimg);
return "index";
}
//1.3.接收页面数据,原生request,response
@RequestMapping("/add3")
public String add3(HttpServletRequest request,HttpServletResponse response) {
String bookid = request.getParameter("bookid");
String bookname = request.getParameter("bookname");
double bookprice = Double.parseDouble(request.getParameter("bookprice"));
String bookimg = request.getParameter("bookimg");
System.out.println(bookid);
System.out.println(bookname);
System.out.println(bookprice);
System.out.println(bookimg);
return "index";
}
//1.4.接收页面数据,但要改参数名字
//@RequestParam("abc"),指定页面参数的名字为abc
@RequestMapping("/page")
public String page(@RequestParam("abc") int pageIndex) {
System.out.println("页数:"+pageIndex);
return "index";
}
//2.对象注解
//@ModelAttribute("abc"),这个注解的意思是:request.setAttribute("abc",bookinfo);
//@ModelAttribute,不写其它也写了这个注解,request.setAttribute("bookinfo",bookinfo);
//所以,如果是下面这个方法的写法,到index.jsp页面得到数据的方式应该是:#{abc.bookid}
@RequestMapping(value="/add4",method=RequestMethod.GET)//设定请求方式为Get,post不能访问
public String add4(@ModelAttribute("abc") Bookinfo bookinfo) {
System.out.println(bookinfo.getBookid());
System.out.println(bookinfo.getBookname());
System.out.println(bookinfo.getBookprice());
System.out.println(bookinfo.getBookimg());
return "index";
// return "redirect:../index.jsp";
}
//3.转发
//request.getRequestDispatcher("/index.jsp").forward(request,response);
//response.sendRedirect("/index.jsp");
//4.sesion共享
//默认共享为request,如果想让这个bookinfo共享范围提升为session的,在类的最后面加上
//@SessionAttributes
@RequestMapping("/add5")
public String add5(Bookinfo bookinfo) {
return "index";
}
}