好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

利用springmvc处理模型数据

springmvc处理模型数据

很多情况下页面上需要很多数据,单单返回页面是不行的,那么springmvc如何将数据返回到该页面呢

springmvc提供了四种方式来输出模型数据

ModelAndView: 处理返回值为ModelAndView时,可以将该对象中添加数据模型 Map及Model:入参为Model、ModelMap或Map时,处理方法返回时,Map中的数据会自动添加到模型中 @SessionAttributes: 将模型中的某个属性暂存到HttpSession中,以便多个请求之间共享数据 @ModelAttribute: 方法入参标注该注解后,入参的对象就会放到数据模型中

ModelAndView

主要有两个重要的变量

?

1

2

3

4

// 视图 可以传字符串(视图名字)也可以传View对象

private Object view;

// 数据模型 本质是一个map

private ModelMap model;

视图相关的方法

?

1

2

3

4

5

6

7

8

// 设置视图

public void setViewName(String viewName) {

  this .view = viewName;

}

// 获取视图

public String getViewName() {

  return this .view instanceof String ? (String) this .view : null ;

}

数据模型相关方法

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

// 获取数据模型

protected Map<String, Object> getModelInternal() {

  return this .model;

}

 

public ModelMap getModelMap() {

  if ( this .model == null ) {

  this .model = new ModelMap();

  }

 

  return this .model;

}

 

public Map<String, Object> getModel() {

  return this .getModelMap();

}

 

// 添加视图模型

public ModelAndView addObject(String attributeName, Object attributeValue) {

  this .getModelMap().addAttribute(attributeName, attributeValue);

  return this ;

}

springmvc底层使用request.setAttribute(name,value)来将数据放入到请求中

示例:

?

1

2

3

4

5

6

7

8

@RequestMapping ( "/modelAndViewTest" )

public ModelAndView modelAndViewTest(){

  // 视图名

  ModelAndView modelAndView = new ModelAndView( "modelAndViewTest" );

  // 包含的数据

  modelAndView.addObject( "dateTime" , new Date());

  return modelAndView;

}

Map及Model

?

1

2

3

4

5

6

@RequestMapping ( "/mapTest" )

public String mapTest(Map<String,String> map){

  System.out.println(map.getClass()); //class org.springframework.validation.support.BindingAwareModelMap

  map.put( "name" , "张三" );

  return "hello" ;

}

@SessionAttributes

在类上添加@SessionAttributes可以使该类所代表的路径下的session共享

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

@Controller

@RequestMapping ( "helloWorld" )

// 设置name属性共享

@SessionAttributes (value={ "name" })

public class HelloWorldController {

 

  @RequestMapping ( "/mapTest" )

  public String mapTest(Map<String,String> map){

  System.out.println(map.getClass()); //class org.springframework.validation.support.BindingAwareModelMap

  map.put( "name" , "张三" );

  return "hello" ;

  }

 

  // 可以在该方法中获取到name值为张三

  @RequestMapping ( "/sessionAttributes" )

  public String sessionAttributes(HttpSession session){

  System.out.println(session.getAttribute( "name" ));

  return "hello" ;

  }

}

@ModelAttribute

用在无返回值的方法

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

package com.yiidian.controller;

 

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.ModelAttribute;

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

import org.springframework.web.bind.annotation.RequestParam;

 

 

@Controller

public class ModelAttributeController {

  

   //没有返回值的情况

   @ModelAttribute

   public void myModel( @RequestParam (required = false ) String name, Model model) {

     model.addAttribute( "name" , name);

   }

 

   @RequestMapping (value = "/model" )

   public String model() {

     return "success" ;

   }

}

用在带返回值的方法

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

package com.yiidian.controller;

 

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.ModelAttribute;

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

import org.springframework.web.bind.annotation.RequestParam;

 

 

@Controller

public class ModelAttributeController {

 

   /**

    * 带返回值的情况

    * @param name

    */

   @ModelAttribute ( "name" )

   public String myModel( @RequestParam (required = false ) String name) {

     return name;

   }

 

   @RequestMapping (value = "/model" )

   public String model() {

     return "success" ;

   }

}

应用在方法的参数上

?

1

2

3

4

5

6

7

8

9

10

11

@ModelAttribute ( "name" )

public String myModel( @RequestParam (required = false ) String name) {

   return name;

}

 

//应用在方法的参数行

@RequestMapping (value = "/model" )

public String model( @ModelAttribute ( "name" ) String name) {

   System.out.println( "name=" +name);

   return "success" ;

}

以上就是利用springmvc 处理模型数据的详细内容,更多关于springmvc 处理模型数据的资料请关注其它相关文章!

原文链接:http://zhhll.icu/2021/01/07/%E6%A1%86%E6%9E%B6/springmvc/springmvc%E5%A4%84%E7%90%86%E6%A8%A1%E5%9E%8B%E6%95%B0%E6%8D%AE/

查看更多关于利用springmvc处理模型数据的详细内容...

  阅读:19次