Springmvc receives data and sends it to the interface in several ways

Keywords: Programming Session JSP Java

1. reception

1.1 is accepted in the name of javabean and needs to be consistent with javabean

// 1. Any object placed in () is automatically instantiated (NEW)
// 2. Automatic DI/IOC (equivalent to request.getParameter("bookid") written in SERVLET before)
// 3. Automatically sharing data (request.setAttribute("bookinfo",bookinfo);)
// However, this sharing will only share the data entered in the page, for example, you only enter two fields, other fields are not shared.

1.2 Accept page data, and receive the name of page parameters as variables.

1.3 Accept page data, receive with native request, response

1.4 Receive page data and change name (RequestParam("))

If the public String page (@RequestParam ("abc") int pageIndex is unchanged, it is pageIndex.

2. Sending data

2.1 Rename an object (ModelAttribute("))

If public String add4(@ModelAttribute("abc") Bookinfo bookinfo) remains unchanged, the object passed on is called bookinfo.

2.2 session Sharing

You can elevate data to session global, requiring @SessionAttributes("bookinfo") at the top of the class.

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. Receiving Page Data
	//1.1 Object-based access to the name of the parameter in the page is consistent with that in JAVABEAN
	@RequestMapping("/add")
	public String add(Bookinfo bookinfo) {
		//1. Any object placed in () is automatically instantiated (NEW)
		//2. Automatic DI/IOC (equivalent to request.getParameter("bookid") written in SERVLET before)
		//3. Automatically sharing data (request.setAttribute("bookinfo",bookinfo);)
		//  However, this sharing will only share the data entered in the page, for example, you only enter two fields, other fields are not shared.
		//Call the business layer to add 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. Receive page data and take the name of the parameter in the page as a variable to keep the same as that in the variable.
	@RequestMapping("/add2")
	public String add2(String bookid,String bookname,double bookprice,String bookimg) {
		//Call the business layer to add 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. Receive page data, native 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. Receive page data, but change parameter name
	//@ RequestParam("abc"), specifying the name of the page parameter as ABC
	@RequestMapping("/page")
	public String page(@RequestParam("abc") int pageIndex) {
		System.out.println("The number of pages:"+pageIndex);
		return "index";
	}
	
	//2. Object annotations
	//@ ModelAttribute("abc"), which means: request.setAttribute("abc",bookinfo);
	//@ ModelAttribute, without writing anything else, also wrote this annotation, request.setAttribute("bookinfo",bookinfo);
	//So, if you write the following method, the way to get the data on the index.jsp page should be: {abc.bookid}
	@RequestMapping(value="/add4",method=RequestMethod.GET)//Set the request mode to Get,post can not be accessed
	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. forwarding
	//request.getRequestDispatcher("/index.jsp").forward(request,response);
	//response.sendRedirect("/index.jsp");
	
	
	//4. Session Sharing
	//The default share is request. If you want to upgrade the bookinfo share scope to session, add it at the end of the class
	//@SessionAttributes
	@RequestMapping("/add5")
	public String add5(Bookinfo bookinfo) {
		return "index";
	}
	
	
}


   

Posted by poizn on Sun, 03 Feb 2019 01:00:16 -0800