7.SpringMVC的返回值类型和参数传递

1、SpringMVC的返回值类型

(1)ModelAndView返回值类型:

  1.1当返回为null时,页面不跳转。

  1.2当返回值没有指定视图名时,默认使用请求名作为视图名进行跳转。

  1.3当返回值指定了视图名,程序会按照视图名跳转。

/*添加*/
@RequestMapping("/getSale")
public ModelAndView addSale(Sale sale,HttpServletRequest request,ModelAndView mv){
if (sale!=null) {
Double totalPrice = sale.getPrice() * sale.getQuantity();
sale.setTotalPrice(totalPrice);
sale.setSaleDate(new Date());
Users user = (Users) request.getSession().getAttribute("user");
sale.setUserId(user.getUid());
int i = userService.addSale(sale);
if (i > 0) {
mv.setViewName("saleList");
} else {
mv.setViewName("prodectAdd");
}
}
return mv;
}

(2)Object返回值类型

/*绑定下拉框*/
@RequestMapping("/prodectName")
@ResponseBody
public Object getprodectName(){
List<Product> products = userService.getproductName();
return products;
}

(3)String返回值类型:

3.1如果返回值为null,那么以请求名作为视图名进行跳转;

3.2如果指定返回值,那么按照指定返回值作为视图名进行跳转,可以通过model,modeMap携带数据。

3.3如果返回值带有forward或者redirect前缀,那么将会进行相应的请求或重定向,不过不能通过mvc的数据模型携带数据,可以通过ServletApi携带数据。

@RequestMapping("/welcome")
public String welcome(String userName, Model model){
//将用户名保存到对应的作用域中
model.addAttribute("userName",userName);
return "welcome";
}

2.参数传递

(1)JSP页面注意点*:控制器Controller中的方法参数名称必须和表单元素的name属性值保持一致

<form class="loginForm" action="/user/getUser" method="post" >
<div class="inputbox" style="text-align:center; ">
<label for="user">用户名:</label>
<input id="user" type="text" name="userName" placeholder="请输入用户名" />
</div>
<div class="password" style="text-align:center; " >
<label for="mima">密码:</label>
<input id="mima" type="password" name="password" placeholder="请输入密码" />
</div>
<div class="subBtn" style="text-align:center; ">
<input type="submit" value="登录" />
<input type="reset" value="重置"/>
</div>
</form>
/*登录*/
@RequestMapping("/getUser")
@ResponseBody
private ModelAndView getUser(String userName, String password, ModelAndView mv, HttpServletRequest request, HttpServletResponse response, HttpSession session){
Users user = userService.getUser(userName,password);
System.out.println("user======"+user);
if (user!=null){
System.out.println("成功");
//登录成功
request.getSession().setAttribute("user",user);
//转发
mv.setViewName("index");
}else{
//登录失败
mv.setViewName("login");
}
return mv;
}

(2)请求参数装配为POJO对象

新增Person

public class Person {
private String username;
private int age;
//省略get/set方法
}

控制器

//当实体类中的属性名和表单元素的name属性相同时,即可完成自动装配
@RequestMapping(value = "personObject",method = RequestMethod.POST)
public String personObject(Person person){
System.out.println(person);
return "hello";
}

(3)@RequestParam注解

@RequestParam的作用是,当表单元素与控制器方法的参数不匹配的情况下,使用@RequestParam注解声明参数名称。

@RequestParam 有三个属性:

  3.1value:请求参数名(必须配置)

  3.2required:是否必需,默认为 true,即 请求中必须包含该参数,如果没有包含,将会抛出异常(可选配置)

  3.3defaultValue:默认值,如果设置了该值,required 将自动设为 false,无论你是否配置了required,配置了什么值,都是 false(可选配置)

 jsp页面

<form class="loginForm" action="/getUser" method="post" onsubmit="return check()" >
<div class="inputbox" style="text-align:center; ">
<label for="user">用户名:</label>
<input id="user" type="text" name="userName" placeholder="请输入用户名" />
</div>
<div class="password" style="text-align:center; " >
<label for="mima">密码:</label>
<input id="mima" type="password" name="password" placeholder="请输入密码" />
</div>
<div class="subBtn" style="text-align:center; ">
<input type="submit" value="登录" />
<input type="reset" value="重置"/>
</div>
</form>

