1.Spring接收请求参数

1>.使用HttpServletRequest获取

@RequestMapping("/login.do")
public String login(HttpServletRequest request){
String name = request.getParameter("name")
String pass = request.getParameter("pass")
}

2>.Spring会自动将表单参数注入到方法参数,和表单的name属性保持一致。

@RequestMapping("/login.do")
public String login(HttpServletRequest request, String name, @RequestParam("pass")String password) // 表单属性是pass,用变量password接收
{
syso(name);
syso(password)
}

3>.自动注入Bean属性

html代码

<form action="login.do">
用户名:<input name="name"/>
密码:<input name="pass"/>
<input type="submit" value="登陆">
</form>

封装的User类

public class User{
private String name;
private String pass;
}

控制器

@RequestMapping("/login.do")
public String login(User user)
{
syso(user.getName());
syso(user.getPass());
}

2.向页面传值

当Controller组件处理后,向页面传值,
1,使用HttpServletRequest 和 Session 然后setAttribute(),就和Servlet中一样
2,使用ModelAndView对象
3,使用ModelMap对象
4,使用@ModelAttribute注解

Model数据会利用HttpServletRequest的Attribute传值到success.jsp中

@RequestMapping("/login.do")
public ModelAndView login(String name,String pass){
User user = userService.login(name,pwd);
Map<String,Object> data = new HashMap<String,Object>();
data.put("user",user);
return new ModelAndView("success",data);
}

使用ModelMap参数对象示例:
ModelMap数据会利用HttpServletRequest的Attribute传值到success.jsp中

@RequestMapping("/login.do")
public String login(String name,String pass ,ModelMap model){
User user = userService.login(name,pwd);
model.addAttribute("user",user);
model.put("name",name);
return "success";
}

使用@ModelAttribute示例
在Controller方法的参数部分或Bean属性方法上使用
@ModelAttribute数据会利用HttpServletRequest的Attribute传值到success.jsp中

@RequestMapping("/login.do")
public String login(@ModelAttribute("user") User user){
//TODO
return "success";
} @ModelAttribute("name")
public String getName(){
return name;
}

Session存储:
可以利用HttpServletReequest的getSession()方法

@RequestMapping("/login.do")
public String login(String name,String pwd, ModelMap model, HttpServletRequest request){
User user = serService.login(name,pwd);
HttpSession session = request.getSession();
session.setAttribute("user",user);
model.addAttribute("user",user);
return "success";
}

Spring MVC 默认采用的是转发来定位视图,如果要使用重定向,可以如下操作
1,使用RedirectView
2,使用redirect:前缀

public ModelAndView login(){
RedirectView view = new RedirectView("regirst.do");
return new ModelAndView(view);
}

或者用如下方法,工作中常用的方法

public String login(){
//TODO
return "redirect:regirst.do";
}

SpringMVC之接收请求参数和页面传参的更多相关文章

  1. SpringMVC——接收请求参数和页面传参

    Spring接收请求参数: 1.使用HttpServletRequest获取 @RequestMapping("/login.do") public String login(Ht ...

  2. SpringMVC接收请求参数和页面传参

    接收请求参数: 1,使用HttpServletRequest获取 @RequestMapping("/login.do") public String login(HttpServ ...

  3. springMVC中接收请求参数&&数据转发

    ### 1. 接收请求参数 #### 1.1. [不推荐] 通过HttpServletRequest获取请求参数 假设存在: <form action="handle_login.do ...

  4. Springmvc之接受请求参数二

    Springmvc之接受请求参数 准备工作 新建一个表单提交 请求地址: http://localhost:8080/ProjectName/user/login.do <form action ...

  5. Struts系列笔记(6)---action接收请求参数

    action接收请求参数 在web开发中,去接收请求参数来获得表单信息非常的常见,自己也总结整理了有关Struts2通过action接收请求参数的几种方法. Struts2 提供三种数据封装的方式: ...

  6. struts2 action接收请求参数和类型转换

    1,action接收请求参数 在struts2中action是什么?(struts2是一个mvc框架)         V:jsp        M:action         C:action  ...

  7. SpringMVC中post请求参数注解@requestBody使用问题

    一.httpClient发送Post 原文https://www.cnblogs.com/Vdiao/p/5339487.html public static String httpPostWithJ ...

  8. Struts框架(6)---action接收请求参数

    action接收请求参数 在web开发中,去接收请求参数来获得表单信息非常的常见,自己也总结整理了有关Struts2通过action接收请求参数的几种方法. Struts2 提供三种数据封装的方式: ...

  9. action接收请求参数

    一.采用基本类型接收请求参数(get/post)在Action类中定义与请求参数同名的属性,struts2便能接收自动接收请求参数并赋给同名属性. action的代码: public class Pa ...

随机推荐

  1. jdk学习之如何调试jdk

    自从sun被oracle收购后,在oracle下载的jdk使用F5进入调试jdk的方法就不行了,这对于想看jdk的源码的小伙伴是一个暴击(oracle在编译rt.jar时去除了调试信息): 这不得不鼻 ...

  2. JavaScript之调试工具之断言assert

    1.单点断言 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <ti ...

  3. POJ 2503 Babelfish (STL)

    题目链接 Description You have just moved from Waterloo to a big city. The people here speak an incompreh ...

  4. 5、利用两个栈实现队列,完成push和pop操作

    题目描述 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 思路: 1.一个栈用来做push 2.另一个栈用来做pop 3.将push操作的栈的元素放入另一个栈中, ...

  5. 基于神经网络的颜色恒常性—Fully Convolutional Color Constancy with Confidence-weighted Pooling

    论文地址: http://openaccess.thecvf.com/content_cvpr_2017/papers/Hu_FC4_Fully_Convolutional_CVPR_2017_pap ...

  6. Potential Pythonic Pitfalls

    Potential Pythonic Pitfalls Monday, 11 May 2015 Table of Contents Not Knowing the Python Version Obs ...

  7. 再谈:自定义结构体的对齐问题之__attribute__ ((packed))方法【转】

    转自:https://blog.csdn.net/ipromiseu/article/details/5955295 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.c ...

  8. Android9.0新特性曝光,你准备好了吗

    Android9.0最早出现在2018年1月25日的谷歌官网上,初步代号已经确定为“Pistachio Ice Cream”(开心果冰淇淋),不过按照Google的惯例,如此长的三个单词代号,通常都只 ...

  9. webpack中require和import的区别

    commonjs同步语法 经典的commonjs同步语法如下: var a = require('./a'); a.show(); 此时webpack会将a.js打包进引用它的文件中.这是最普遍的情形 ...

  10. 【docker】资料

    https://yeasy.gitbooks.io/docker_practice/content/network/linking.html