Announcement Announcement Module
Collapse
No announcement yet.
Spring Controller - How to handle if required attributes are not present Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring Controller - How to handle if required attributes are not present

    Hello,
    I was just wondering if there is an easier way to handle the case when required parameters are not passed to the Controller.

    Say I have a method in my (address) controller
    Code:
    public String showBuyItNow(@RequestParam("order")
        Integer orderId, WebRequest request, Map<String, Object> model) 
    {
    }
    So if someone tries to access the site - this way
    1) http://localhost/address
    I get an error saying that "Required java.lang.Integer parameter 'order' is not present"

    2) And if someone accesses it this way
    http://localhost/address?order
    I get an error saying java.lang.IllegalArgumentException: id to load is required for loading

    The way I have handled the first case, is to explicitly mark even required attributes as false.
    Code:
    public String showBuyItNow(@RequestParam(value = "order", required = false)
        Integer orderId, WebRequest request, Map<String, Object> model)
    The way I'm handling the second case is by explicitly checking if orderId==null - then send to error page.

    For both these cases is there a better way to handle it in Spring.
    Appreciate your help....
    Thanks
Working...
X