在 SimpleformController 上重定向

发布于 2024-11-08 06:09:02 字数 2382 浏览 0 评论 0原文

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

我尝试将 handlerequest 更改为 private void 方法 displayForm 并从 formBackingObject 方法调用它,该方法工作正常get 并根据需要正确调用 onSubmit 方法。

但唯一的问题是,在调用 displayForm 私有 void 方法时,我无法重定向页面以登录,因为 formBackingObject 方法没有响应对象,也无法使用 return private 方法 displayForm 中的 ModelView 作为 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;
}

I have a simpleformController which needs to handle request get and post both. While gets the user needs to be redirected to login screen which works fine if handlerequest is used but onSubmit does not get called while posting the page?? Any idea why?

I have tried changing the handlerequest to a private void method displayForm and called it from formBackingObject method which works fine while get and also calls the onSubmit method correctly as required.

But only problem is while calling displayForm a private void method I am unable to redirect the page to login as formBackingObject method does not have reponse object nor i can use return ModelView from private method displayForm as formBackingObject returns 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;
}

Original method: does not allow post to be handle by 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;
}

New method: To load form, but user should be forced to login (redirected to login if login is correct user can see this form) before coming to this form. This method is called from formBackingObject method to load the form but does not have response object for redirection to login page. Following method does not work

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

々眼睛长脚气 2024-11-15 06:09:02

您好,我终于可以在到达主页调用之前重定向到登录页面。我重写 isFormSubmission 并使用 showForm 而不是 handlerrequest 或从 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

返回成功视图
}

Hi i could finally redirect to login page before reaching the main page call. I override isFormSubmission and used showForm instead of handlerequest or any private method called from formBackingObject. Heres the code

@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

return successview
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文