SpringMVC 中ModelAndView用法

SpringMVC 中ModelAndView用法ModelAndView作用1.返回到指定的页面ModelAndView构造方法可以指定返回的页面名称   例:returnnewModelAndView("redirect:/m07.jsp");通过setViewName()方法跳转到指定的页面   例:mav.setViewName("hello"); 2.返回参数到指定页面的request作用于中使…

大家好,又见面了,我是你们的朋友全栈君。

ModelAndView 作用

1.返回到指定的页面

ModelAndView构造方法可以指定返回的页面名称

     例:return new ModelAndView(“redirect:/m07.jsp”);

通过setViewName()方法跳转到指定的页面

     例:mav.setViewName(“hello”);

 2.返回参数到指定页面的request作用于中

使用addObject()设置需要返回的值,addObject()有几个不同参数的方法,可以默认和指定返回对象的名字,参数会返回到新页面的request作用域中

 

ModelAndView 的3种用法

1.ModelAndView的第一种用法,先创建ModelAndView对象,再通过它的方法去设置数据与转发的视图名

  • setViewName(String viewName):‎设置此 ModelAndView 的视图名称, 由 DispatcherServlet 通过 ViewResolver 解析‎
  • addObject(String attributeName, Object attributeValue):通过key/value的方式绑定数据
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;
    }
    
}

2.ModelAndView的第二种方法,可以直接通过带有参数的构造方法 ModelAndView(String viewName, String attributeName, Object attributeValue) 来返回数据与转发的视图名

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");
    }
    
}

3.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默认转发
     * 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使用实例

要点:

1.@RequestMapping 注解的使用

2.modelandview 的使用

3.jsp页面request作用域的取值

4.视图解析器配置

ModelAndView 使用代码

package com.dgr.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@RequestMapping("mvc")
@Controller
public class TestRequestMMapping {
	
	@RequestMapping(value="/testModelAndView")
	public ModelAndView testModelAndView(){
		ModelAndView mav = new ModelAndView();
		mav.setViewName("hello");//跳转新的页面名称
		mav.addObject("address", "中国广东省广州市");//传入request作用域参数
		return mav;
	}
}

跳转前jsp页面链接设置

	<a href="mvc/testModelAndView">Test ModelAndView</a>

跳转后jsp页面以及request作用于取值

<title>New Page</title>
</head>
<body>
	<h1>ModelAndView 跳转</h1>
	
	<br>
	
	${requestScope.address}   
	
	<br>
	
	${address }    

	<br>	
	
</body>

视图解析器配置

SpringMVC 中ModelAndView用法

SpringMVC 中ModelAndView用法

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/161006.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • json字符串转换成对象有哪几种方法_jsonstring转对象

    json字符串转换成对象有哪几种方法_jsonstring转对象1.将json字符串转化为json对象a.方案一:jquery自带的$.parseJSON函数&amp;lt;script&amp;gt;varjsonstr=&quot;{\&quot;id\&quot;:\&quot;1\&quot;,\&quot;name\&quot;:\&quot;jack\&quot;}&quot;;varobj=$.parseJSON(jsonstr);&a

    2022年9月1日
    5
  • 西门子plc scl语言很少人用_西门子plc的scl语言

    西门子plc scl语言很少人用_西门子plc的scl语言原标题:为什么说SCL将成为西门子PLC的主流编程语言接触S7-1200的时间不是很长,但个人感觉TIAPROTAL中的SCL编程语言还不错,下面是我写的一个传送带的启停程序:bnnyygysaid:我献丑来一个,半成品,给设备改造的,用的欧姆龙CP1L,ST语言功能块,部分节选。wenpiansaid:还是梯形图适合逻辑。ljj977said:程序写的不错。tiaprotal中可以…

    2025年11月12日
    2
  • datagrid激活码(破解版激活)「建议收藏」

    datagrid激活码(破解版激活),https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月16日
    51
  • java mediatype utf-8_Java MediaType.APPLICATION_JSON_UTF8屬性代碼示例

    java mediatype utf-8_Java MediaType.APPLICATION_JSON_UTF8屬性代碼示例/***Searches{@linkorg.springframework.web.bind.annotation.RequestMappingRequestMapping}*annotationonthegivenmethodargumentandextracts*IfRequestMappingannotationisnotfound,NoRequestM…

    2022年5月12日
    35
  • HBase Shell命令大全「建议收藏」

    HBase Shell命令大全「建议收藏」HBase关键名称:RowKey列族columnfamily单元Cell时间戳timestampHBaseShell是官方提供的一组命令,用于操作HBase。如果配置了HBase的环境变量了,就可以知己在命令行中输入hbaseshell命令进入命令行。hbaseshellhelp命令可以通过help’命名名称’来查看命令行的具体使用查询服务器状态st…

    2022年7月16日
    14
  • C++宏和枚举

    宏我们的计算器程序,用1234对应加减乘除,对于人阅读很产生一点障碍。隔一个月后再看此代码可能想不起是0123还是1234了,还得去代码中查找,如果能为代表四则运算的四个数取个有意义的别名就好了,一

    2021年12月24日
    52

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号