一、Multiaction Controller

package cn.framelife.mvc.control;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView; import cn.framelife.mvc.entity.User; @Controller
@RequestMapping("/user")
public class UserControl { /**
* 这种方法接收/user/create.abc请求
*/
@RequestMapping("create")
public ModelAndView createUser(User user){
System.out.println("createUser"); ModelAndView view = new ModelAndView();
view.setViewName("/success"); return view;
} /**
* 这种方法接收/user/update.abc请求
*/
@RequestMapping("update")
public ModelAndView update(){
System.out.println("updateUser"); ModelAndView view = new ModelAndView();
view.setViewName("/success"); return view;
}
}

二、 ModelAndView

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

1、ModelAndView中的方法

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

    addObject(Object modelObject)
addObject(String modelName, Object modelObject)
addAllObjects(Map modelMap)

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

    setViewName(String viewName)
setView(View view)

2、重定向:

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

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

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

三、Controler处理方法获取值

1、获取页面值

页面Form表单:

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

A、 绑定到同名參数中

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

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

User实体类:

public class User implements java.io.Serializable {
private Integer id;
private String username;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}

Controller中的方法:

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

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

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

2、获取Servlet API

Controller中的处理方法:

    @RequestMapping("create")
public ModelAndView createUser(HttpServletRequest request,HttpServletResponse response,HttpSession session){
String username = request.getParameter("username");
System.out.println(username); request.setAttribute("requestName", "aaaaaaaaaaaa");
session.setAttribute("sessionName", "bbbbbbbbbbbb"); Cookie cookie = new Cookie("cookieName", "ccccccccccc");
response.addCookie(cookie); ModelAndView view = new ModelAndView();
view.setViewName("/success"); return view;
}

Success.jsp页面显示:

<body>
${requestName}<br/>
${sessionName}<br/>
${cookie.cookieName.value}<br/>
</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. KMS

    slmgr -ipk 73KQT-CD9G6-K7TQG-66MRP-CQ22C

  2. C++ 多态、虚函数、虚析构函数

    1.若某种语言只支持类但不支持多态,则只能称为基于对象,不能说是面向对象. 2.多态:向不同对象发送同一个消息,不同的对象会产生不同的行为,发送消息可以是调用函数等操作.函数重载.运算符重载都是多态. ...

  3. CAD绘制固定矩形标注(网页版)

    js中实现代码说明: function DoFixRectComment() { var ent = mxOcx.DrawCustomEntity("TestMxCustomEntity&q ...

  4. ajax请求回数组数据,Vue页面数组没同步问题

    记录bug 为什么 ajax 获取到了 vm.$data.list 页面上却没有显示出来的? 代码 //页面 <tr v-for="item in list">{{ * ...

  5. python在linux下的使用

    1.查看python(解释器)的版本(什么版本的解释器支持哪一版版的语言标准) 一般在linux上已经预装了python,只要在Bash Shell中输入python,即可看到如下版本信息: 按Ctr ...

  6. 用PHP的GD库画五星红旗来玩玩

    1 header("Content-Type:image/jpeg"); $img=imagecreatetruecolor(999,667); $color=imagecolor ...

  7. SVN CommandLine

    要是SVN命令行用不了的话: 1.SVN装了没? 2.SVN安装时,选项“command line client tools”选了没. 检出: svn checkout [-depth ARG] [- ...

  8. [转]TOpenDialog

    转自:http://www.cnblogs.com/zhangzhifeng/archive/2011/08/04/2127395.html 1.TOpenDialog组件的典型用法“打开”对话框是用 ...

  9. 第二周习题F

    Starting with x and repeatedly multiplying by x, we can compute x31 with thirty multiplications: x2  ...

  10. [OJ#39]左手右手

    [OJ#39]左手右手 试题描述 有 n 个人,每个人左右手上各写着一个整数.对于编号为 a 的人和编号为 b 的人, a 对 b 的好感度等于 a 左手上写的数字乘 b 右手上写的数字,a 和 b  ...