控制器

<form class="loginForm" action="/getUser" method="post" onsubmit="return check()" >
<div class="inputbox" style="text-align:center; ">
<label for="user">用户名:</label>
<input id="user" type="text" name="userName" placeholder="请输入用户名" />
</div>
<div class="password" style="text-align:center; " >
<label for="mima">密码:</label>
<input id="mima" type="password" name="password" placeholder="请输入密码" />
</div>
<div class="subBtn" style="text-align:center; ">
<input type="submit" value="登录" />
<input type="reset" value="重置"/>
</div>
</form>

(4)RESTFUL风格的参数传递

/*{id}表示占位符*/
@RequestMapping("/getid/{id}")
@ResponseBody
public Object getid(@PathVariable("id") Integer id){
List<Accounts> name = accountService.getName(id);
return name;
}

(5)对象传递参数

IUserInfo实体类:

package com.cmy.entity;
import java.util.ArrayList;
import java.util.List;
public class IUserInfo {
private int uid;
private String username;
//域属性注入
private Teacher teacher;
private List<Teacher> teacherList;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
public List<Teacher> getTeacherList() {
return teacherList;
} public void setTeacherList(List<Teacher> teacherList) {
this.teacherList = teacherList;
}
@Override
public String toString() {
return "IUserInfo{" +
"uid=" + uid +
", username='" + username + '\'' +
", teacher=" + teacher +
", teacherList=" + teacherList +
'}';
}
}

控制层

@Controller
@RequestMapping("/fout")
public class FoutController {//属性
@RequestMapping("/getUser")
public String getUser(IUserInfo userInfo){
System.out.println(userInfo.toString());
return "welcome";
}
}

页面

域属性1

<form class="loginForm" action="/fout/getUser" method="post" onsubmit="return check()" >
<div class="inputbox" style="text-align:center; ">
<label for="user">用户名:</label>
<input id="user" type="text" name="teacher.teachername" placeholder="请输入用户名" />
<input id="users" type="text" name="teacher.teachername" placeholder="请输入用户名" />
</div>
<div class="password" style="text-align:center; " >
<label for="mima">密码:</label>
<input id="mima" type="password" name="password" placeholder="请输入密码" />
</div>
<div class="subBtn" style="text-align:center; ">
<input type="submit" value="登录" />
<input type="reset" value="重置"/>
</div>
</form>

集合2

<form class="loginForm" action="/fout/getUser" method="post" onsubmit="return check()" >
<div class="inputbox" style="text-align:center; ">
<label for="user">用户名:</label>
<input id="user" type="text" name="teacherList[0].teachername" placeholder="请输入用户名" />
<input id="users" type="text" name="teacherList[1].teachername" placeholder="请输入用户名" />
</div>
<div class="password" style="text-align:center; " >
<label for="mima">密码:</label>
<input id="mima" type="password" name="password" placeholder="请输入密码" /></div>
<div class="subBtn" style="text-align:center; ">
<input type="submit" value="登录" />
<input type="reset" value="重置"/>
</div>
</form>

