ModelAndView的用法

发布时间:2025-12-09 11:58:56 浏览次数:1

package com.gxa.spmvc.controller;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.servlet.ModelAndView;import com.gxa.spmvc.entity.Student;/** * SpringMVC的控制器(业务控制器) * 定义的方法就是一个请求处理的方法 * @author caleb * */@Controller@RequestMapping("/user")public class TestController {        /**     * 利用ModelAndView来转发数据,给前端视图     * @return     */    @RequestMapping("/m06")    public ModelAndView m06() {        ModelAndView modelAndView = new ModelAndView();        modelAndView.setViewName("m06");        modelAndView.addObject("message", "Hello World, Hello Kitty");        return modelAndView;    }    }

  

package com.gxa.spmvc.controller;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.servlet.ModelAndView;import com.gxa.spmvc.entity.Student;/** * SpringMVC的控制器(业务控制器) * 定义的方法就是一个请求处理的方法 * @author caleb * */@Controller@RequestMapping("/user")public class TestController {        /**     * 利用ModelAndView来转发数据,给前端视图     * @return     */    @RequestMapping("/m07")    public ModelAndView m07() {        return new ModelAndView("m07", "message", "Hello World");    }    }

  

package com.gxa.spmvc.controller;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.servlet.ModelAndView;import com.gxa.spmvc.entity.Student;/** * SpringMVC的控制器(业务控制器) * 定义的方法就是一个请求处理的方法 * @author caleb * */@Controller@RequestMapping("/user")public class TestController {        /**     * ModelAndView默认转发     * ModelAndView还是可以设置重定向     * 1. 重定向另一个控制器     * 2. 重定向具体的jsp页面     * @param name     * @return     */    @RequestMapping("/{name}/m07")    public ModelAndView m07(@PathVariable String name) {        if (!"admin".equals(name)) {            return new ModelAndView("redirect:/m07.jsp");        }        return new ModelAndView("m07");    }    }

  

modelandview
需要做网站?需要网络推广?欢迎咨询客户经理 13272073477