当前位置: 代码迷 >> Web前端 >> jquery serialize()步骤自动编码后,后台使用前解码处理
  详细解决方案

jquery serialize()步骤自动编码后,后台使用前解码处理

热度:299   发布时间:2012-10-20 14:12:48.0
jquery serialize()方法自动编码后,后台使用前解码处理
由于调用Jquery的序列化方法时,jQuery会自动进行编码处理,因此后台使用前,应该先进行解码,然后再使用
// 使用
DynaBean dynaBean = (DynaBean) form;
		super.decodeCharacterForGetMethod(dynaBean, request);

// 方法
	public void decodeCharacterForGetMethod(Object object, HttpServletRequest request) throws Exception {
		if (!StringUtils.equalsIgnoreCase(request.getMethod(), "GET")) {
			return;
		}

		if (object instanceof DynaBean) {
			DynaBean dynaBean = (DynaBean) object;
			DynaProperty origDescriptors[] = dynaBean.getDynaClass().getDynaProperties();
			for (int i = 0; i < origDescriptors.length; i++) {
				String name = origDescriptors[i].getName();
				if (getBeanUtilsBean().getPropertyUtils().isWriteable(dynaBean, name)) {
					Object value = dynaBean.get(name);
					if (value instanceof String) {
						getBeanUtilsBean().copyProperty(dynaBean, name, URLDecoder.decode(value.toString(), "UTF-8"));
					}
				}
			}
		} else {// is a standard JavaBean
			PropertyDescriptor origDescriptors[] = getBeanUtilsBean().getPropertyUtils().getPropertyDescriptors(object);
			for (int i = 0; i < origDescriptors.length; i++) {
				String name = origDescriptors[i].getName();
				if ("class".equals(name)) {
					continue; // No point in trying to set an object's class
				}
				if (getBeanUtilsBean().getPropertyUtils().isReadable(object, name)
						&& getBeanUtilsBean().getPropertyUtils().isWriteable(object, name)) {
					Object value = getBeanUtilsBean().getPropertyUtils().getSimpleProperty(object, name);
					if (value instanceof String) {
						getBeanUtilsBean().copyProperty(object, name, URLDecoder.decode(value.toString(), "UTF-8"));
					}
				}
			}
		}
	}

	public BeanUtilsBean getBeanUtilsBean() {
		return BeanUtilsBean.getInstance();
	}

  相关解决方案