请求处理方法可接收参数

今天学习了前三个方法。

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 之请求处理方法可接收参数(三)的更多相关文章

  1. Spring MVC如何获取请求中的参数

    目录 一.获取URL中路径参数 1.1 @PathVariable 注解 1.2 @PathParam 注解 二.获取请求参数: 2.1 GET请求 2.1.1 获取请求中的单个参数:@Request ...

  2. spring mvc 3.1的自动注入参数遇到的问题

    在网上下载了xheditor作为页面的编辑器,编辑内容后post到后台保存,后台方法用spring mvc的自动注入的方式接收参数. 这种方式在各个浏览器下运行良好,但是在ie11下发现,从word. ...

  3. spring mvc从前台往后台传递参数的三种方式

     jsp页面: 第一种:使用控制器方法形参的方式(常用) 第二种:使用模型传参的方式(如果前台往后台传递的参数非常多,如果还使用形参的方式传递,非常复杂.我们可以使用模型传参的方式,把多 个请求的参数 ...

  4. Spring MVC无法获取ajax POST的参数和值

    一.怎么会这个样子 很简单的一个想法,ajax以POST的方式提交一个表单,Spring MVC解析.然而一次次的打印null折磨了我整整一天…… 最后的解决现在看来是很明显的问题,“只是当时已惘然” ...

  5. Spring MVC 页面跳转时传递参数

    页面仍然使用 JSP,在跳转时如果想传递参数则需要用到类 RedirectAttributes. 首先看看如何打开一个普通页面: // 登录页面(每个页面都要独立的 Action 来支持其呈现) @R ...

  6. Spring MVC Restful Put方法无法获取参数值

    Spring MVC Restful 无法通过@ReqeustParam获取参数值 原因是Tomcat只支持POST/GET获取参数值,对于PUT这些方法需要通过HttpPutFormContentF ...

  7. Spring MVC 确定目标方法POJO 类型参数

    1:确定一个Key 2. 在implicitMode 中存在Key 对应的对象, 若存在则作为参数传入 3. 在implicitMode 中不存在Key 对应的对象, 则检查当前@SessionAtr ...

  8. Spring MVC(十三)--保存并获取属性参数

    这里的属性参数主要是指通过request.session.cookie等设置的属性,有时候我们需要将一些请求的参数保存到HTTP的request或者session对象中去,在控制器中也会进行设置和获取 ...

  9. Spring MVC(八)--控制器接受简单列表参数

    有些场景下需要向后台传递一个数组,比如批量删除传多个ID的情况,可以使用数组传递,数组中的ID元素为简单类型,即基本类型. 现在我的测试场景是:要从数据库中查询minId<id<maxId ...

随机推荐

  1. ROADS

    ROADS Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11977 Accepted: 4429 Description N ...

  2. 多校赛3- Solve this interesting problem 分类: 比赛 2015-07-29 21:01 8人阅读 评论(0) 收藏

    H - Solve this interesting problem Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I ...

  3. ural 1115,搜索

    题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1115 题意:n个军舰,m个海岸线,各个长度给出,求n个军舰怎么组成这些海岸线. 思路很简 ...

  4. libevent安装及使用

    一.安装libevent 官网:http://libevent.org/ 选择最新版本下载,我选择的是libevent-2.0.22-stable.tar.gz,然后安装README文件中描述的方法编 ...

  5. VC 实现文件与应用程序关联(转载)

    转载:http://www.cnblogs.com/RascallySnake/archive/2013/03/01/2939102.html 日常工作中,doc文件直接双击后,就能启动word软件, ...

  6. Struts2的标签库(二)——OGNL表达式

    Struts2的标签库(二) --OGNL表达式 1.Struts2中的OGNL表达式增加了ValueStack的支持. 注:ValueStack--实际上是一个容器对象,该对象在启动Struts2框 ...

  7. thinkphp nginx 配置

    thinkphp convention配置:'URL_MODEL' => '2', //URL模式 nginx rewrite配置: if (!-e $request_filename) { r ...

  8. SQl格式化日期时间

    Sql Server 中一个非常强大的日期格式化函数Select CONVERT(varchar(100), GETDATE(), 0): 05 16 2006 10:57AMSelect CONVE ...

  9. An Example of On-Error Trigger in Oracle Forms

    I wrote this trigger around 4 years ago to handle errors in an application based on Oracle Forms 6i. ...

  10. [51NOD1537] 分解(递推,矩阵快速幂)

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1537 思路:一开始用二项式拆了一下,发现这个式子的形式总能变成 ...