Spring MVC 从 Controller向页面传值的方式

Spring MVC 从 Controller向页面传值的方式

Spring MVC  Controller向页面传值的方式

验证代码:https://files.cnblogs.com/files/peiyangjun/20180104_springMVC_easyui.zip

在实际开发中,Controller取得数据(可以在Controller中处理,当然也可以来源于业务逻辑层),传给页面,常用的方式有:

 

1、利用ModelAndView页面传值

后台程序如下:

复制代码

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

    public ModelAndView StartPage() {

         ModelMap map=new ModelMap();

         User user=new User();

         user.setPassword("123456");

         user.setUserName("ZhangSan");

         map.put("user", user);

    return new ModelAndView("reciveControllerData",map);

}

复制代码

 

页面程序如下:

 

复制代码

    <body>

    <h1>recive Data From Controller</h1>

    <br>

      用户名:${user.userName }   

      <br>

      密码:${user.password }

</body>

</html>

复制代码

 

注意:

     ModelAndView总共有七个构造函数,其中构造函中参数model就可以传参数。具体见ModelAndView的文档,model是一个Map对象,在其中设定好key与value值,之后可以在视图中取出。

从参数定义Map<String, ?> model ,可知,任何Map的对象,都可以作为ModeAndView的参数。

 

2、 ModelMap作为函数参数调用方式

    

复制代码

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

    public ModelAndView StartPage2(ModelMap map) {      

         User user=new User();

         user.setPassword("123456");

         user.setUserName("ZhangSan"); 

         map.put("user", user);

    return new ModelAndView("reciveControllerData");

}

复制代码

 

3、使用@ModelAttribute注解

方法1:@modelAttribute在函数参数上使用,在页面端可以通过HttpServletRequest传到页面中去

        

 View Code

 

方法2:@ModelAttribute在属性上使用

  

复制代码

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

    public ModelAndView StartPage4() {       

       sthname="LiSi";

 

       return new ModelAndView("reciveControllerData");

} 

          /*一定要有sthname属性,并在get属性上取加上@ModelAttribute属性*/

  private String sthname;  

    @ModelAttribute("name") 

    public String getName(){ 

       

       return sthname; 

    }

复制代码

 

                        

4、 使用@ModelAttribute注解

/*直接用httpServletRequest的Session保存值。

     * */

    

复制代码

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

    public ModelAndView StartPage5(HttpServletRequest request) {      

         User user=new User();

         user.setPassword("123456");

         user.setUserName("ZhangSan"); 

         HttpSession session=request.getSession();

         session.setAttribute("user", user);

    return new ModelAndView("reciveControllerData");

}

复制代码

转载:https://www.cnblogs.com/peiyangjun/p/8183438.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring MVC是一种基于MVC(Model-View-Controller)设计模式的Web框架,它可以帮助我们快速地开发Web应用程序。在Spring MVC中,Controller是控制器的核心组件,它负责接收用户请求并决定如何处理这些请求。 在Spring MVC中,我们可以通过编写Controller类来实现请求的处理。在Controller类中,我们可以定义多个方法,每个方法对应处理一个具体的请求。这些方法通常使用注解来标识它们应该处理哪些请求。 例如,我们可以使用@Controller注解来标识一个类为Controller,并使用@RequestMapping注解来标识一个方法应该处理哪些请求。下面是一个简单的Controller类的示例: ``` @Controller @RequestMapping("/hello") public class HelloController { @RequestMapping("/world") public ModelAndView helloWorld() { String message = "Hello World, Spring MVC!"; return new ModelAndView("hello", "message", message); } } ``` 在上面的示例中,我们使用@Controller注解将HelloController类标识为Controller,并使用@RequestMapping注解将该类处理的请求路径设置为“/hello”。我们还使用@RequestMapping注解将helloWorld方法标识为处理“/hello/world”请求的方法。该方法返回一个包含“Hello World, Spring MVC!”消息的ModelAndView对象。 需要注意的是,Controller类的方法可以返回不同类型的结果。除了ModelAndView之外,还可以返回String、void、HttpEntity、ResponseEntity等类型的结果。这些结果将由Spring MVC框架进行处理,并将相应的内容返回给客户端。 总的来说,Spring MVCController是Web应用程序的核心组件之一,它可以帮助我们快速地开发功能强大、易于维护的Web应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值