在 SimpleformController 上重定向
我有一个 simpleformController ,它需要处理请求 get 和 post 两者。虽然获取用户需要重定向到登录屏幕,但如果使用 handlerequest
但在发布页面时不会调用 onSubmit
,则该屏幕可以正常工作?知道为什么吗?
我尝试将 handlerequest
更改为 private void
方法 displayForm
并从 formBackingObject
方法调用它,该方法工作正常get 并根据需要正确调用 onSubmit
方法。
但唯一的问题是,在调用 displayForm
私有 void 方法时,我无法重定向页面以登录,因为 formBackingObject
方法没有响应对象,也无法使用 return
作为 private
方法 displayForm
中的 ModelViewformBackingObject
返回 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您好,我终于可以在到达主页调用之前重定向到登录页面。我重写 isFormSubmission 并使用 showForm 而不是 handlerrequest 或从 formBackingObject 调用的任何私有方法。继承人的代码
返回成功视图
}
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
return successview
}