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 ...
随机推荐
- 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. ...
- new Buffer 生成二进制数据
node编辑环境下: > new Buffer("admin")<Buffer 61 64 6d 69 6e> 通过post请求,服务端接收到是流数据,必须把流数 ...
- 201621123082《Java程序设计》第1周学习总结
1. 本周学习总结: 关键词: 了解Java语言的发展历史.了解Java语言的特点.JDK.JRE.JVM.eclipse等. 联系: JDK是提供给Java开发人员使用的一组工具,JDK包含JVM及 ...
- MongoDB中mapReduce的使用
MongoDB中mapReduce的使用 制作人:全心全意 mapReduce的功能和group by的功能类似,但比group by处理的数据量更大 使用示例: var map = function ...
- buf.equals()
buf.equals(otherBuffer) otherBuffer {Buffer} 返回:{Boolean} 返回一个 boolean 标识,无论 this 和 otherBuffer 是否具有 ...
- Oracle 实现查询不区分大小写(设置数据库)
转http://blog.csdn.net/shl7765856/article/details/7622756 查询数据的时候. SQL Server 默认 不区分大小写. 如果要区分,就要额外的设 ...
- The Bells are Ringing(枚举)
Description Perhaps you all have heard the mythical story about Tower of Hanoi (The details of this ...
- Android Studio 使用图片
首先将图片放在drawable下 然后: <ImageView android:layout_width="wrap_content" android:layout_heig ...
- 2017ccpc 杭州Master of Sequence
Problem K. Master of SequenceTherearetwosequencesa1,a2,··· ,an, b1,b2,··· ,bn. LetS(t) =∑n i=1⌊t−bi ...
- HDU-1083Courses,二分图模板题!
Courses ...