Hi thanks for reaching out! I don't remember and should check that your desired behavior used to work or not but as a quick answer I think you're looking for Class wide conversion [1] e.g. @TypeConversion(type = ConversionType.APPLICATION, property = "java.util.Date", DateConverter.class)?
Regards. [1] https://struts.apache.org/core-developers/type-conversion-annotation.html On 2021/04/25 20:02:47, M Huzaifah <mhuzaifah.i...@gmail.com> wrote: > Hy guys, > > i found somethin strange with StrutsTypeConverter. > let say i have a converter like this: > > > //------------------ begin DateConverter class --------------- > import java.text.ParseException; > import java.text.SimpleDateFormat; > import java.util.Date; > import java.util.Map; > > import javax.annotation.PostConstruct; > > import org.apache.commons.lang.ArrayUtils; > import org.apache.commons.lang.StringUtils; > import org.apache.struts2.util.StrutsTypeConverter; > > public class DateConverter extends StrutsTypeConverter { > > private String shortDateFormat; > > @PostConstruct > public void loadFormat() { > this.shortDateFormat = "dd/MM/yyyy"; > } > > @SuppressWarnings("unused") > @Override > public Object convertFromString(Map arg0, String[] arg1, Class arg2) { > > System.out.println("begin converter"); > > if(ArrayUtils.isEmpty(arg1) || StringUtils.isEmpty(arg1[0])) { > return null; > } > > Date date = null; > > try { > //cek kalo formatnya dd/MM/yyyy hh:mm:ss > if(arg1[0].contains(" ")) { > this.shortDateFormat = "dd/MM/yyyy hh:mm:ss"; > date = new > SimpleDateFormat(shortDateFormat).parse(arg1[0]); > }else { > date = new > SimpleDateFormat(shortDateFormat).parse(arg1[0]); > } > }catch (ParseException e) { > System.out.println("DateConverter StrutsTypeConverter > gagal parsing date;"); > e.printStackTrace(); > } > > System.out.println("end converter"); > return date; > } > > @Override > public String convertToString(Map arg0, Object arg1) { > > System.out.println("begin converter to string"); > if(arg1 instanceof Date) { > System.out.println("end converter to string"); > return new > SimpleDateFormat(shortDateFormat).format((Date) arg1); > } > > System.out.println("end converter to string"); > return StringUtils.EMPTY; > } > > > > } > > //------------------ end DateConverter class --------------- > > > > and in my action class, i have 2 property. one of them is Date type. and > other one is Pojo which one of that Pojo property have a Date type. so my > action look like this > > > //------------------ begin MasterPatienAction class --------------- > > @Namespace("/patient") > @ParentPackage("app-default") > @Conversion(conversions = { > @TypeConversion(key = "birthDate", converterClass = > DateConverter.class) > }) > public class MasterPatientAction extends BaseAction { > > /** > * > */ > private static final long serialVersionUID = 1L; > > private static transient Logger log = > Logger.getLogger(MasterPatientAction.class); > > > Patient patient; > List<Patient> listPatient; > Date birthDate; > > @Action(value = "init", results = { > @Result(name = "success", location = > "/pages/pasient-main.jsp"), > @Result(name = "error", location = > "/pages/pasient-main.jsp"), > @Result(name = "input", location = > "/pages/pasient-main.jsp") > }) > public String init() { > log.info("begin execute method init"); > > log.info("end execute method init"); > return SUCCESS; > } > > @Action(value = "search", results = { > @Result(name = "success", location = > "/pages/pasient-main.jsp"), > @Result(name = "error", location = > "/pages/pasient-main.jsp"), > @Result(name = "input", location = > "/pages/pasient-main.jsp") > }) > public String search() { > log.info("begin execute method search"); > > if(birthDate!=null) { > patient.setPatientBirthDate(birthDate); > } > > try { > listPasien = service.search(patient); > } catch (DnaException e) { > setMessageError("error caused : "+e.getMessage()); > return ERROR; > } > > log.info("end execute method search"); > return SUCCESS; > } > > //--setter getter > } > > > //------------------ end MasterPatienAction class --------------- > > > let say my pojo like this: > > > //------------------ begin Patient pojo class --------------- > > public class Patient { > private integer patientId; > private String patientName; > private String patientCode; > private Date patientBirthDate; > > //setter and getter goes here > > } > > //------------------ end Patient pojo class --------------- > > > for the first case, in my JSP, i use struts tag : > <s:textfield name="birthDate" class="form-control form-control-sm text-md > datepicker" placeholder="Birth Date dd/MM/yyyy"/> > > until here, i dont have a problem with it. i can convert my date string (with > format dd/MM/yyyy e.g 20/01/2021) from JSP to the action class with this step: > 1. request /patient/init (to show jsp) > 2. insert value in text field with format dd/MM/yyyy e.g 20/01/2021 > 3. submit with action to /patient/search > 4. in action class date converted successful on search method > 5. return success from action class > 6. text birthDate still the same (e.g 20/01/2021) > > here the second case, when i want to convert my date type in my Pojo > so, i change my typeConversion key to : patient.patientBirthDate and also in > my textfield tag on JSP i change name to patient.patientBirthDate. so this is > what i change: > 1. change key in TypeConvertion annotation to key = "patient.patientBirthDate" > 2. change textfield tag in jsp to <s:textfield > name="patient.patientBirthDate" /> > > when i follow step in first case: > 1. request /patient/init (to show jsp) > 2. insert value in text field with format dd/MM/yyyy e.g "20/01/2021" > 3. submit with action to /patient/search > 4. in action class date converted successfull > 5. return success from action class > 6. but my textfield with name patient.patientBirthDate in JSP change to > "1/20/21" (the value should not change. it should be "20/01/2021") also in my > console i dont see "begin converter to string" printed which i put in > convertToString method in converter class. > > i use <struts-version>2.5.22</struts-version> > > could someone explain to me and how can i fix it. > > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: user-unsubscr...@struts.apache.org > For additional commands, e-mail: user-h...@struts.apache.org > > --------------------------------------------------------------------- To unsubscribe, e-mail: user-unsubscr...@struts.apache.org For additional commands, e-mail: user-h...@struts.apache.org