Spring框架---springmvc中ModelAndView簡單使用

我們先看圖來演示一下ModelAndView這個方法 

 

再來看源碼 

package com.qcr.controllers;


	import java.util.ArrayList;
	import java.util.HashMap;
	import java.util.Map;
	import org.springframework.stereotype.Controller;
	import org.springframework.web.bind.annotation.RequestBody;
	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.ResponseBody;
	import org.springframework.web.servlet.ModelAndView;
	@Controller
	public class Goods {
		@ResponseBody
		

	    @RequestMapping(value ="views/goods",method ={RequestMethod.GET})
	    public ModelAndView goods(String name){ 
			System.out.println(1231);
	    	ModelAndView mv = new ModelAndView();
	    	mv.addObject("title","EN MÉRINOS");
	    	mv.addObject("price","1,000$");
	    	mv.addObject("desc","Cardigan à manches longues en laine mérinos. Encolure ronde. Fermeture par boutons recouverts de maille. Finition bords côtes bas de manches et de vêtement. Visuel retouché.");
	    	mv.addObject("url","http://localhost:8080/shopping/views/images/goods/p1.jpg");
	    	mv.setViewName("goods/Goods_page");
	        return mv;
	    }
	}

我們在這個方法裏首先用ModelAndView定義了一個mv對象,然後用add方法往裏面添加數據,最後return回去.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章