Spring MVC的简单使用方法
一、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的简单使用方法的更多相关文章
- 用Spring MVC开发简单的Web应用程序
1 工具与环境 借助Eclipse4.3 + Maven3.0.3构建Java Web应用程序.使用Maven内置的servlet 容器jetty,不需手工集成Web服务器到Eclipse.还帮我们自 ...
- Spring MVC之简单入门
一.Spring MVC简介: 1.什么是MVC 模型-视图-控制器(MVC)是一个众所周知的以设计界面应用程序为基础的设计模式.它主要通过分离模型(Model).视图(View)及控制器(Contr ...
- 用Spring MVC开发简单的Web应用
这个例子是来自于Gary Mak等人写的Spring攻略(第二版)第八章Spring @MVC中的一个例子,在此以学习为目的进行记录. 问题:想用Spring MVC开发一个简单的Web应用, 学习这 ...
- 基于注解的Spring MVC的简单入门——简略版
网上关于此教程各种版本,太多太多了,因为我之前没搭过框架,最近带着两个实习生,为了帮他们搭框架,我只好...惭愧啊...基本原理的话各位自己了解下,表示我自己从来没研究过Spring的源码,所以工作了 ...
- spring boot 1.x完整学习指南(含各种常见问题servlet、web.xml、maven打包,spring mvc差别及解决方法)
spring boot 入门 关于版本的选择,spring boot 2.0开始依赖于 Spring Framework 5.1.0,而spring 5.x和之前的版本差距比较大,而且应该来说还没有广 ...
- spring MVC controller中的方法跳转到另外controller中的某个method的方法
1. 需求背景 需求:spring MVC框架controller间跳转,需重定向.有几种情况:不带参数跳转,带参数拼接url形式跳转,带参数不拼接参数跳转,页面也能显示. 本来以为挺简单的一 ...
- 为何Spring MVC可获取到方法参数名,而MyBatis却不行?【享学Spring MVC】
每篇一句 胡适:多谈些问题,少聊些主义 前言 Spring MVC和MyBatis作为当下最为流行的两个框架,大家平时开发中都在用.如果你往深了一步去思考,你应该会有这样的疑问: 在使用Spring ...
- Spring MVC集成Tiles使用方法
首先,我们定义一个总体的tiles视图 /tiles/mainTemplate.jsp首先使用:<tiles:getAsString name="title"/>打印t ...
- Spring MVC 参数的绑定方法
在Spring MVC中,常见的应用场景就是给请求的Url绑定参数.本篇就介绍两种最最基本的绑定参数的方式: 基于@RequestParam 这种方法一般用于在URL后使用?添加参数,比如: @Req ...
随机推荐
- 8086汇编——Introduction(8086内部寄存器,段寄存器,存储器分段)
8086汇编--Introduction 一.8086CPU的三种工作模式 实模式:只有低20位地址线起作用,仅能寻址第一个1MB的内存空间.MS DOS运行于该模式下. 保护模式:在该模式下,机器可 ...
- HTML a标签的href 属性 tel 点击可以直接拨打电话 ( 移动端 )
<a href="tel:13828172679">13622178579</a>
- C# 处理年月日提取时间
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 用闭包方式实现点击a标签弹也索引值
var self = document.getElementsByTagName("a"); for(var i=0;i<self.length;i++){ self[i]. ...
- vijos1382寻找主人
题目大意: 给出两个串(长度<=1e6),问是否同构,如果同构输出最小表示. 题解: 这是最小表示法模板题.在这里好好讲一下最小表示法. 首先有一个最暴力的方法:把所有表示搞出来排序. 时间复杂 ...
- Python列表、元组、字典、集合的内置使用方法
列表: 是一种可以存储多个值得数据容器 内容是有序的 可以存储任何数据类型 可以存储重复的元素 是可变类型(当内容发生变化时id不变) 元组: 也是一种可以存储多个值得数据容器 元组中的元素不可以被修 ...
- 零基础入门学习Python(16)--序列!序列!
前言 你可能发现了,小甲鱼把这个列表,元组,字符串放在一起讲是有道理的,它们有许多共同点: 都可以通过索引得到每一个元素 默认索引值总是从0开始 可以通过分片的方法得到一个范围内的元素的集合 有很多共 ...
- PHP:图片上传
文章来源:http://www.cnblogs.com/hello-tl/p/7593033.html <?php class TL_Update_File{ private $file = n ...
- 杭电 2124 Repair the Wall(贪心)
Description Long time ago , Kitty lived in a small village. The air was fresh and the scenery was ve ...
- PS注意点
2.颜色 设计师应该具备审美能力. 3.实验 不断的练习会让你学习到更多的东西,请不要给自己太多压力,你的付出不会仅仅只让你原地踏步,要坚持. 填充和不透明的掌握. 还有流量的使用. 填充是一 ...