0

EDIT: Perhaps I need to be more clear on how this application works. I DO NOT have a form here. I capture the page response and load times using the HTML navigation API and then send a 'GET' request to my application as shown below:

window.onload=function(){
    setTimeout(function(){
        var page = window.location.pathname;
        var xmlhttp = new XMLHttpRequest();
        var pt = performance.timing;

                ...SNIP....
        var load  = pt.loadEventEnd - pt.loadEventStart;
        var redc = window.performance.navigation.redirectCount;

        xmlhttp.open("GET", "http://localhost:8080/wp/timing?app=test&page="+page
                      +"&str="+str
                      +"&tot="+tot
                      +"&red="+red
                      +"&cache="+cache
                      +"&dns="+dns
                      +"&conn="+conn
                      +"&req="+req
                      +"&res="+res
                      +"&dom="+dom
                      +"&load="+load
                      +"&redc="+redc
                      , true);
        xmlhttp.send(null);
    }, 0);
};

This is where my controller (shown below) comes into picture. Now, one of the request parameters here is time in Milliseconds. Spring automatically maps primitive data types with the same name as the request parameters to appropriate fields in a bean. However, this fails when I use a 'Date'.

======================================================================================

I'm working on this application where I receive a certain number of request parameters in a controller and inside the controller, I pass in a custom bean as an argument. Now, I was able to successfully map app the request parameters (with the same names as in the beans) with the POJO. However, now I've added a Date type in my bean and I want the appropriate request parameter to be automatically converted to 'Date'. However, the moment I add the date field in my POJO, the controller does not get called.

Here's a snippet from my code:

Controller:

@Controller
public class Timing {

    @Autowired
    private PageService pageService;

    @RequestMapping(value="/timing", method = RequestMethod.GET)
    @ResponseBody
    public void insert(Page page) throws UnknownHostException {
        page.setTs(new Date());
        pageService.updatePage(page);
    }

I've also tried adding a Custom Editor:

@InitBinder
     public void initBinder(WebDataBinder binder,HttpServletRequest request) {
                     SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yy");
                     dateFormat.setLenient(false);
                     binder.registerCustomEditor(Date.class, new CustomDateEditor(
                                                     dateFormat, true));
     }

Here's my bean:

@Document
public class Page {

    @Id
    private String id;
    private Date ts;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    ....SNIP....

    public Date getTs() {
        return ts;
    }

    public void setTs(Date ts) {
        this.ts = ts;
    }

Any pointers would be appreciated. I;'m using Spring 4.x.

va1b4av
  • 431
  • 9
  • 26

1 Answers1

0

You could use @ModelAttribute or @RequestBody annotation.

Please refer this Using @ModelAttribute on method argument.

Existing stackoverflow question on @ModelAttribute

Community
  • 1
  • 1
Touchstone
  • 5,575
  • 7
  • 41
  • 48