Spring MVC notes method parameters and handling ajax

Keywords: JSP Spring xml JSON

Forwarding and redirection of spring MVC

Configuring the view parser in spring-mvc.xml

<!-- Try parser  springMVC Managerial jsp File location should be in /WEB-INF/meto/  -->
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/meto/"/>
    <property name="suffix" value=".jsp"/>
</bean>

When the return type is String, prefix and suffix String splicing is the path of jsp file
If the beginning of the string is redirect: /, it is a redirect. You can access the url of another method or the public. jsp file. Resources under WEB-INF cannot be accessed

	@RequestMapping("/aaa")
	public String f1(){
		System.out.println(this.getClass() + "Journal 1... f1()..Forward meto");
		
		return "test2"; 
	}
	
	@RequestMapping("/bbb")
	public String f2(){	
		System.out.println(this.getClass() + "Journal 2... f2()..redirect meto");
	
		//Return "redirect: test2"; / / resources under Web inf / are not allowed to be accessed and can only be forwarded
		//return "redirect:/fff/aaa.do";
		return "redirect:/test1.jsp";
	}

Return value of method in spring MVC

ModelAndView, redirection and forwarding form

The parameter of the method is ModelAndView mv
The return value is mv
mv.setViewName(); is the method of forwarding and redirection
Code demonstration:


	@RequestMapping("/ccc")
	public ModelAndView f3( ModelAndView mv ){
		System.out.println(this.getClass() + "Journal 3... f1()..Forward meto");
		
		mv.setViewName("test2");
		return mv; 
	}
	
	@RequestMapping("/ddd")
	public ModelAndView f4( ModelAndView mv){	
		System.out.println(this.getClass() + "Journal 4... f2()..redirect meto");
		
		//Mv.setviewname ("redirect: test2"); / / resources under Web inf / are not accessible and can only be forwarded
		mv.setViewName("redirect:/test1.jsp");
		return mv;
	}

Return value is JSON

@ResponseBody + manual splicing
Parameter HttpServletResponse
Code demonstration:

	@RequestMapping("/eee")
	@ResponseBody
	public void f5(HttpServletResponse resp ) throws IOException{
		System.out.println(this.getClass() + "Journal 3... f1()..Forward meto");
		String str="{'success':0,'data':['Zhang San','bbb','ccc']}";
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("text/html;charset=utf-8");
		resp.getWriter().print(str);	
	}

@ResponseBody+jar package automatic splicing
Import jar package

new JSONObject object, put("", "") method to store data
Code demo


	@RequestMapping("/fff")
	@ResponseBody
	public void f6(HttpServletResponse resp) throws IOException{
		System.out.println(this.getClass() + "Journal 3... f1()..Forward meto");
		
		List<Users> ulist = new ArrayList<Users>(); 
		ulist.add( new Users("A1","Battle three 1",21));
		ulist.add( new Users("A2","Battle three 2",22));
		ulist.add( new Users("A3","Battle three 3",23));
		ulist.add( new Users("A4","Battle three 4",20));
		ulist.add( new Users("A5","Battle three 5",25));
		ulist.add( new Users("A6","Battle three 6",26));
		
		JSONObject js = new JSONObject();
		
		js.put("success", 0);
		js.put("data", ulist);
		
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("text/html;charset=utf-8");
		resp.getWriter().print( js.toString() );
	}//f6

@ModelAttribute annotation: pre executed method in Controller

Code demonstration:
The pre1 method will be executed first


	@Controller
	@RequestMapping("/sss")
	public class Second {
		
		@ModelAttribute
		public void pre1(){
			System.out.println("...pre1()....");
		}
		
		
		@RequestMapping("/aaa")
		public String s1(){
			System.out.println(this.getClass()+"Journal 1...");
			return "test2";
		}
	}

Preprocessing - object processing

@ModelAttribute
In the preprocessing method, an object such as Users is returned. It can be found from the database or created by parameter receiving. It is placed in the Model/ModelMap
Stores the lowercase string of the return type of the method as a key
It can be obtained through model and modelmap in another method
Code demonstration:

	@ModelAttribute
	public Users pre1(){
		System.out.println("...pre1()....Preprocess and place objects in Model/ModelMap");
		//It may be created by querying data or receiving parameters
		Users u = new Users("A001","Porcelain washed Queen Mother",40);
		return u;
	}
	
	
	@RequestMapping("/aaa")
	public String s1( Model md, ModelMap mdMap , ModelAndView mv){
		System.out.println(this.getClass()+"Journal 1..." + md.asMap().get("users"));
		System.out.println(this.getClass()+"Journal 2..." + mdMap.get("users"));
		System.out.println(this.getClass()+"Journal 3..." + mv.getModelMap().get("users"));
		return "test2";
	}

Preprocessing - object processing + key:@ModelAttribute("uvv")

The string in this annotation is a key, not lowercase of the return value type
Data is still stored in Model/ModelMap
Code demonstration:


	@ModelAttribute("uvv")
	public Users pre4(){
		System.out.println("...pre4()....Preprocess and place objects in Model/ModelMap");
		//It may be created by querying data or receiving parameters
		Users u = new Users("A001","Breaking wheel",40);
		return u;
	}
	
	@RequestMapping("/aaa")
	public String s1( Model md, ModelMap mdMap , ModelAndView mv){
		System.out.println(this.getClass()+"Journal 1..." + md.asMap().get("uvv"));
		System.out.println(this.getClass()+"Journal 2..." + mdMap.get("uvv"));
		System.out.println(this.getClass()+"Journal 3..." + mv.getModelMap().get("uvv"));
		return "test2";
	}

