Announcement Announcement Module
Collapse
No announcement yet.
why object not pass between controllers by redirect Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • why object not pass between controllers by redirect

    First controller:
    Code:
    public class PositionsController extends MultiActionController {
    ...
    	public ModelAndView add(HttpServletRequest request,
    			HttpServletResponse response, Position position) throws Exception {
    		try {
    			if (WebUtils.hasSubmitParameter(request, "update")) {
    				positionManager.remove(position.getId());
    			}
    			validate(position);
    			if (bindingResult.hasErrors()) {
    				logger.error("Error binding:\n" + bindingResult.getAllErrors());
    				ModelAndView mav = new ModelAndView("position", bindingResult
    						.getModel());
    				return fillMAV(mav);
    			}
    			position = finishCreatePosition(position);
    			if (WebUtils.hasSubmitParameter(request, "add")) {
    				return create(request, response);
    			} else {
    				String mode = request.getParameter("mode");
    				request.setAttribute("position", position);
    				ModelAndView mav = new ModelAndView(
    						"redirect:/employers/fromPosition.htm?mode=" + mode,
    						"position", position);
    				return mav;
    			}
    		} catch (Exception expObj) {
    			logger.error(expObj.getMessage(), expObj);
    			return new ModelAndView("position");
    		}
    	}
    second controller:
    Code:
    public class EmployersController extends MultiActionController {
    ...
    public ModelAndView fromPosition(HttpServletRequest request,
    			HttpServletResponse response) throws Exception {
    		logger.info("request.position=" + request.getParameter("position"));
    ..
    result of log:
    Code:
    .String;@15ef3e7}
    Sep 11, 2009 8:43:28 PM com.mycompany.myproject.web.controllers.EmployersController fromPosition
    INFO: request.position=null
    Why in the second controller passed object (position) is null ?

  • #2
    A redirect is client side and you can only add objects to the model which are simple (like strings, integers) because those can be encoded into the redirect url. With an object like yours that is not possible.

    Comment


    • #3
      Originally posted by Marten Deinum View Post
      A redirect is client side and you can only add objects to the model which are simple (like strings, integers) because those can be encoded into the redirect url. With an object like yours that is not possible.
      OK, I use session object to pass objects position between controllers. But I think that it's NOT good solution because every controllers (about 50 controllers) in the my application HAS access to this session object. What about incapsulation ?
      I need to only EmployerControllers to has access to the position object
      Code:
      public class PositionsController extends MultiActionController {
      ...
      
      	public ModelAndView add(HttpServletRequest request,
      			HttpServletResponse response, Position position) throws Exception {
      		try {
      			if (WebUtils.hasSubmitParameter(request, "update")) {
      				positionManager.remove(position.getId());
      			}
      			validate(position);
      			if (bindingResult.hasErrors()) {
      				logger.error("Error binding:\n" + bindingResult.getAllErrors());
      				ModelAndView mav = new ModelAndView("position", bindingResult
      						.getModel());
      				return fillMAV(mav);
      			}
      			position = finishCreatePosition(position);
      			if (WebUtils.hasSubmitParameter(request, "add")) {
      				return create(request, response);
      			} else {
      				String mode = request.getParameter("mode");
      				ModelAndView mav = new ModelAndView(
      						"redirect:/employers/fromPosition.htm?mode=" + mode);
      				WebUtils.setSessionAttribute(request, "position", position);
      				return mav;
      			}
      		} catch (Exception expObj) {
      			logger.error(expObj.getMessage(), expObj);
      			return new ModelAndView("position");
      		}
      	}

      Code:
      public class EmployersController extends MultiActionController {
      ...
      	public ModelAndView fromPosition(HttpServletRequest request,
      			HttpServletResponse response) throws Exception {
      		Position position = (Position) WebUtils.getSessionAttribute(request,
      				"position");
      		addPositionList(position);
      		Employer employer = (Employer) bindingResult.getModel().get(
      				"commandName");
      		employer.setPositionList(positionList);
      		ModelAndView mav = new ModelAndView("employer", bindingResult
      				.getModel());
      		return fillMAV(mav);
      	}

      Comment


      • #4
        If you need wizard style functionality use spring web flow...

        Comment

        Working...
        X