SpingMVCModelAndViewModelControl以及参数传递.docx

上传人:b****6 文档编号:2922087 上传时间:2022-11-16 格式:DOCX 页数:14 大小:21.70KB
下载 相关 举报
SpingMVCModelAndViewModelControl以及参数传递.docx_第1页
第1页 / 共14页
SpingMVCModelAndViewModelControl以及参数传递.docx_第2页
第2页 / 共14页
SpingMVCModelAndViewModelControl以及参数传递.docx_第3页
第3页 / 共14页
SpingMVCModelAndViewModelControl以及参数传递.docx_第4页
第4页 / 共14页
SpingMVCModelAndViewModelControl以及参数传递.docx_第5页
第5页 / 共14页
点击查看更多>>
下载资源
资源描述

SpingMVCModelAndViewModelControl以及参数传递.docx

《SpingMVCModelAndViewModelControl以及参数传递.docx》由会员分享,可在线阅读,更多相关《SpingMVCModelAndViewModelControl以及参数传递.docx(14页珍藏版)》请在冰豆网上搜索。

SpingMVCModelAndViewModelControl以及参数传递.docx

SpingMVCModelAndViewModelControl以及参数传递

1.web.xml 配置:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

15px;">

    dispatcher

    org.springframework.web.servlet.DispatcherServlet

    

        加载/WEB-INF/spring-mvc/目录下的所有XML作为SpringMVC的配置文件

        contextConfigLocation

        /WEB-INF/spring-mvc/*.xml

    

    1

    dispatcher

    *.htm

 

  这样,所有的.htm的请求,都会被DispatcherServlet处理;

初始化DispatcherServlet时,该框架在web应用程序WEB-INF目录中寻找一个名为[servlet-名称]-servlet.xml的文件,并在那里定义相关的Beans,重写在全局中定义的任何Beans,像上面的web.xml中的代码,对应的是dispatcher-servlet.xml;当然也可以使用元素,手动指定配置文件的路径;dispatcher-servlet.xml 配置:

1

xmlversion="1.0"encoding="UTF-8"?

>

2

//www.springframework.org/schema/beans"

3xmlns:

xsi="http:

//www.w3.org/2001/XMLSchema-instance"

4xmlns:

mvc="http:

//www.springframework.org/schema/mvc"

5xmlns:

p="http:

//www.springframework.org/schema/p"

6xmlns:

context="http:

//www.springframework.org/schema/context"

7xmlns:

aop="http:

//www.springframework.org/schema/aop"

8xmlns:

tx="http:

//www.springframework.org/schema/tx"

9xsi:

schemaLocation="http:

//www.springframework.org/schema/beans

10http:

//www.springframework.org/schema/beans/spring-beans-3.0.xsd

11http:

//www.springframework.org/schema/context

12http:

//www.springframework.org/schema/context/spring-context-3.0.xsd

13http:

//www.springframework.org/schema/aop

14http:

//www.springframework.org/schema/aop/spring-aop-3.0.xsd

15http:

//www.springframework.org/schema/tx

16http:

//www.springframework.org/schema/tx/spring-tx-3.0.xsd

17http:

//www.springframework.org/schema/mvc

18http:

//www.springframework.org/schema/mvc/spring-mvc-3.0.xsd

19http:

//www.springframework.org/schema/context

20http:

//www.springframework.org/schema/context/spring-context-3.0.xsd">

21

--

22使Spring支持自动检测组件,如注解的Controller

23-->

24

component-scanbase-package="com.minx.crm.web.controller"/>

25

26

27class="org.springframework.web.servlet.view.InternalResourceViewResolver"

28p:

prefix="/WEB-INF/jsp/"

29p:

suffix=".jsp"/>

30

 

2.springmvc处理方法支持如下的返回方式:

ModelAndView,Model,ModelMap,Map,View,String,void

 

ModelAndView

 

1.   

2.@RequestMapping("/show1") 

3.public ModelAndViewshow1(HttpServletRequestrequest, 

4.          HttpServletResponseresponse) throws Exception{ 

5.      ModelAndViewmav= new ModelAndView("/demo2/show"); 

6.      mav.addObject("account", "account-1"); 

7.       return mav; 

8.  } 

通过ModelAndView构造方法可以指定返回的页面名称,也可以通过setViewName()方法跳转到指定的页面,使用addObject()设置需要返回的值,addObject()有几个不同参数的方法,可以默认和指定返回对象的名字。

调用addObject()方法将值设置到一个名为ModelMap的类属性,ModelMap是LinkedHashMap的子类,具体请看类。

 

 

Model是一个接口,其实现类为ExtendedModelMap,继承了ModelMap类。

model.addAttribute("pojo",pojo);

Map 

 

1.@RequestMapping("/demo2/show") 

2.    public MapgetMap(){ 

3.       Mapmap= new HashMap(); 

4.       map.put("key1", "value-1"); 

5.       map.put("key2", "value-2"); 

6.        return map; 

7.   } 

 

在jsp页面中可直通过${key1}获得到值,map.put()相当于request.setAttribute方法。

写例子时发现,key值包括-.时会有问题.

View可以返回pdfexcel等,暂时没详细了解。

 

 

String指定返回的视图页面名称,结合设置的返回地址路径加上页面名称后缀即可访问到。

 

注意:

如果方法声明了注解@ResponseBody ,则会直接将返回值输出到页面。

例如:

1.@RequestMapping(value= "/something",method=RequestMethod.GET) 

2.@ResponseBody 

3.public StringhelloWorld() { 

4.return"HelloWorld"; 

5.} 

上面的结果会将文本"HelloWorld"直接写到http响应流。

1.@RequestMapping("/welcome") 

2.public StringwelcomeHandler(){ 

3.  return"center"; 

4.} 

对应的逻辑视图名为“center”,URL=prefix前缀+视图名称+suffix后缀组成。

void 如果返回值为空,则响应的视图页面对应为访问地址

1.@RequestMapping("/welcome") 

2.publicvoid welcomeHandler(){} 

此例对应的逻辑视图名为"welcome"。

小结:

1.使用String作为请求处理方法的返回值类型是比较通用的方法,这样返回的逻辑视图名不会和请求URL绑定,具有很大的灵活性,而模型数据又可以通过ModelMap控制。

2.使用void,map,Model时,返回对应的逻辑视图名称真实url为:

prefix前缀+视图名称+suffix后缀组成。

3.使用String,ModelAndView返回视图名称可以不受请求的url绑定,ModelAndView可以设置返回的视图名称。

 

 

 

Modelmodel,HttpServletRequestrequest,ModelMapmap声明变量

 

request.getSession().setAttribute("test","haiwei2Session");request.setAttribute("test","haiwei1request");map.addAttribute("test","haiweiModelMap");model.addAttribute("test","haiweiModel");

我通过${test}这个方式取值,优先取Model和ModelMap的,Model和ModelMap是同一个东西,谁最后赋值的就取谁的,然后是request,最后是从session中获取

 

 第一个Controller:

1.package com.minx.crm.web.controller;  

2.  

3.import org.springframework.stereotype.Controller;  

4.import org.springframework.web.bind.annotation.RequestMapping;  

5.@Controller  

6.public class IndexController {  

7.    @RequestMapping("/index")  

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 表格模板 > 调查报告

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1