Controller返回值string、mv等區別

package com.buba.controller;
 
import com.buba.pojo.User;
import com.buba.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
 
@RestController
public class UserController {
 
    @Resource
    private UserService userService;
 
    //返回index,這個字符串
    @RequestMapping("/1")
    @ResponseBody
    public String index() {
        return "index";
    }
 
    //返回index2.jsp這個頁面裏面的內容
    @RequestMapping("/2")
    public String index2(){
        return "index2";
    }
 
    /**
     * 返回的是視圖加模型
     * 視圖是view     指的是視圖的名字,通過試圖解析器指向到相對應的視圖上
     * 模型是model    指的是一個類型,好比list,對象,map,在前臺頁面上能夠取出相對應的東西
     * @param model
     * @return
     */
    @RequestMapping("/3")
    public ModelAndView index3(Model model){
        model.addAttribute("list",userService.select());
        return new ModelAndView("index","usermodel",model);
    }
 
    @RequestMapping("/4")
    public ModelAndView index4(Map<String,Object> map){
        map.put("list",userService.select());
        return new ModelAndView("index","usermodel",map);
    }
 
    //ajax作請求的時候
    @RequestMapping("/5")
    @ResponseBody
    public Object index5(){
        return userService.select();
    }
 
    @RequestMapping("/6")
    public void index6(String vname, String vpassword, HttpServletRequest request, HttpServletResponse response) throws Exception{
        request.setAttribute("vname", vname);
        request.setAttribute("vpassword", vpassword);
        System.out.println(vname+" "+vpassword);
        request.getRequestDispatcher("/index.jsp").forward(request, response);
    }
 
}
————————————————
版權聲明:本文爲CSDN博主「墨咖」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處連接及本聲明。
原文連接:https://blog.csdn.net/qq_37335810/article/details/79074952java

相關文章
相關標籤/搜索