Java – format spring error message parameters

I have a spring boot web application, and I reject the value in the controller, as follows:

@RequestMapping(value = "/create",method = RequestMethod.POST)
public String createSubmit(@modelattribute("createForm") CreateForm createForm,BindingResult result,SessionStatus status) {
    DateTime dt1 = createForm.getDt1();
    DateTime dt2 = createForm.getDt2();

    if (!dt1.isBefore(dt2)){
        result.rejectValue("fieldId","validation.isbefore",new Object[]{dt1,dt2},"first date must be before second");
    }
}

Therefore, if the date dt1 does not precede DT2, the value is rejected Now, I have a very standard resourcebundlemessagesource in messages_ en. There is this entry in properties:

validation.isbefore = Start date {0} must be before end date {1}

When a validation error occurs, I receive a message that the start date 3 / 21 / 16 5:01 PM must be before the end date 3 / 20 / 16 5:01 PM (both dt1 and DT2 use their tostring() to format the message)

Now, Java text. Messageformat supports some formats, namely {0, date, short} But this only applies to Java util. Date, not joda time (or any other custom class)

Is there any way to customize the format of error message parameters? I don't want to do this during validation (in the final code, the validator itself is separated from the controller and has no information about the selected language, so it doesn't know what date format to use)

Solution

You can try to format the date using customdateeditor and webbinding in the controller You can try to add something like this to the controller:

@Controller
public class MyController {
       @InitBinder
       public void customizeBinding (WebDataBinder binder) {
            SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
            binder.registerCustomEditor(Date.class,"dt1",new CustomDateEditor(dateFormatter,true));
            binder.registerCustomEditor(Date.class,"dt2",true));
       }

       @RequestMapping(value = "/create",method = RequestMethod.POST)
       public String createSubmit(@modelattribute("createForm") CreateForm 
       createForm,SessionStatus status) {
            DateTime dt1 = createForm.getDt1();
            DateTime dt2 = createForm.getDt2();

            if (!dt1.isBefore(dt2)){
                 result.rejectValue("fieldId",new 
                 Object[]{dt1,"first date must be before second");
            }
        }

}

I used and modified this example: http://www.logicbig.com/tutorials/spring-framework/spring-web-mvc/spring-custom-property-editor/

This is the official spring document for the built-in Attribute Editor: http://docs.spring.io/spring-framework/docs/current/spring-framework-reference/html/validation.html#beans -beans-conversion

I hope it can use the format method to display the required format when converting it to string

Otherwise, you may have to do something very strange and extend datetime to your own datetime and override the toString () method, but this seems to be an overly powerful force for the solution

public class MyDateTime extends DateTime {

      @Override
      public toString() {
           return new SimpleDateFormat("yyyy-MM-dd).format(this);
      }
 }

then

MyDateTime dt1 = createForm.getDt1();
MyDateTime dt2 = createForm.getDt2();
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>