Spring MVC 之请求处理方法可接收参数(三)

时间:2021-10-13 21:20:58

请求处理方法可接收参数

今天学习了前三个方法。

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";
} }

其他的配置都和前一篇是相同的。

Spring MVC 之请求处理方法可接收参数(三)

4、Map对象
5、PrintWriter作为参数
6、Cookie中的数据作为参数
7、Http协议头的数据作为参数
8、从restful风格请求从获取数据