WEB开发网
开发学院软件开发Java Java取得request对象中所有的参数值并生成实体对象... 阅读

Java取得request对象中所有的参数值并生成实体对象工具类

 2012-06-04 16:46:29 来源:WEB开发网   
核心提示:package com.crazyiter.mssp.utils;import java.util.Map;import javax.servlet.http.HttpServletRequest;import org.apache.commons.beanutils.BeanUtils;/** * 取得request
package com.crazyiter.mssp.utils;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.beanutils.BeanUtils;
/**
 * 取得request对象中 所有的参数值并生成 一个相应的对象返回
 * @author john
 *
 */
@SuppressWarnings("all")
public class ParseHtml {
    /**
     * Class<T> beanClass可以接受任何类型的javaBean,使用泛型调用者不用进行强转
     * @param <T>
     * @param request
     * @param beanClass
     * @return
     */
    public static <T> T request2Bean(HttpServletRequest request, Class<T> beanClass) {
        try {
            /**创建封装数据的bean**/
            T bean = beanClass.newInstance();
            Map map = request.getParameterMap();
            BeanUtils.populate(bean, map);
            return bean;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
} 
package com.crazyiter.mssp.action;

import java.io.IOException;
import java.lang.reflect.Method;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.crazyiter.mssp.common.ReflectionUtils;
import com.crazyiter.mssp.domain.SmsConfig;
import com.crazyiter.mssp.service.SmsService;
import com.crazyiter.mssp.utils.ParseHtml;

/**
 * 用户管理Controller
 * @author john
 */
@SuppressWarnings("serial")
public class SmsConfigAction extends HttpServlet {
    SmsService smsService = new SmsService();
    /**
     * 根据短信配置信息
     */
    public void getsmsconfig(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        SmsConfig smsConfig = smsService.getSmsConfig();
        request.setAttribute("smsconfig", smsConfig);
        request.getRequestDispatcher("view/smsconfig/edit.jsp").forward(request,response);
    }
    /**
     * 修改短信配置信息
     */
    public void updatesmsconfig(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        SmsConfig  sc = (SmsConfig)ParseHtml.request2Bean(request, SmsConfig.class);
        System.out.println(sc.getUrl());
        request.getRequestDispatcher("index.jsp").forward(request,response);
    }
    /**
     * 短信数据Controller过滤器
     */
    public void service(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        String cmd = request.getParameter("cmd");
        Method method = ReflectionUtils.getDeclaredMethod(SmsConfigAction.class, cmd);
        try {
            if(method!=null){
                method.invoke(new SmsConfigAction(),request, response);
            }else{
                request.getRequestDispatcher("index.jsp").forward(request,response);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
} 

Tags:Java 取得 request

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接