SpringMVC返回类型的更多相关文章

  1. springMVC 返回类型选择 以及 SpringMVC中model,modelMap.request,session取值顺序

    springMVC 返回类型选择 以及 SpringMVC中model,modelMap.request,session取值顺序 http://www.360doc.com/content/14/03 ...

  2. spring学习之springMVC 返回类型选择 以及 SpringMVC中model,modelMap.request,session取值顺序

    spring mvc处理方法支持如下的返回方式:ModelAndView, Model, ModelMap, Map,View, String, void.下面将对具体的一一进行说明: ModelAn ...

  3. springMVC中controller的几种返回类型

    ==网文1,还不错,感觉比较老旧springMVC中controller的几种返回类型 - CSDN博客http://blog.csdn.net/qq_16071145/article/details ...

  4. SpringMVC返回Json,自定义Json中Date类型格式

    http://www.cnblogs.com/jsczljh/p/3654636.html —————————————————————————————————————————————————————— ...

  5. 05 SpringMVC:02.参数绑定及自定义类型转换&&04.SpringMVC返回值类型及响应数据类型&&05.文件上传&&06.异常处理及拦截器

    springMVC共三天 第一天: 01.SpringMVC概述及入门案例 02.参数绑定及自定义类型转换 03.SpringMVC常用注解 第二天: 04.SpringMVC返回值类型及响应数据类型 ...

  6. SpringMVC Controller的返回类型

    Controller的三种返回类型中 ModelAndView类型 带数据带跳转页面 String 跳转页面不带数据 void 通常是ajax格式请求时使用 1返回ModelAndView contr ...

  7. SpringMVC 返回 html 视图页面,SpringMVC与Servlet,Servlet重定向与转发

    1. SpringMVC与Servlet的关系 SpringMVC框架是建立在Servlet之上的,提供各种功能,各种封装,各种方便的同时,它一点儿也没有限制Servlet,我们完全可以在Spring ...

  8. Spring MVC控制层的返回类型--String类型与Bean类型

    SpringMVC控制层的返回类型形式多样,现拿其中的两种--String类型与Bean类型作以说明. 一.测试项目的结构 说明:(jsp的名字没起好) 控制层:UserController.java ...

  9. springmvc 返回xml

    需求: 1.springmvc返回xml: 技术及环境: Spring 4.3.1.RELEASE JDK 1.8 IDEA 15.0.6 Maven 3 实现: spirngxml的配置主要如下: ...

随机推荐

  1. Python14之字符串(各种奇葩的内置方法)

    一.字符串的分片操作 其分片操作和列表和元组一样 str1 = 'keshengtao' str1[2:6] 'shen' str1[:] 'keshengtao' str1[:4] 'kesh' 二 ...

  2. 小程序--e.target和e.currentTarget区别

    事件捕获与事件冒泡 事件捕获是从外到内,事件冒泡是从内到外. 注意:不管是不是冒泡事件,都不会改变事件传递的参数值,都还是在dataset中获取(******) target:指事件源组件对象    ...

  3. AVR单片机教程——开发环境配置

    今天去交大密院参观了设计展,无外乎两个主题:Arduino.Python. 关于Python,我印象最深的是一位Python程序员的话:你要硬核的话,可以去那边看Java. 拜托,都9102年了,Ja ...

  4. vim常用命令的使用

    中文博客:https://www.cnblogs.com/lijia0511/p/5644566.html 英文原文:http://yannesposito.com/Scratch/en/blog/L ...

  5. vue项目过程的理解: main.js文件理解 router.js文件理解 以及组件 路由 等之间的关系

    https://blog.csdn.net/qq_26229005/article/details/85040393 内容太多了,有空再整理

  6. 2019牛客多校八 H. How Many Schemes (AC自动机,树链剖分)

    大意: 给定树, 每条边有一个字符集合, 给定$m$个模式串, $q$个询问$(u,v)$, 对于路径$(u,v)$中的所有边, 每条边从对应字符集合中取一个字符, 得到一个串$s$, 求$s$至少包 ...

  7. 解决batik使用JScrollPane显示svg图滚动条不显示的问题

    // 必须使用batik提供的JSVGScrollPane,使用swing自己的组件JScrollPane初始化时滚动条不会显示. JSVGScrollPane svgJScrollPane = ne ...

  8. Consul作为SpringCloud配置中心

    一.背景介绍 在分布式系统中动态配置中,可以避免重复重启服务,动态更改服务参数等.一句话非常重要. 另外一篇文章也是这样说的,哈哈. Consul 作为Spring 推荐的分布式调度系统其也具备配置中 ...

  9. robot framework 的关键字Continue For Loop 用法

    Continue For Loop关键字就是python的continue的意思,跳出本层循环,继续执行下一个循环. 我先举个栗子: :FOR    ${index}    IN RANGE    5 ...

  10. MySQL性能测试调优

    MySQL性能测试调优 操作系统 基本操作 查看磁盘分区mount选项 $ mount 永久修改分区mount选项(系统重启后生效) 修改文件 /etc/fstab 中对应分区的mount optio ...