Announcement Announcement Module
Collapse
No announcement yet.
How to handle multiple POST-methods in one Controller? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to handle multiple POST-methods in one Controller?

    Hi

    My Controller : "AccountController" have to handle multiple POST-requests for Example SendPrivateMessage, SaveProfileSettings etc.

    Code:
    @Controller
    @RequestMapping(value = "/profile")
    public class AccountController{
        @RequestMapping(method = RequestMethod.POST)
        public String doPost(Object obj, HttpServletRequest request, Model model) {
            
            _logger.info("doPost:" + obj);
            
            if(obj instanceof MessageReply){
                MessageReply message = (MessageReply) obj;
                return doSendMessage(message, request);
            } else if (obj instanceof ProfileSettings){
                _logger.info("ProfileSettings: " + obj);
            } else {
                _logger.info(obj.getClass().toString()); // <- allways 'java.lang.Object'
            }
            return Page.USER_HOME.file;
        }
    
    //....
    }
    the method doPost getting allways as parameter 'obj' simple Object. no way to cast...

    How can i handle multiple POSTs in the same Controller?

    Important: I dont want to use other Controllers and i have to stay in "/profile"-Path

    Thx!

  • #2
    Hi,

    let's approach the "multiple POSTs" issue first. Just use the "value" attribute in @RequestMapping:
    Code:
    @RequestMapping(value = "/someUrl")
    So, you'll end up with something like /profile/someUrl

    If you're expecting to handle multiple POSTs (or GETs, PUTs, etc.) with a single URI, then the answer is "no you can't".

    I kindly suggest you visit the Spring MVC showcase repository, and review the documentation (it's really good).

    Best,
    Carlos

    Comment

    Working...
    X