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 ...
随机推荐
- sql查询某条记录
select * from (SELECT t.*,ROWNUM AS RN FROM AWARDISSUE_FOOTBALL t ORDER BY ID DESC) WHERE RN=2
- Linux高级变量
http://blog.chinaunix.net/uid-27040051-id-3450991.html 高级变量 基本形式 [1].变量扩展 格式 ${变量名称} [2].命令扩展 格式 $(命 ...
- HDU(4528),BFS,2013腾讯编程马拉松初赛第五场(3月25日)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4528 小明系列故事——捉迷藏 Time Limit: 500/200 MS (Java/O ...
- Educational Codeforces Round 1 A
In this problem you are to calculate the sum of all integers from 1 to n, but you should take all po ...
- MVCAction接受与返回
//Action方法接受有:如下四种: //Test(int id) 接受url路由中配置的同名参数 //通过request.Form/request.querystring 接受(get) //Te ...
- 动态创建Layout
public Form1() { InitializeComponent(); AddLayoutItems1(); //初始化 } /// <summary> /// 1.一个方法(定义 ...
- javascript实现二分查找
今天做了道笔试题,要求是实现二分查找,当然不难,想了一下,因为没有要求语言就用javascript实现了.当然,期间还是出来了一点问题. ok,上代码 /* * 稳定二分查找 * 作者:吴伟欣 * * ...
- MySQL中别名的使用
MySQL中别名的使用 为数据列或者表达式起别名时,别名紧跟数据列,中间以空格隔开,或者用关键字as隔开. #为表达式起别名 使用as关键字隔开 as T_ID from teacher_table; ...
- C#打开指定路径文件对话框
private string OpenFileDlog(string DeafultDir) { OpenFileDialog Ofd = new OpenFileDialog(); Ofd.AddE ...
- 关于跨域的ajax
$.getJSON("http://query.yahooapis.com/v1/public/yql?callback=?", { q: "select * from ...