• 数据回显


    在添加数据的时候有时因为数据条件不符合而页面的数据要重新输入比较麻烦,所以要做数据回显功能

    根据提供domain里的基本数据类写个类CustomerFormBean .java

     1 package cn.itcast.Controller;
     2 
     3 import java.util.Date;
     4 import java.util.HashMap;
     5 import java.util.Map;
     6 
     7 public class CustomerFormBean {
     8     private String id;
     9     public String getId() {
    10         return id;
    11     }
    12     public void setId(String id) {
    13         this.id = id;
    14     }
    15     private String name;
    16     private String gender;//  1男  0女
    17     private String birthday;
    18     private String cellphone;
    19     private String email;
    20     private String []hobbies;// 足球,篮球,乒乓球
    21     private String type;//VIP 普通客户
    22     private String description;
    23     Map<String,String> errors=new HashMap<String,String>();
    24     public String getName() {
    25         return name;
    26     }
    27     public void setName(String name) {
    28         this.name = name;
    29     }
    30     public String getGender() {
    31         return gender;
    32     }
    33     public void setGender(String gender) {
    34         this.gender = gender;
    35     }
    36     public String getBirthday() {
    37         return birthday;
    38     }
    39     public void setBirthday(String birthday) {
    40         this.birthday = birthday;
    41     }
    42     public String getCellphone() {
    43         return cellphone;
    44     }
    45     public void setCellphone(String cellphone) {
    46         this.cellphone = cellphone;
    47     }
    48     public String getEmail() {
    49         return email;
    50     }
    51     public void setEmail(String email) {
    52         this.email = email;
    53     }
    54     public String[] getHobbies() {
    55         return hobbies;
    56     }
    57     public void setHobbies(String[] hobbies) {
    58         this.hobbies = hobbies;
    59     }
    60     public String getType() {
    61         return type;
    62     }
    63     public void setType(String type) {
    64         this.type = type;
    65     }
    66     public String getDescription() {
    67         return description;
    68     }
    69     public void setDescription(String description) {
    70         this.description = description;
    71     }
    72     
    73     public Map<String, String> getErrors() {
    74         return errors;
    75     }
    76     public boolean validate()
    77     {
    78         if(null==birthday||"".equals(birthday))
    79         {
    80             errors.put("birthday", "请输入生日数据");
    81             return false;
    82         }
    83         return true;
    84     }
    85 }
    View Code
     1         request.setCharacterEncoding("utf-8");
     2         CustomerFormBean formBean=new CustomerFormBean();
     3         try {
     4             BeanUtils.populate(formBean,request.getParameterMap());      //装载数据到formbean实例上
     5             if(false==formBean.validate())     //数据验证不通过
     6             {
     7                 request.setAttribute("formBean", formBean);      //用于数据回显  在jsp页面上 回显
     8                 request.getRequestDispatcher("/WEB-INF/jsp/addCustomer.jsp").forward(request, response);
     9                 return;
    10             }
    11         } catch (Exception e) {
    12             e.printStackTrace();
    13         }

    jsp页面

     1 <%@ page language="java" contentType="text/html; charset=utf-8"
     2     pageEncoding="utf-8"%>
     3 <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
     4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     5 <html>
     6 <head>
     7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
     8 <title>Insert title here</title>
     9 </head>
    10 <body>
    11     <form action="${pageContext.request.contextPath}/AddCustomerServlet"
    12         method="post">
    13         <table align="center" cellpadding="5" border="4">
    14             <tr>
    15                 <th>姓名</th>
    16                 <td><input type="text" name="name" value="${formBean.name }"/></td>
    17             </tr>
    18             <tr>
    19                 <th>性别</th>
    20                 <td><input type="radio" name="gender" value='0'  ${formBean.gender==0? 'checked="checked"':''} />21                  <input
    22                     type="radio" name="gender" value='1'   ${formBean.gender==1? 'checked="checked"':''}/></td>
    23             </tr>
    24             <tr>
    25                 <th>生日</th>
    26                 <td><input type="text" name="birthday" value ="${formBean.birthday }"/>${formBean.errors.birthday}</td>
    27             </tr>
    28             <tr>
    29                 <th>电话</th>
    30                 <td><input type="text" name="cellphone"  value ="${formBean.cellphone }"/></td>
    31             </tr>
    32             <tr>
    33                 <th>email</th>
    34                 <td><input type="text" name="email"  value ="${formBean.email }"/></td>
    35             </tr>
    36             <tr>
    37                 <th>爱好</th>
    38                 <td><input type="checkbox" name="hobbies" value="游泳" ${fn:contains(requestScope.formBean.hobbies,'游泳')?'checked="checked"':''}/>游泳 <input
    39                     type="checkbox" name="hobbies" value="看戏"  ${fn:contains(requestScope.formBean.hobbies,'看戏')?'checked="checked"':''}/>看戏 <input
    40                     type="checkbox" name="hobbies" value="耍斗" ${fn:contains(requestScope.formBean.hobbies,'耍斗')?'checked="checked"':''}/>耍斗</td>
    41             </tr>
    42             <tr>
    43                 <th>类型</th>
    44                 <td><input type="text" name="type" /></td>
    45             </tr>
    46             <tr>
    47                 <th>描述</th>
    48                 <td><textarea rows="4" cols="20" name="description" value="${formBean.description}"> </textarea>
    49                 </td>
    50             </tr>
    51             <tr align="center">
    52                 <td colspan="2"><input type="submit" value="添加" /></td>
    53             </tr>
    54 
    55         </table>
    56     </form>
    57 </body>
    58 </html>
    View Code
  • 相关阅读:
    工作经验和学历孰优孰劣
    关于c语言文件的基本操作1
    从头学起Expert C Program 1
    栈和堆——c语言中
    浅谈进程和线程的区别
    通用试题库组卷策略算法
    marquee标记用法及在asp.net中的应用(来自于网上)
    在ASP.NET页面中显示自动生成图片的两种方法
    《ASP.NET高级编程》电子版下载地址
    从中央气象台抓取天气预报
  • 原文地址:https://www.cnblogs.com/friends-wf/p/3745184.html
Copyright © 2020-2023  润新知