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


    请求处理方法可接收参数

    今天学习了前三个方法。

    1、作用域对象
    2、单个表单提交数据
    3、表单数据封装的Bean对象

    首先创建一个实体对象。

     1 package com.cy.springannotation.entity;
     2 /**
     3  * 定义一个表单实体类
     4  * @author acer
     5  *
     6  */
     7 public class UserBean {
     8     //要求属性名必须要和表单的参数名一样的!
     9     private String username;
    10     private String password;
    11     public String getUsername() {
    12         return username;
    13     }
    14     public void setUsername(String username) {
    15         this.username = username;
    16     }
    17     public String getPassword() {
    18         return password;
    19     }
    20     public void setPassword(String password) {
    21         this.password = password;
    22     }
    23     
    24 
    25 }

    简单的一个jsp页面!login.jsp

    为了方便观察 password的type为text。

     1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8" contentType="text/html; charset=utf-8"%>
     2 <%
     3 String path = request.getContextPath();
     4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
     5 %>
     6 
     7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
     8 <html>
     9   <head>
    10     <base href="<%=basePath%>">
    11     
    12     <title>登录页面</title>
    13     
    14     <meta http-equiv="pragma" content="no-cache">
    15     <meta http-equiv="cache-control" content="no-cache">
    16     <meta http-equiv="expires" content="0">    
    17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    18     <meta http-equiv="description" content="This is my page">
    19     <!--
    20     <link rel="stylesheet" type="text/css" href="styles.css">
    21     -->
    22 
    23   </head>
    24   
    25   <body>
    26   <form action="login.do" method="post">
    27     <table>
    28        <tr>
    29            <td>用户名:</td>
    30            <td><input type="text" name="username"/></td>
    31        </tr>
    32        <tr>
    33            <td>密码</td>
    34            <td><input type="text" name="password"/></td>
    35        </tr>
    36        <tr>
    37            <td colspan="2"> <input type="submit" value="提交"/> </td>
    38        </tr>
    39     </table>
    40   </form>
    41   </body>
    42 </html>
    LoginController.java
     1 package com.cy.springannotation.controller;
     2 
     3 import javax.servlet.http.HttpServletRequest;
     4 
     5 import org.apache.log4j.Logger;
     6 import org.springframework.stereotype.Controller;
     7 import org.springframework.web.bind.annotation.RequestMapping;
     8 import org.springframework.web.bind.annotation.RequestMethod;
     9 import org.springframework.web.bind.annotation.RequestParam;
    10 import org.springframework.web.servlet.ModelAndView;
    11 
    12 import com.cy.springannotation.entity.UserBean;
    13 
    14 @Controller  // @Controller告知Spring容器这是一个控制器组件
    15 public class LoginController {
    16     
    17     private Logger log=Logger.getLogger(this.getClass());
    18     
    19     
    20     
    21     /* @RequestMapping("/login.do")  // @RequestMapping告知该方法是针对/login.do请求的处理方法
    22      public String login(String username){
    23          System.out.println(username);
    24         return "index";           // 返回的字符串被当做ViewName
    25          
    26      }*/
    27     
    28     /**
    29      * 
    30      * 1 、作用域对象
    31      * HttpServletRequest,HttpServletResponse,HttpSession
    32      * 个数顺序可以自行定义
    33      * @param request
    34      * @return
    35      */
    36 
    37     /*@RequestMapping("/login.do") 
    38     public ModelAndView login(HttpServletRequest request){
    39         String username=request.getParameter("username");
    40         String password=request.getParameter("password");
    41         log.info(username);
    42         log.info(password);
    43         ModelAndView  mav=new ModelAndView();
    44         mav.setViewName("index");
    45         return mav;
    46         
    47     }*/
    48     
    49     /**
    50      * 2、单个表单提交数据
    51      */
    52     
    53     /*@RequestMapping("/login.do")
    54     public String login(@RequestParam("username")String name,@RequestParam("password")String pwd){
    55         log.info(name);
    56         log.info(pwd);
    57         return "index";
    58     }*/
    59 
    60     
    61     
    62     
    63     
    64     /**method主要是制定请求方法的规则,比如:如果设置了RequestMethod.POST,
    65      * 那么你的表单提交就必须使用POST提交,否则将报405错误 
    66      params="password" 表示我的表单提交中,一定要有password这个参数,否则将报400的错误*/
    67     
    68     /**
    69      * 2、单个表单提交数据
    70      */
    71     /*@RequestMapping(value="/login.do",method=RequestMethod.POST,params="password")
    72     //如果属性名与提交项名称相同,可以不配置@RequestParam
    73     public ModelAndView login(String username,String password){
    74         log.info(username);
    75         log.info(password);
    76         ModelAndView mv = new ModelAndView();
    77         mv.setViewName("index");
    78         return mv;
    79     }*/
    80     
    81     
    82     /**
    83      * 3 表单数据封装的Bean对象
    84      * @param user
    85      * @return
    86      */
    87     @RequestMapping(value="/login.do")
    88     public String login(UserBean user){
    89         log.info(user.getUsername());
    90         log.info(user.getPassword());
    91         return "index";
    92     }
    93     
    94      
    95      
    96 }

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

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

  • 相关阅读:
    关于敏捷开发的一些思考。
    【转】浅谈程序猿的职业规划,看你如何决定自己的未来吧。
    Individual Project
    homework_06 围棋程序改进
    homework 08_2 C++11新特性作业之二
    homework_08
    软件工程个人作业——Agile Software Development读后感
    附加题——软件工程之结对编程
    软件工程——PairProject
    软件工程第一次个人项目——词频统计by11061153柴泽华
  • 原文地址:https://www.cnblogs.com/hellokitty1/p/5161238.html
Copyright © 2020-2023  润新知