How to trim input field value in struts?

I use Struts v1.3 and have the following input form:

In struts-config.xml:

<form-bean name="testForm" type="org.apache.struts.validator.DynaValidatorForm"> <form-property name="displayName" type="java.lang.String" /> </form-bean> 

In validation.xml:

  <form name="testForm"> <field property="displayName" depends="required"> <arg key="input.displayName" /> </field> </form> 

How do I trim the value of "displayName"? How to trim values ​​of all input fields "java.lang.String" of a form?

+4
source share
4 answers

You may be able to trim the string right now, the query processor will update the data from the input fields in the form. This has not been tested, but what happens when you change setterSizeDisplayName (String displayName) to something like

 public void setDisplayName(String displayName) { this.displayName = displayName.trim(); } 

This is not a good solution because it transfers logic to the setter.

considers

+2
source

If you want to use cropping for validation purposes, I believe that the right way is to create and use your own (or extend an existing) validator for required fields that take care of accuracy. For example, you can use this page: http://struts.apache.org/1.2.4/userGuide/dev_validator.html , section "Plug-in validators".

If you want to use cropping to trim String values ​​to using them in your business logic , you can extend org.apache.struts.validator.DynaValidatorForm and overwrite methods that retrieve values, such as get (string name), getString (string name ) etc. After that, you use your class in bean form declarations.

+1
source

If you don't mind having String manipulation logic in your Form class, you can try StringUtils methods in the Apache Commons Lang JAR:

StringUtils JavaDoc

This will allow you to trim your lines in several ways, be it trimToEmpty, trimToNull, etc. This means that you have access to null methods that may be useful with some values.

0
source

Alternatively try using javascript regexp in jsp that will crop onfocus or onblur

<html: text name = "testForm" property = "displayName" onfocus = "javascript: this.value = this.value.replace (/ ^ \ s + | \ s + $ / g, '')" onblur = "javascript : this.value = this.value.replace (/ ^ \ s + | \ s + $ / g, '') "/">

-1
source

All Articles