• web组件工具之获取表单数据:webUtils


      本文需要的架包:commons-beanutils-1.8.3.jar、commons-logging-1.1.3.jar、servlet-api.jar。

      本文共分为五部分:1)封装通用工具类:从表单接收数据并封装到实体类中;2)绘制jsp页面:传输数据; 3)创建实体类:用来存放表单数据; 4)创建servlet类:调用工具类,实现数据封装; 5) 配置web.xml文件

      注意:1)本工具类也同样适用于action;

         2)jsp中的表单名称与实体类中的属性名称必须一致。

    1.封装通用工具类(WebUtils.java)

    package com.BlueStar.util;
    
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import javax.servlet.http.HttpServletRequest;
    import org.apache.commons.beanutils.BeanUtils;
    import org.apache.commons.beanutils.ConvertUtils;
    import org.apache.commons.beanutils.Converter;
    
    public class WebUtils {
        
        public static <T> T getData(HttpServletRequest req,Class<T> clazz){
            // convert Date start
            ConvertUtils.register(new Converter() {            
                @Override
                public Object convert(Class type, Object value) {
                    //filter
                    if(type != Date.class) return null;
                    if(value == null || "".equals(value.toString().trim())) return null;
                    //convert
                    SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
                    Date date = null;
                    try {
                        date = format.parse(value.toString());
                    } catch (ParseException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    
                    return date;
                }
            }, Date.class);
            // convert Date end
            
            T t = null;
            try {
                //newInstance
                t = clazz.newInstance();
                //set value from jsp
                BeanUtils.populate(t, req.getParameterMap());
            } catch (Exception e) {
                e.printStackTrace();
            }
            return t;
        }
    }

    2. 绘制jsp页面(userInfo.jsp)

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
        <form action="info" method="post">
            姓名:<input type="text" name="userName" width="20%"/><br/><br/>
            密码:<input type="password" name="password" width="20%"/><br/><br/>
            生日:<input type="text" name="birthday" width="20%"/><br/><br/>
            
            <button type="submit" value="submit">submit</button>
            &nbsp;&nbsp;&nbsp;
            <button type="reset" value="reset">reset</button>
        </form>
    </body>
    </html>

    3.创建实体类(UserInfoFormBean.java)

    package com.BlueStar.entity;
    
    import java.util.Date;
    
    public class UserInfoFormBean {
        
        private String userName;
        private String password;
        private Date birthday;
        
        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;
        }
        
        public Date getBirthday() {
            return birthday;
        }
        public void setBirthday(Date birthday) {
            this.birthday = birthday;
        }
        @Override
        public String toString() {
            return "UserInfoFormBean [userName=" + userName + ", password="
                    + password + ", birthday=" + birthday + "]";
        }
         
    }

    4.创建servlet(UserInfoServlet.java)

    package com.BlueStar.servlet;
    
    import java.io.IOException;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import com.BlueStar.entity.UserInfoFormBean;
    import com.BlueStar.util.WebUtils;
    
    public class UserInfoServlet extends HttpServlet{
        
        private static final long serialVersionUID = 1L;
    
        @Override
        protected void service(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
            
            UserInfoFormBean formBean = WebUtils.getData(req, UserInfoFormBean.class);
            System.out.println(formBean);
        }
    }

    5. 配置web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
      <display-name>library</display-name>
      <welcome-file-list>
        <welcome-file>userInfo.jsp</welcome-file>
      </welcome-file-list>
      
      <servlet>
          <servlet-name>UserInfoServlet</servlet-name>
          <servlet-class>com.BlueStar.servlet.UserInfoServlet</servlet-class>
      </servlet>
      <servlet-mapping>
          <servlet-name>UserInfoServlet</servlet-name>
        <url-pattern>/info</url-pattern>     
      </servlet-mapping>
    </web-app>

      更多内容,请访问:http://www.cnblogs.com/BlueStarWei/

  • 相关阅读:
    Strapi and MongoDB
    Windows 下入手 MongoDB
    npm 创建一个 github action
    Vue3: does not provide an export named 'createRouter'
    How To Use Rocketbots As A Dialogflow CRM
    Telegram Groups vs Telegram Channels
    WhatsApp Group vs WhatsApp Broadcast for Business
    Instant Messaging for Business: Your 10 Best Options
    How to Create and Use Facebook Messenger Codes (June 2019)
    Ultimate Guide to Line For Business (May 2019)
  • 原文地址:https://www.cnblogs.com/BlueStarWei/p/7259585.html
Copyright © 2020-2023  润新知