一、Multiaction Controller

  1. package cn.framelife.mvc.control;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import org.springframework.web.bind.annotation.RequestMethod;
  5. import org.springframework.web.servlet.ModelAndView;
  6. import cn.framelife.mvc.entity.User;
  7. @Controller
  8. @RequestMapping("/user")
  9. public class UserControl {
  10. /**
  11. * 这种方法接收/user/create.abc请求
  12. */
  13. @RequestMapping("create")
  14. public ModelAndView createUser(User user){
  15. System.out.println("createUser");
  16. ModelAndView view = new ModelAndView();
  17. view.setViewName("/success");
  18. return view;
  19. }
  20. /**
  21. * 这种方法接收/user/update.abc请求
  22. */
  23. @RequestMapping("update")
  24. public ModelAndView update(){
  25. System.out.println("updateUser");
  26. ModelAndView view = new ModelAndView();
  27. view.setViewName("/success");
  28. return view;
  29. }
  30. }

二、 ModelAndView

  1. Controller处理方法的返回值为ModelAndView,既包括视图信息,也包括模型数据信息。

1、ModelAndView中的方法

能够使用下面方法加入模型数据:

  1. addObject(Object modelObject)
  2. addObject(String modelName, Object modelObject)
  3. addAllObjects(Map modelMap)

能够通过下面方法设置视图:

  1. setViewName(String viewName)
  2. setView(View view)

2、重定向:

这里的重定向仅仅能重定向到其他的处理方法,不能重定向到页面。

假设想重定向到页面,那么在另外的的处理方法中跳转就是。

  1. @RequestMapping(value="/add",method = RequestMethod.GET)
  2. public ModelAndView initForm(){
  3. User user = new User();
  4. return new ModelAndView("/add").addObject(user);
  5. }
  6. @RequestMapping("create")
  7. public ModelAndView createUser(){
  8. ModelAndView view = new ModelAndView();
  9. //这里会重定向到add.abc的处理方法中,再由add.abc的处理方法作页面跳转
  10. RedirectView redirectView = new RedirectView("add.abc");
  11. view.setView(redirectView);
  12. return view;
  13. }

三、Controler处理方法获取值

1、获取页面值

页面Form表单:

  1. <form action="user/create.abc" method="post">
  2. 用户名:<input type="text" name="username"><br/>
  3. 密 码:<input type="text" name="password"><br/>
  4. 其他:<input type="text" name="other"><br/>
  5. <input type="submit">
  6. </form>

A、 绑定到同名參数中

  1. /*
  2. * @RequestParam能够用来提取名为“username”的String类型的參数。并将之作为输入參数传入
  3. */
  4. @RequestMapping("create")
  5. public ModelAndView createUser(@RequestParam("username") String username,String password,String other){
  6. System.out.println(username+"-"+password+"-"+other);
  7. ModelAndView view = new ModelAndView();
  8. view.setViewName("/success");
  9. return view;
  10. }

B、 绑定到实体类模型数据中

User实体类:

  1. public class User implements java.io.Serializable {
  2. private Integer id;
  3. private String username;
  4. private String password;
  5. public Integer getId() {
  6. return id;
  7. }
  8. public void setId(Integer id) {
  9. this.id = id;
  10. }
  11. public String getUsername() {
  12. return username;
  13. }
  14. public void setUsername(String username) {
  15. this.username = username;
  16. }
  17. public String getPassword() {
  18. return password;
  19. }
  20. public void setPassword(String password) {
  21. this.password = password;
  22. }
  23. }

Controller中的方法:

  1. @RequestMapping("create")
  2. public ModelAndView createUser(User user){
  3. System.out.println(user.getUsername()+"-"+user.getPassword());
  4. ModelAndView view = new ModelAndView();
  5. view.setViewName("/success");
  6. return view;
  7. }

C、既有模型又有同名參数

  1. /**
  2. * 页面表单值传输时,假设User类中有同名的属性。那就绑定到User对象中
  3. * 假设User类中没有的属性,能够使用同名參数接收
  4. * 假设User类中也有,而我们又有同名參数。两个都能够接收
  5. */
  6. @RequestMapping("create")
  7. public ModelAndView createUser(User user,String username,String other){
  8. System.out.println(user.getUsername()+"-"+user.getPassword()+"-"+username+"-"+other);
  9. ModelAndView view = new ModelAndView();
  10. view.setViewName("/success");
  11. return view;
  12. }

2、获取Servlet API

Controller中的处理方法:

  1. @RequestMapping("create")
  2. public ModelAndView createUser(HttpServletRequest request,HttpServletResponse response,HttpSession session){
  3. String username = request.getParameter("username");
  4. System.out.println(username);
  5. request.setAttribute("requestName", "aaaaaaaaaaaa");
  6. session.setAttribute("sessionName", "bbbbbbbbbbbb");
  7. Cookie cookie = new Cookie("cookieName", "ccccccccccc");
  8. response.addCookie(cookie);
  9. ModelAndView view = new ModelAndView();
  10. view.setViewName("/success");
  11. return view;
  12. }

