前辈让总结一些可以通用的代码。 才发现自己写的代码复用性这么差。 只找出来了下面的几个方法。 还有那个封装对象的方法是后来加上去的。 本机测试成功了。可能大家会觉得这个方法根本没用。 有ActionForm就行了 而且比我这个方法要强大的多。 不过。 我这里不让用ActionForm. 我又不想重复地进行 request.getParameter这种浪费时间的事 。 就写了这个方法。。- package com.softtech.qihua.action;
- import java.util.*;
- import java.lang.reflect.*;
- import java.text.*;
- import javax.servlet.http.HttpServletRequest;
- import org.apache.struts.action.ActionError;
- import org.apache.struts.action.ActionErrors;
- public class method{
-
- private NumberFormat format = NumberFormat.getInstance(); ///数字格式化对象
- private SimpleDateFormat dateformat = null; /////日期格式化对象
- private String pattern = "yyyy-MM-dd HH:mm:ss"; ///格式化日期的模式信息
-
- public String getPattern() {
- return pattern;
- }
- public void setPattern(String pattern) {
- this.pattern = pattern;
- }
-
- public method() {
-
- }
-
- /**四舍五入
- * @param value 要进行四舍五入的数
- *
- * */
- public int round(double value) {
- format.setMaximumFractionDigits(0);
- return Integer.parseInt(format.format(value));
- }
-
- /**
- * 四舍五入指定的位数
- * @param value 要进行四舍五入的数
- * @param num 要保存的小数位数
- * */
- public double round(double value,int num) {
- format.setMaximumFractionDigits(num);
- return Double.parseDouble(format.format(value));
- }
-
-
- /**
- * 得到一个指定时间的字符串表示形式
- * @param dates 可选的Date类的对象 返回这个Date对象的字符串表示形式 如果不传入这个参数 默认返回当前时间的字符串表示形式
- * */
- public String getStringOfDate(Date...dates) {
-
- if (this.pattern == null) {
- return null;
- }
-
- try {
-
- dateformat = new SimpleDateFormat(pattern);
-
- if (dates.length >0) {
- return dateformat.format(dates[0]);
- }
-
- return dateformat.format(new Date());
- } catch (Exception e) {
- e.printStackTrace();
- return "";
- }
-
- }
-
- /**
- * 根据字符串得到Date类的对象
- * @param value 保存有时间的字符串值
- * @return date
- * */
- public Date getDateOfString(String value) {
-
- if (pattern == null) {
- return null;
- }
-
- try {
- dateformat = new SimpleDateFormat(pattern);
- return dateformat.parse(value);
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- }
-
-
- /**
- * 对用户提交的参数进行检测的方法
- * @param request
- * @param obj 要将提交的参数封装入的对象
- * @param 必须的参数数组
- * @param 参数的类型数组
- * @param 错误对象
- * @return success 对象是否封装成功 .
- * */
- public boolean checkParameter(HttpServletRequest request,Object obj,String[] parameters,Class[] types,ActionErrors errors) {
-
-
- Class cla = obj.getClass();
-
- try {
-
- for (int i = 0;i<parameters.length;i++) {
- String value = request.getParameter(parameters[i]);
-
- if (value == null || value.trim().length() == 0) {
- errors.add("msg",new ActionError("error.userdefine",parameters[i]+"不能为空!"));
- return false;
- } else {
- String name = Character.toUpperCase(parameters[i].charAt(0)) + parameters[i].substring(1);
- Method me = cla.getDeclaredMethod("set"+name, types[i]);
- me.setAccessible(true);
-
- String ty = types[i].getSimpleName();
-
- if (ty.equals("Integer") || ty.equals("Long") || ty.equals("Short")) {
- if (value.matches("^\\d+$") == false ) {
- errors.add("msg",new ActionError("error.userdefine",parameters[i]+"只能是数字!"));
- return false;
- } else {
- if (ty.equals("Integer")) {
- me.invoke(obj, Integer.parseInt(value));
- } else if (ty.equals("Long")) {
- me.invoke(obj, Long.parseLong(value));
- } else {
- me.invoke(obj, Short.parseShort(value));
- }
-
- }
- } else if (types[i].getSimpleName().equals("Date")) {
- me.invoke(obj, this.getDateOfString(value));
- } else {
- me.invoke(obj, value);
- }
- }
-
- }
- }catch (NoSuchMethodException e) {
- e.printStackTrace();
- errors.add("msg",new ActionError("error.userdefine","参数类型不正确!"));
- } catch (Exception e) {
- e.printStackTrace();
- }
- return true;
- }
-
- public static void main(String[] args) {
- try {
-
-
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
-
复制代码 |