Spring MVC 之请求处理方法可接收参数(三)
请求处理方法可接收参数
今天学习了前三个方法。
1、作用域对象
2、单个表单提交数据
3、表单数据封装的Bean对象
首先创建一个实体对象。
package com.cy.springannotation.entity;
/**
* 定义一个表单实体类
* @author acer
*
*/
public class UserBean {
//要求属性名必须要和表单的参数名一样的!
private String username;
private String password;
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;
} }
简单的一个jsp页面!login.jsp
为了方便观察 password的type为text。
<%@ page language="java" import="java.util.*" pageEncoding="utf-8" contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>登录页面</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
<form action="login.do" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="username"/></td>
</tr>
<tr>
<td>密码</td>
<td><input type="text" name="password"/></td>
</tr>
<tr>
<td colspan="2"> <input type="submit" value="提交"/> </td>
</tr>
</table>
</form>
</body>
</html>
LoginController.java
package com.cy.springannotation.controller; import javax.servlet.http.HttpServletRequest; import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView; import com.cy.springannotation.entity.UserBean; @Controller // @Controller告知Spring容器这是一个控制器组件
public class LoginController { private Logger log=Logger.getLogger(this.getClass()); /* @RequestMapping("/login.do") // @RequestMapping告知该方法是针对/login.do请求的处理方法
public String login(String username){
System.out.println(username);
return "index"; // 返回的字符串被当做ViewName }*/ /**
*
* 1 、作用域对象
* HttpServletRequest,HttpServletResponse,HttpSession
* 个数顺序可以自行定义
* @param request
* @return
*/ /*@RequestMapping("/login.do")
public ModelAndView login(HttpServletRequest request){
String username=request.getParameter("username");
String password=request.getParameter("password");
log.info(username);
log.info(password);
ModelAndView mav=new ModelAndView();
mav.setViewName("index");
return mav; }*/ /**
* 2、单个表单提交数据
*/ /*@RequestMapping("/login.do")
public String login(@RequestParam("username")String name,@RequestParam("password")String pwd){
log.info(name);
log.info(pwd);
return "index";
}*/ /**method主要是制定请求方法的规则,比如:如果设置了RequestMethod.POST,
* 那么你的表单提交就必须使用POST提交,否则将报405错误
params="password" 表示我的表单提交中,一定要有password这个参数,否则将报400的错误*/ /**
* 2、单个表单提交数据
*/
/*@RequestMapping(value="/login.do",method=RequestMethod.POST,params="password")
//如果属性名与提交项名称相同,可以不配置@RequestParam
public ModelAndView login(String username,String password){
log.info(username);
log.info(password);
ModelAndView mv = new ModelAndView();
mv.setViewName("index");
return mv;
}*/ /**
* 3 表单数据封装的Bean对象
* @param user
* @return
*/
@RequestMapping(value="/login.do")
public String login(UserBean user){
log.info(user.getUsername());
log.info(user.getPassword());
return "index";
} }
其他的配置都和前一篇是相同的。

4、Map对象
5、PrintWriter作为参数
6、Cookie中的数据作为参数
7、Http协议头的数据作为参数
8、从restful风格请求从获取数据
Spring MVC 之请求处理方法可接收参数(三)的更多相关文章
- Spring MVC如何获取请求中的参数
目录 一.获取URL中路径参数 1.1 @PathVariable 注解 1.2 @PathParam 注解 二.获取请求参数: 2.1 GET请求 2.1.1 获取请求中的单个参数:@Request ...
- spring mvc 3.1的自动注入参数遇到的问题
在网上下载了xheditor作为页面的编辑器,编辑内容后post到后台保存,后台方法用spring mvc的自动注入的方式接收参数. 这种方式在各个浏览器下运行良好,但是在ie11下发现,从word. ...
- spring mvc从前台往后台传递参数的三种方式
jsp页面: 第一种:使用控制器方法形参的方式(常用) 第二种:使用模型传参的方式(如果前台往后台传递的参数非常多,如果还使用形参的方式传递,非常复杂.我们可以使用模型传参的方式,把多 个请求的参数 ...
- Spring MVC无法获取ajax POST的参数和值
一.怎么会这个样子 很简单的一个想法,ajax以POST的方式提交一个表单,Spring MVC解析.然而一次次的打印null折磨了我整整一天…… 最后的解决现在看来是很明显的问题,“只是当时已惘然” ...
- Spring MVC 页面跳转时传递参数
页面仍然使用 JSP,在跳转时如果想传递参数则需要用到类 RedirectAttributes. 首先看看如何打开一个普通页面: // 登录页面(每个页面都要独立的 Action 来支持其呈现) @R ...
- Spring MVC Restful Put方法无法获取参数值
Spring MVC Restful 无法通过@ReqeustParam获取参数值 原因是Tomcat只支持POST/GET获取参数值,对于PUT这些方法需要通过HttpPutFormContentF ...
- Spring MVC 确定目标方法POJO 类型参数
1:确定一个Key 2. 在implicitMode 中存在Key 对应的对象, 若存在则作为参数传入 3. 在implicitMode 中不存在Key 对应的对象, 则检查当前@SessionAtr ...
- Spring MVC(十三)--保存并获取属性参数
这里的属性参数主要是指通过request.session.cookie等设置的属性,有时候我们需要将一些请求的参数保存到HTTP的request或者session对象中去,在控制器中也会进行设置和获取 ...
- Spring MVC(八)--控制器接受简单列表参数
有些场景下需要向后台传递一个数组,比如批量删除传多个ID的情况,可以使用数组传递,数组中的ID元素为简单类型,即基本类型. 现在我的测试场景是:要从数据库中查询minId<id<maxId ...
随机推荐
- Pearls
Pearls Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7980 Accepted: 3966 Description In ...
- Bag Problem
Bag Problem Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/131072 K (Java/Others) Total ...
- Help Me with the Game 分类: POJ 2015-06-29 16:34 17人阅读 评论(0) 收藏
Help Me with the Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3706 Accepted: ...
- Sqlite查询时间段内的数据问题解决!
最近搞Sqlite本地查询,需求为查询某时间段内的数据,在SQL中我们都知道为: select * from tblName where rDate Between '2008-6-10' and ...
- 驱动之module_init/module_exit
在前面helloworld的编写里面,我们使用了两个宏分别是module_init和module_exit,这里分析下为什么使用这两个宏. 在写模块的时候有两个特殊的函数,分别是init_module ...
- C++中的const详解
const的用法,特别是用在函数后面 在普通的非 const成员函数中,this的类型是一个指向类类型的 const指针.可以改变this所指向的值,但不能改变 this所保存的地址. 在 const ...
- c++学习之:根据GetLastError()返回值获取错误信息
VC中GetLastError()获取错误信息的使用在VC中编写应用程序时,经常需要涉及到错误处理问题.许多函数调用只用TRUE和FALSE来表明函数的运行结果.一旦出现错误,MSDN中往往会指出请用 ...
- vi及缩进设置
set autoindent,把当前行的对起格式应用到下一行: set smartindent,智能的选择对起方式: set tabstop=4,设置tab键为4个空格: set shiftwidth ...
- spring+hibernate 实体类注解问题
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.Ann ...
- [HDOJ3709]Balanced Number(数位dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3709 题意:求区间[L,R]内每一个数中是否存在一位,使得左边的各位数*距离=右边的各位数*距离(自己 ...