Preprocessing - object processing + model

When the parameter of preprocessing method is (Model md), it can be saved to Model/ModelMap through md.addAttribute("",u)
Code demonstration:


	@ModelAttribute
	public void pre5(Model md){
		System.out.println("...pre5()....Preprocess and place objects in Model/ModelMap");
		//It may be created by querying data or receiving parameters
		Users u = new Users("A001","Welding Emperor Wu",40);
		md.addAttribute("www", u);
	}
	
	@RequestMapping("/aaa")
	public String s1( Model md, ModelMap mdMap , ModelAndView mv){
		System.out.println(this.getClass()+"Journal 1..." + md.asMap().get("www"));
		System.out.println(this.getClass()+"Journal 2..." + mdMap.get("www"));
		System.out.println(this.getClass()+"Journal 3..." + mv.getModelMap().get("www"));
		return "test2";
	}

Preprocessing - object processing + parameter @ ModelAttribute

The parameter md.addAttribute("xxx", u) in the preprocessing method;
It can be obtained in the parameter of another method
(@ModelAttribute("xxx") Users t)
So t is the object stored in the preprocessing method
Code demonstration:


	@ModelAttribute
	public void pre6(Model md){
		System.out.println("...pre6()....Preprocess and place objects in Model/ModelMap");
		//It may be created by querying data or receiving parameters
		Users u = new Users("A001","Sugar Taizong",40);
		md.addAttribute("xxx", u);
	}
	
	@RequestMapping("/aaa")
	public String s1(@ModelAttribute("xxx") Users t){
		System.out.println(this.getClass()+"Journal 1..." + t);
		System.out.println(this.getClass()+"Journal 2..." + t);
		return "test2";
	}

The parameters of the method are Model,ModelMap,ModeAndView. When the return type is String

Save some data,

	md.addAttribute("kkk", "qxs Tsien Hsueshen");
	mdMap.addAttribute("vvv", "ylp Yuan Longping");
	mv.addObject("xxx", "djx Deng Jia Xian");

When forwarding to page
On the page, kkk{kkk}kkk{vvv} can get the corresponding value ${xxx} can't
When redirecting to a page

	1.${kkk}<br/>
	2.${vvv}<br/>
	3.${xxx}<br/>
	a.${param.kkk}<br/>
	b.${param.vvv}<br/>
	c.${param.xxx}<br/>

123 is null,ab has value, c is null, null means no display
The code is as follows:



	@RequestMapping("/aaa")
	public String f1(Model md,ModelMap mdMap,ModelAndView mv){
		System.out.println(this.getClass()+"Journal...Model/ModelMap/ModelAndView Forward");
	
		md.addAttribute("kkk", "qxs Tsien Hsueshen");
		mdMap.addAttribute("vvv", "ylp Yuan Longping");
		mv.addObject("xxx", "djx Deng Jia Xian");
		return "test2";
	}
	
	@RequestMapping("/bbb")
	public String f2(Model md,ModelMap mdMap,ModelAndView mv){
		System.out.println(this.getClass()+"Journal...Model/ModelMap/ModelAndView redirect");
	
		md.addAttribute("kkk", "qxs Tsien Hsueshen");
		mdMap.addAttribute("vvv", "ylp Yuan Longping");
		mv.addObject("xxx", "djx Deng Jia Xian");
		return "redirect:/test1.jsp";
	}

The parameters of the method are Model,ModelMap,ModeAndView. When the return type is ModelAndView

Store value, return mv,
mv.setViewName("test2") forwarding method

	md.addAttribute("kkk", "qxs Tsien Hsueshen");
	mdMap.addAttribute("vvv", "ylp Yuan Longping");
	mv.addObject("xxx", "djx Deng Jia Xian");
	
	mv.setViewName("test2");
	return mv;

Page

	1.${kkk}<br/>
    2.${vvv}<br/>
    3.${xxx}<br/>

1, 2 and 3 can be taken as corresponding values

Redirection to
mv.setViewName("redirect:/test1.jsp");

	md.addAttribute("kkk", "qxs Tsien Hsueshen");
	mdMap.addAttribute("vvv", "ylp Yuan Longping");
	mv.addObject("xxx", "djx Deng Jia Xian");
	
	mv.setViewName("redirect:/test1.jsp");
	return mv;  

test1.jsp page

	1.${kkk}<br/>
	2.${vvv}<br/>
	3.${xxx}<br/>
	a.${param.kkk}<br/>
	b.${param.vvv}<br/>
	c.${param.xxx}<br/>

1,2,3 no value, abc has value
Thus it can be seen
When saving, a copy will be saved in the Model/ModelMap/ModelAndView collection and also in the request
Forwarding to the next resource will still be received from request.getattribute
The next resource needs to be received from request.getParamter when redirecting
ModelAndView needs to set that the return value of this method is also ModelAndView to access the resources in ModelAndView

Published 15 original articles · praised 1 · visited 81
Private letter follow

Posted by ayampanggang on Wed, 26 Feb 2020 20:29:22 -0800