开发界
首页 移动开发 在线交易 IT笔记 网络技术 操作系统 企业架构 数据库 考试认证 IT培训 开源软件 数据存储 行业资讯
大家都在看:java   开发   C#   oracle   mysql   android   web开发   学习   sqlserver   linux   asp   php   ajax   javascript   css   html
基于Spring MVC的Web应用开发(10) - Views --Form
2013-02-25 13:05:17 来源:开发界 作者:admin 栏目:spring 责任编辑:admin
[导读]在FileUpload一文中,我们初步了解了SpringMVC中View的用法,在例子中,通过给Model添加一个属性(model.addAttribute()),View对应的JSP就可以获取该值。

  在FileUpload一文中,我们初步了解了SpringMVC中View的用法,在例子中,通过给Model添加一个属性(model.addAttribute()),View对应的JSP就可以获取该值。本文再介绍一些View对应JSP取值的方式。

  增加一个Controller,ViewsController:

  Java代码

  package org.springframework.samples.mvc.views;

  import javax.validation.Valid;

  import org.springframework.stereotype.Controller;

  import org.springframework.ui.Model;

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

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

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

  @Controller

  @RequestMapping("/views/*")

  public class ViewsController {

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

  public String prepare(Model model) {

  model.addAttribute("foo", "bar");

  model.addAttribute("fruit", "apple");

  return "views/html";

  }

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

  public void usingRequestToViewNameTranslator(Model model) {

  model.addAttribute("foo", "bar");

  model.addAttribute("fruit", "apple");

  }

  @RequestMapping(value="pathVariables/{foo}/{fruit}", method=RequestMethod.GET)

  public String pathVars(@PathVariable String foo, @PathVariable String fruit) {

  // No need to add @PathVariables "foo" and "fruit" to the model

  // They will be merged in the model before rendering

  return "views/html";

  }

  @RequestMapping(value="dataBinding/{foo}/{fruit}", method=RequestMethod.GET)

  public String dataBinding(@Valid JavaBean javaBean, Model model) {

  // JavaBean "foo" and "fruit" properties populated from URI variables

  return "views/dataBinding";

  }

  }

  Java代码

  package org.springframework.samples.mvc.views;

  import javax.validation.Valid;

  import org.springframework.stereotype.Controller;

  import org.springframework.ui.Model;

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

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

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

  @Controller

  @RequestMapping("/views/*")

  public class ViewsController {

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

  public String prepare(Model model) {

  model.addAttribute("foo", "bar");

  model.addAttribute("fruit", "apple");

  return "views/html";

  }

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

  public void usingRequestToViewNameTranslator(Model model) {

  model.addAttribute("foo", "bar");

  model.addAttribute("fruit", "apple");

  }

  @RequestMapping(value="pathVariables/{foo}/{fruit}", method=RequestMethod.GET)

  public String pathVars(@PathVariable String foo, @PathVariable String fruit) {

  // No need to add @PathVariables "foo" and "fruit" to the model

  // They will be merged in the model before rendering

  return "views/html";

  }

  @RequestMapping(value="dataBinding/{foo}/{fruit}", method=RequestMethod.GET)

  public String dataBinding(@Valid JavaBean javaBean, Model model) {

  // JavaBean "foo" and "fruit" properties populated from URI variables

  return "views/dataBinding";

  }

  }

  package org.springframework.samples.mvc.views;

  import javax.validation.Valid;

  import org.springframework.stereotype.Controller;

  import org.springframework.ui.Model;

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

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

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

  @Controller

  @RequestMapping("/views/*")

  public class ViewsController {

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

  public String prepare(Model model) {

  model.addAttribute("foo", "bar");

  model.addAttribute("fruit", "apple");

  return "views/html";

  }

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

  public void usingRequestToViewNameTranslator(Model model) {

  model.addAttribute("foo", "bar");

  model.addAttribute("fruit", "apple");

  }

  @RequestMapping(value="pathVariables/{foo}/{fruit}", method=RequestMethod.GET)

  public String pathVars(@PathVariable String foo, @PathVariable String fruit) {

  // No need to add @PathVariables "foo" and "fruit" to the model

  // They will be merged in the model before rendering

  return "views/html";

  }

  @RequestMapping(value="dataBinding/{foo}/{fruit}", method=RequestMethod.GET)

  public String dataBinding(@Valid JavaBean javaBean, Model model) {

  // JavaBean "foo" and "fruit" properties populated from URI variables

  return "views/dataBinding";

  }

  }

  1. 访问"http://localhost:8080/web/views/html",返回到"webapp/WEB-INF/views/views/html.jsp":

  Html代码

  

版权所有:转载请注明出处!
分享到:
上一篇Spring事务类型祥解 下一篇rabbitmq 学习-7- 官方rabbitmq+s..
您可能还喜欢
今日最新资讯 最新推荐信息
考试认证 更多
开发技术 更多
移动开发 更多

关于开发界 | 合作伙伴 | 联系我们 | 友情链接 | 版权声明 | 网站制作 | 网站地图 | 加入收藏 | 设为首页

Copyright@2012-2016 开发界 京ICP备12027873号