Success.jsp页面显示:

  1. <body>
  2. ${requestName}<br/>
  3. ${sessionName}<br/>
  4. ${cookie.cookieName.value}<br/>
  5. </body>

Spring MVC的简单使用方法的更多相关文章

  1. 用Spring MVC开发简单的Web应用程序

    1 工具与环境 借助Eclipse4.3 + Maven3.0.3构建Java Web应用程序.使用Maven内置的servlet 容器jetty,不需手工集成Web服务器到Eclipse.还帮我们自 ...

  2. Spring MVC之简单入门

    一.Spring MVC简介: 1.什么是MVC 模型-视图-控制器(MVC)是一个众所周知的以设计界面应用程序为基础的设计模式.它主要通过分离模型(Model).视图(View)及控制器(Contr ...

  3. 用Spring MVC开发简单的Web应用

    这个例子是来自于Gary Mak等人写的Spring攻略(第二版)第八章Spring @MVC中的一个例子,在此以学习为目的进行记录. 问题:想用Spring MVC开发一个简单的Web应用, 学习这 ...

  4. 基于注解的Spring MVC的简单入门——简略版

    网上关于此教程各种版本,太多太多了,因为我之前没搭过框架,最近带着两个实习生,为了帮他们搭框架,我只好...惭愧啊...基本原理的话各位自己了解下,表示我自己从来没研究过Spring的源码,所以工作了 ...

  5. spring boot 1.x完整学习指南(含各种常见问题servlet、web.xml、maven打包,spring mvc差别及解决方法)

    spring boot 入门 关于版本的选择,spring boot 2.0开始依赖于 Spring Framework 5.1.0,而spring 5.x和之前的版本差距比较大,而且应该来说还没有广 ...

  6. spring MVC controller中的方法跳转到另外controller中的某个method的方法

    1. 需求背景     需求:spring MVC框架controller间跳转,需重定向.有几种情况:不带参数跳转,带参数拼接url形式跳转,带参数不拼接参数跳转,页面也能显示. 本来以为挺简单的一 ...

  7. 为何Spring MVC可获取到方法参数名,而MyBatis却不行?【享学Spring MVC】

    每篇一句 胡适:多谈些问题,少聊些主义 前言 Spring MVC和MyBatis作为当下最为流行的两个框架,大家平时开发中都在用.如果你往深了一步去思考,你应该会有这样的疑问: 在使用Spring ...

  8. Spring MVC集成Tiles使用方法

    首先,我们定义一个总体的tiles视图 /tiles/mainTemplate.jsp首先使用:<tiles:getAsString name="title"/>打印t ...

  9. Spring MVC 参数的绑定方法

    在Spring MVC中,常见的应用场景就是给请求的Url绑定参数.本篇就介绍两种最最基本的绑定参数的方式: 基于@RequestParam 这种方法一般用于在URL后使用?添加参数,比如: @Req ...

随机推荐

  1. leetcode_1011. Capacity To Ship Packages Within D Days_binary search二分

    https://leetcode.com/problems/capacity-to-ship-packages-within-d-days/ 传送带每天有最大传送量V,对于n个货物[w1,w2,w3. ...

  2. new Buffer 生成二进制数据

    node编辑环境下: > new Buffer("admin")<Buffer 61 64 6d 69 6e> 通过post请求,服务端接收到是流数据,必须把流数 ...

  3. 201621123082《Java程序设计》第1周学习总结

    1. 本周学习总结: 关键词: 了解Java语言的发展历史.了解Java语言的特点.JDK.JRE.JVM.eclipse等. 联系: JDK是提供给Java开发人员使用的一组工具,JDK包含JVM及 ...

  4. MongoDB中mapReduce的使用

    MongoDB中mapReduce的使用 制作人:全心全意 mapReduce的功能和group by的功能类似,但比group by处理的数据量更大 使用示例: var map = function ...

  5. buf.equals()

    buf.equals(otherBuffer) otherBuffer {Buffer} 返回:{Boolean} 返回一个 boolean 标识,无论 this 和 otherBuffer 是否具有 ...

  6. Oracle 实现查询不区分大小写(设置数据库)

    转http://blog.csdn.net/shl7765856/article/details/7622756 查询数据的时候. SQL Server 默认 不区分大小写. 如果要区分,就要额外的设 ...

  7. The Bells are Ringing(枚举)

    Description Perhaps you all have heard the mythical story about Tower of Hanoi (The details of this ...

  8. Android Studio 使用图片

    首先将图片放在drawable下 然后: <ImageView android:layout_width="wrap_content" android:layout_heig ...

  9. 2017ccpc 杭州Master of Sequence

    Problem K. Master of SequenceTherearetwosequencesa1,a2,··· ,an, b1,b2,··· ,bn. LetS(t) =∑n i=1⌊t−bi ...

  10. HDU-1083Courses,二分图模板题!

    Courses                                                                                             ...