struts2修剪从表单中获取的所有字符串

我使用struts2开发Web应用程序。 我想改进从表单中获取字符串。 为此需要修剪所有字符串,如果获得的字符串为空,则将null设置为field。

为此,我创建了StringConverter

public class StringConverter extends StrutsTypeConverter { @Override public Object convertFromString(Map context, String[] strings, Class toClass) { if (strings == null || strings.length == 0) { return null; } String result = strings[0]; if (result == null) { return null; } result = result.trim(); if (result.isEmpty()) { return null; } return result; } @Override public String convertToString(Map context, Object object) { if (object != null && object instanceof String) { return object.toString(); } return null; } } 

接下来,我将行添加到xwork-conversion.properties

 java.lang.String=com.mypackage.StringConverter 

就这样。 但我没有得到理想的结果。


在jsp构建表单时调用convertToString()方法,但convertFromString()不会调用。

我做错了什么? 如何使用其他方式获得相同的行为?

请不要提供以下解决方案:

  1. 使用javascript删除此类表单元素的值。
  2. 创建使用reflection的util方法。 然后为每个表单bean调用它。

先谢谢,阿列克谢。

您将在StrutsTypeConverter类的代码中找到答案。 基本上,在这个级别,类型转换器框架不知道数据是“来自”还是“到”用户,它只知道它正在从一种类型(String)转换为另一种类型(也是String)。 因为它首先检查“to”类型,所以它总是调用convertToString。

简而言之,当前版本的Struts(2.1.x是我正在使用的) 不能也不能支持String-to-String类型的转换器 。 毕竟,它们是类型转换器,你可以说这是设计的。

我也在寻找一种方法来获得类似的结果,但还没有找到一个非常好的解决方案。 可能最“正确”的方法是编写一个拦截器(如提到的@leonbloy)。 有几种方法可以解决这个问题,最简单的方法是在操作之前设置所有请求参数(即在“params”拦截器执行之前)。

似乎对我来说。 你确定甚至没有调用convertFromString吗?

你可能尝试的另一种方法是编写一个拦截所有参数的拦截器(一个经常需要它)。

我不做Struts2,但类似的问题已经在JSF中出现,直到2006年版本1.2(JSF是Sun的MVC框架,Struts2的竞争对手)。 转换为String在JSF中也是不可能的“按设计”。 较旧的JSF版本用于检查目标类型是否等于java.lang.String然后它只是在模型中设置请求参数值而不尝试转换它(因为请求参数值已经作为String获得)。 如果目标类型不同,那么它将找到并运行任何相关的转换器,将其转换为所需的目标类型(不是String )。 从JSF 1.2开始,他们通过删除目标类型的检查并以任何方式定位转换器来修复它。

如果Struts2中存在类似的function/错误,我不会感到惊讶。 如果没有关于该问题的问题/错误报告,我会环顾他们的主页,否则发布一个。

从这篇博客中提到,我在代码中进行了一些小修改,它运行正常。 这不是任何与struts相关的实用程序,但您可以满足您的需求。

这是实用程序类:

 package com.company.project.common.helpers; import java.io.Serializable; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; /** * * File Name : BeanUtils.java 
* Description : *

* This class contains the activities of skill program.
*
* Copyright : Copyright© yyyy description *

*

Version History :

* * Date : 20-06-2013
* Description : First Draft * * @version 1.0.0.1 * @since 1.0.0.0.1 * @author visruth * */ public class BeanUtils implements Serializable { /** * This method trims all String variables defined in the bean if they have * corresponding getter setter methods.
* Eg : BeanUtils beanUtils=new BeanUtils();
* StudentVO studentVO=new StudentVO();
* studentVO.setName(" foo ");
* studentVO.setAddress(" bar ");
* beanUtils.trimAllStrings(studentVO);
* System.out.println(studentVO.getName());//prints foo
* System.out.println(studentVO.getAddress());//prints bar
* * @param beanObject * @throws Exception * If a variable has its getter method defined but not setter * method will throw NoSuchMethodException instance as * Exception. * @author visruth */ public void trimAllStrings(Object beanObject) throws Exception { Exception noSuchMethodException = null; boolean throwNoSuchMethodException = false; if (beanObject != null) { Method[] methods = null; try { methods = beanObject.getClass().getMethods(); } catch (SecurityException e) { throw new Exception(e); } if (methods != null) { for (Method method : methods) { String methodName = method.getName(); if (!methodName.equals("getClass")) { String returnType = method.getReturnType().toString(); String commonMethodName = null; if (methodName.startsWith("get") && "class java.lang.String".equals(returnType)) { commonMethodName = methodName.replaceFirst("get", ""); String returnedValue = null; try { returnedValue = (String) method .invoke(beanObject); } catch (IllegalArgumentException e) { e.printStackTrace(); throw e; } catch (IllegalAccessException e) { e.printStackTrace(); throw e; } catch (InvocationTargetException e) { e.printStackTrace(); throw e; } if (returnedValue != null) { StringBuilder setterMethodName = new StringBuilder(); setterMethodName.append("set"); setterMethodName.append(commonMethodName); Method setterMethod = null; try { setterMethod = beanObject .getClass() .getMethod( setterMethodName.toString(), String.class); if (setterMethod != null) { if(returnedValue.isEmpty()) { Object o=null; setterMethod.invoke(beanObject, o); } else { setterMethod.invoke(beanObject, (returnedValue.trim())); } } } catch (SecurityException e) { e.printStackTrace(); throw e; } catch (NoSuchMethodException e) { e.printStackTrace(); if (!throwNoSuchMethodException) { noSuchMethodException = e; } throwNoSuchMethodException = true; } catch (IllegalArgumentException e) { e.printStackTrace(); throw e; } catch (IllegalAccessException e) { e.printStackTrace(); throw e; } catch (InvocationTargetException e) { e.printStackTrace(); throw e; } } } } } } } if (throwNoSuchMethodException && noSuchMethodException != null) { throw noSuchMethodException; } } }

尝试:

 package com.company.project.common.valueobject; import java.io.Serializable; import com.company.project.common.helpers.BeanUtils; public class DetailsVO implements Serializable { private static final long serialVersionUID = 6378955155265367593L; private String firstName; private String lastName; private String address; private double latitude; private double longitude; public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public double getLatitude() { return latitude; } public void setLatitude(double latitude) { this.latitude = latitude; } public double getLongitude() { return longitude; } public void setLongitude(double longitude) { this.longitude = longitude; } public static void main(String[] args) { BeanUtils beanUtils = new BeanUtils(); DetailsVO profileVO = new DetailsVO(); profileVO.setFirstName(""); profileVO.setLastName(" last name "); profileVO.setAddress(" address "); System.out.println(profileVO.getFirstName()); System.out.println(profileVO.getLastName()); System.out.println(profileVO.getAddress()); try { beanUtils.trimAllStrings(profileVO); } catch (Exception e) { e.printStackTrace(); } System.out.println(profileVO.getFirstName()); System.out.println(profileVO.getLastName()); System.out.println(profileVO.getAddress()); } } 

并给出这个输出:

  last name address null last name address