SimpleformController上的重定向

时间:2011-05-18 15:27:51

标签: java spring-mvc

我有simpleformController需要处理请求获取和发布两者。虽然获取用户需要重定向到登录屏幕,如果使用handlerequest但在发布页面时没有调用onSubmit,则工作正常知道为什么吗?

我尝试将handlerequest更改为private void方法displayForm,然后从formBackingObject方法调用它,该方法正常工作,同时调用onSubmit方法可以根据需要正确使用。

但唯一的问题是在调用displayForm私有void方法时我无法将页面重定向到登录,因为formBackingObject方法没有响应对象,也不能使用返回ModelView private方法displayForm formBackingObject返回Object

@Override
protected Object formBackingObject(HttpServletRequest request) throws Exception {
  if (logger.isDebugEnabled()) {
    logger.debug("In formBackingObject()... ");
  }
  Object obj = request.getSession().getAttribute(CURRENT_FORM);
  if (obj != null) {
    if (logger.isDebugEnabled()){
      logger.debug("update profile details form already exist, returning...");
    }
    return obj;
  }

  MyForm Form = new MyForm();

  request.getSession().setAttribute(CURRENT_FORM, Form);

  displayForm(request);

  return Form;
}

原始方法:不允许onSubmit

处理帖子
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {

  mv = new ModelAndView(new RedirectView(request.getContextPath() + "login.htm"));

  RequestCache requestCache = new HttpSessionRequestCache();
  requestCache.saveRequest(request, response);

  mv.addObject("c_name",getPManager().getCName());

  request.getSession().setAttribute("login_from_pac","yes");
  return mv;
}

新方法:要加载表单,但在进入此表单之前,应强制用户登录(如果登录正确,则重定向到登录,用户可以看到此表单)。从formBackingObject方法调用此方法以加载表单,但没有用于重定向到登录页面的响应对象。以下方法不起作用

private void displayForm(HttpServletRequest request) throws Exception {

  HttpServletResponse response = null; -->
  ModelAndView mav = null;
  mav=new ModelAndView(new RedirectView(request.getContextPath() + "login.htm"));

  RequestCache requestCache = new HttpSessionRequestCache();
  requestCache.saveRequest(request, response);

  //mav.addObject("c_name",getPManager().getCName());

  request.getSession().setAttribute("c_name", getPManager().getCName());

  request.getSession().setAttribute("login_from_pac","yes");
  //return mav;
}

1 个答案:

答案 0 :(得分:0)

您好我终于可以在到达主页面调用之前重定向到登录页面。我重写isFormSubmission并使用showForm而不是handlerequest或从formBackingObject调用的任何私有方法。继承人的代码

@Override
protected Object formBackingObject(HttpServletRequest request) throws Exception {           
    if (logger.isDebugEnabled()) {
        logger.debug("In formBackingObject()... ");
        }
    Object obj = request.getSession().getAttribute(CURRENT_FORM);
    if (obj != null) {
                    }
        return obj;
    }

    MyForm Form = new MyForm(); 

    request.getSession().setAttribute(CURRENT_FORM, Form);


    return Form;
}

/**
 * Submit form
 * @param HttpServletRequest
 * @param HttpServletResponse
 * @param Object
 * @param BindException
 * @throws Exception 
 */
@Override
protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception {

    if (logger.isDebugEnabled()){
    logger.debug("onSubmit");
}



        ModelAndView mav;
        mav = new ModelAndView("formactivated");

        MyForm Form = (MyForm) command;


        try{                             
//do logic here

                                                    }

@Override
protected ModelAndView showForm(HttpServletRequest request, HttpServletResponse response, BindException errors)throws Exception {
    if (logger.isDebugEnabled()) {
        logger.debug("In showForm ... ");
    }

    if(errors.hasErrors()){
        if (logger.isDebugEnabled()) {
            logger.debug("In showForm  has errors... ");
        }
    ModelAndView mav;
    mav = new ModelAndView("formactivated");    
    mav.addAllObjects(errors.getModel());           
    return mav;
    }


    other login here

返回成功视图
    }