请求处理方法可接收参数

今天学习了前三个方法。

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. oracle触发器自增字段

    1.创建序列 代码 -- Create sequence create sequence seq_userinfo minvalue 1 start with 1 increment by 1; 2. ...

  2. Unity-Animator深入系列---fullPathHash和shortNameHash

    回到 Animator深入系列总目录 Unity5对状态哈希做了改进,拆分成了fullPathHash和shortNameHash,那么就来看看他们有什么区别 测试结果: fullPathHash支持 ...

  3. Memcached缓存瓶颈分析

    Memcached缓存瓶颈分析 获取Memcached的统计信息 Shell: # echo "stats" | nc 127.0.0.1 11211 PHP: $mc = new ...

  4. JAVA基础知识之JVM-——自定义类加载器

    JVM中除了根加载器之外其他加载器都是ClassLoader的子类实例, 可以通过扩展ClassLoader的子类,通过重写方法来实现自定义的类加载器. ClassLoader中有两个关键的方法如下, ...

  5. python测试代理IP地址

    代码: # -*- coding: utf-8 -*- import urllib,urllib2,re from random import choice from scrapy.selector ...

  6. 基于PowerShell 3.0的web接口测试

    对于web接口测试,做一下总结. 接口测试总结 1. 接口url格式:http://www.xxx.com/a/bbb.html: 2. 接口url后面接的参数格式:“?参数名=参数值&参数名 ...

  7. ExecutorService - 10个技巧和窍门

    ExecutorService已经成为Java并发编程中常用的基础库,几乎所有到线程 任务等执行都要委托ExecutorService.下面是使用过程中10个技巧和窍门. 1.为线程池和线程取名 当我 ...

  8. uploadify的用法与动态传参 提供demo下载

    ---恢复内容开始--- 官网:http://www.uploadify.com/   一款不错的上传插件.官方文档http://www.uploadify.com/documentation/ 用法 ...

  9. js取当前项目名称

    function getContextPath(){ var path = window.location.href; path = path.substring(0, path.lastIndexO ...

  10. CentOS6 启动流程图文解剖

    我们在使用Linux操作系统的时候,我们只需按下电源键,等待,然后输入账户和密码就可以使用Linux操作系统了.那么在按下电源到输入账号和密码之前,操作系统都做了些什么?下面就来讲述在这段时间发生的动 ...