将request容器中的参数封装到javabean中
2013-12-08 21:02
387 查看
WEB开发中,将表单提交的数据封装到bean中,这样servlet处理起来,不需要多次调用request.getParameters方法来获取request容器的参数,将request参数封装到bean中,方便servlet来处理;代码也会更加清晰和优雅。下面是一个例子。
//example,将request对象封装到T的对象中
public static <T> T request2Bean(HttpServletRequest request,Class<T> clazz){
try{
T t = clazz.newInstance(); //申请一个对象
//自定义转换类,对日期进行转换
ConvertUtils.register(new Converter() {
public Object convert(Class type, Object value) {
if (value == null) {
return null;
}
if (!(value instanceof String)) {
throw new ConversionException("conversion error");
}
String str = (String) value;
if (str.trim().equals("")) {
return null;
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
return sdf.parse(str);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}, Date.class);
Enumeration<String> e = request.getParameterNames();
while(e.hasMoreElements()){
String name = e.nextElement();
String value = request.getParameter(name);
BeanUtils.setProperty(t, name, value);
}
return t;
}catch (Exception e) {
throw new RuntimeException(e);
}
}
//demo,User对象
public class User {
private String id;
private String name;
private String gender;
private String type;
private String cellphone;
private Date birthday;
private String email;
private String prefences;
private String description;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getCellphone() {
return cellphone;
}
public void setCellphone(String cellphone) {
this.cellphone = cellphone;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPrefences() {
return prefences;
}
public void setPrefences(String prefences) {
this.prefences = prefences;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
//servlet
//example,将request对象封装到T的对象中
public static <T> T request2Bean(HttpServletRequest request,Class<T> clazz){
try{
T t = clazz.newInstance(); //申请一个对象
//自定义转换类,对日期进行转换
ConvertUtils.register(new Converter() {
public Object convert(Class type, Object value) {
if (value == null) {
return null;
}
if (!(value instanceof String)) {
throw new ConversionException("conversion error");
}
String str = (String) value;
if (str.trim().equals("")) {
return null;
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
return sdf.parse(str);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}, Date.class);
Enumeration<String> e = request.getParameterNames();
while(e.hasMoreElements()){
String name = e.nextElement();
String value = request.getParameter(name);
BeanUtils.setProperty(t, name, value);
}
return t;
}catch (Exception e) {
throw new RuntimeException(e);
}
}
//demo,User对象
public class User {
private String id;
private String name;
private String gender;
private String type;
private String cellphone;
private Date birthday;
private String email;
private String prefences;
private String description;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getCellphone() {
return cellphone;
}
public void setCellphone(String cellphone) {
this.cellphone = cellphone;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPrefences() {
return prefences;
}
public void setPrefences(String prefences) {
this.prefences = prefences;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
//servlet
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); try{ User user = WebUtils.request2Bean(request, User.class);//将request容器中的参数封装到User类中 BusinessService service = BusinessServiceFactory.newInstance().createService(); service.add(user); request.setAttribute("message", "客户添加成功!!"); }catch (Exception e) { e.printStackTrace(); request.setAttribute("message", "客户添加失败!!"); } request.getRequestDispatcher("/message.jsp").forward(request, response); }
相关文章推荐
- struts2 中属性驱动(其实就是struts2 action 中处理 request 的参数【old 方式servlet api 封装数据到javabean中(or beanutils)】),这里属性驱动是新方式
- 反射封装request参数为对象
- java 获取request中的参数,并将所有参数封装成map 工具le
- 自定义类封装request配合filter解决参数乱码
- 在HttpServletRequest request获得参数,通过类的set方法封装到对象中
- Springmvc注解开发:日期类型参数封装报错:The request sent by the client was syntactically incorrect.
- 项目升级-加密的参数传递到后台然后解密(相当于重新封装下request)
- HttpServletRequestWrapper采用装饰模式重新封装request,并打印request请求相关参数。
- struts使用JavaBean封装表单参数
- 将request请求参数封装为map
- JavaBean实现简单参数封装
- 0056 Spring MVC如何接收浏览器传递来的请求参数--request--形参--实体类封装
- Volley介绍JSon 和提交字符串参数的封装(StringRequest和Gson)
- C# HttpWebRequest用Post同时提交参数和文件的封装类
- Spring MVC如何接收浏览器传递来的请求参数--request--形参--实体类封装(类比Struts2模型驱动)
- 获取request参数并通过BeanUtil进行封装处理的参考工具类
- 关于在struts2中interceptor 中使用ActionInvocation 对象获得HttpServletRequest中封装的参数,以Map的形式
- 请求参数封装到JavaBean从入门到精通
- struts2中action如何获取Session,request,jsp页面参数等等信息的值
- Jmeter对于请求参数封装及使用