Detailed explanation of three writing methods of SpringMVC's Contrller controller layer

Reprint link: https://www.cnblogs.com/zjf-1992/p/5736101.html

What is the MVC framework

Model-View-Controller (MVC) is a well-known design pattern based on designing interface applications . It decouples business logic from the interface mainly by separating the roles of model, view, and controller in the application. Usually, the model is responsible for encapsulating the application data and displaying it in the view layer. The view only displays the data and does not contain any business logic. The controller is responsible for receiving requests from users and calling background services (manager or dao) to process business logic. After processing, the background business layer may return some data for display in the view layer. The controller collects these data and prepares the model for display in the view layer. The core idea of ​​the MVC pattern is to separate the business logic from the interface and allow them to be changed separately without affecting each other.

package com.platform.mvc.system.layer.controller.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.catalina.connector.Request;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
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
@RequestMapping("/user")
public class userCtroller {
    
    //方式一:String
    @RequestMapping("test")
    public String list(Map<String, Object> map){
        创建一个数组对象
        List<Map<String, Object>> userList = new ArrayList<Map<String, Object>>();
        for (int i = 0; i <10; i++) {
            //存储对象map<K, V>
            Map<String, Object> map1 = new HashMap<String, Object>();
            map.put("name", "HelloKitty"+i);
            map.put("age", i);
            map.put("dept", "技术部"+i);
            //将map对象存储在userList数组中
            userList.add(map);
        }
        //[{age=Avenstar4, name=Avenstar4}]
        map.put("elList", userList);
        //返回视图
        return "system/userList";
    }
    
    
    //方式二:ModelAndView
    @RequestMapping(value="test2", method=RequestMethod.GET)
    public ModelAndView list2(Model model){
        //创建一个视图对象
        ModelAndView view = new ModelAndView();
        //创建一个数组对象
        List<Map<String, Object>> userList = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < 5; i++) {
            //存储对象map<K, V>
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("name", "HelloKitty"+i);
            map.put("age", i);
            map.put("dept", "技术部"+i);
            userList.add(map);
        }
        //绑定数据
        model.addAttribute("userList", userList);
        //设置视图
        view.setViewName("system/userList");

        return view;
    }
    
    
    //方式三:AJAX
    //接收参数:@RequestParam(value="username", required=true, defaultValue="hello") String username
    @RequestMapping(value="/test3", method=RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> list3(HttpServletRequest request, @RequestParam(value="username", required=true, defaultValue="hello") String username){
       
        Map<String, Object> map = new HashMap<>();
        //创建一个数组对象
        List<Map<String, Object>> userList = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < 5; i++) {
            //存储对象map<K, V>
            Map<String, Object> map1 = new HashMap<String, Object>();
            map1.put("name", "HelloKitty"+i);
            map1.put("age", i);
            map1.put("dept", "技术部"+i);
            userList.add(map1);
        }
        
        map.put("userList", userList);

        return map;
    }

}

 

Guess you like

Origin blog.csdn.net/qq_30764991/article/details/102655985