0

I am using Struts2,

I have a Action with Properties, I have a property Person, with "date" property.

class Person{
      Date birthDate;
      //more properties
}

class MyAction implements ModelDriven<Person>{
      Person person;
       public String create(){
            person = new Person();
       }
       public String save(){
            MyPersistenceContext.save(person);
       }
       @Override
       public PhysicalPerson getModel() {
           return person;
       }
}


            <label class="col-sm-2 control-label">Birthdate</label>
               <div class="col-sm-4">
                   <s:textfield name="birthDate" cssClass="form-control"></s:textfield>
                   <s:fielderror name="birthDate"
                              fieldName="birthDate"></s:fielderror>
            </div>

When I call the "create" method the Input shows as MM/dd/yy(short format), and when I do submit of the form the date is readed as the same format by the Struts, but I want to manage the date with the format "dd-MM-yyyy"

I see the page

http://www.mkyong.com/struts2/how-to-configure-global-resource-bundle-in-struts-2/

But, I think that the Listener method is called before of the Filter, and does not work Well.

what is the best way to manage a Locale Global Properties with Struts???

What is the best way to setting a Locale by Http Session User.

regards.

jrey
  • 2,163
  • 1
  • 32
  • 48

1 Answers1

0

I have solved my problem with a Converter, i do not know if this is the best way... but finally I can do it.

public class CustomDateConverter extends StrutsTypeConverter {

private static Logger logger = LoggerFactory.getLogger(CustomDateConverter.class);

private static final String DEFAULT_FORMAT = "dd-MM-yyyy";
private static Map<String, DateFormat> instances = new LinkedHashMap<>();

static DateFormat getInstance(String format) {
    if (instances.containsKey(format)) {
        return instances.get(format);
    }
    DateFormat dateFormat = new SimpleDateFormat(format);
    instances.put(format, dateFormat);
    return dateFormat;
}

@Override
public Object convertFromString(Map map, String[] values, Class aClass) {
    try {
        logger.info("convert from string");
        DateFormat dateFormat = getInstance(DEFAULT_FORMAT);
        Date date = (Date) dateFormat.parse(values[0]);
        return date;
    } catch (ParseException e) {
        return values[0];
    }
}
@Override
public String convertToString(Map map, Object o) {
    logger.info("convertToString");
    DateFormat dateFormat = getInstance(DEFAULT_FORMAT);
    return dateFormat.format(o);
}
}

for register the converter

I have created a file (resource)

src/main/resources/xwork-conversion.properties (Maven Structure)

with the next content:

 # syntax: <type> = <converterClassName>
 java.util.Date=com.roshka.javorai.webapp.struts.converter.CustomDateConverter
jrey
  • 2,163
  • 1
  • 32
  • 48
  • 1
    This is correct: the only way to impose a date format that is different than the JVM's default formats (for the request locale) is to use a custom `TypeConverter`. However you **must not** cache your `DateFormat` instances like in your example. `DateFormat.parse(String)` is *not* thread-safe. – Glenn Lane Sep 29 '16 at 20:01