Spring mvc Spring MVC验证-避免回发
我想验证Spring3MVC表单。当一个元素无效时,我想用一条验证消息重新显示表单。到目前为止,这相当简单。问题是,当用户在无效提交后点击刷新时,我不想让他们发布,我想让他们得到。这意味着我需要从表单POST(提交)执行重定向,以重新显示带有验证消息的表单(表单通过POST提交) 我认为最好的方法是使用SessionAttributeStore.retrieveAttribute来测试表单是否已经在用户的会话中。如果是,请使用存储表单,否则请创建新表单Spring mvc Spring MVC验证-避免回发,spring-mvc,validation,Spring Mvc,Validation,我想验证Spring3MVC表单。当一个元素无效时,我想用一条验证消息重新显示表单。到目前为止,这相当简单。问题是,当用户在无效提交后点击刷新时,我不想让他们发布,我想让他们得到。这意味着我需要从表单POST(提交)执行重定向,以重新显示带有验证消息的表单(表单通过POST提交) 我认为最好的方法是使用SessionAttributeStore.retrieveAttribute来测试表单是否已经在用户的会话中。如果是,请使用存储表单,否则请创建新表单 这听起来对吗?有更好的方法吗?您的问题不清
这听起来对吗?有更好的方法吗?您的问题不清楚,但听起来您的GET和POST操作映射到了同一个处理程序。在这种情况下,您可以执行以下操作:
if ("POST".equalsIgnoreCase(request.getMethod())) {
// validate form
model.addAttribute(form);
return "redirect:/me.html";
}
model.addAttribute(new MyForm());
return "/me.html";
在JSP中,检查表单上是否有任何错误,并根据需要显示。您的问题不清楚,但听起来GET和POST操作映射到了同一个处理程序。在这种情况下,您可以执行以下操作:
if ("POST".equalsIgnoreCase(request.getMethod())) {
// validate form
model.addAttribute(form);
return "redirect:/me.html";
}
model.addAttribute(new MyForm());
return "/me.html";
在JSP中,检查表单上是否有任何错误,并根据需要显示。这种方法称为PRG(POST/REdirect/GET)设计模式,我几天前将其解释为答案之一:
希望有帮助:)这种方法被称为PRG(POST/REdirect/GET)设计模式,我几天前解释了它,作为答案之一:
希望有帮助:)为了解决这个问题,我在一篇文章重定向后将Errors对象存储在会话中。然后,我把它放回模型中。这里有一些漏洞,但它应该在99.999%的时间内工作
public class ErrorsRedirectInterceptor extends HandlerInterceptorAdapter {
private final static Logger log = Logger.getLogger(ErrorsRedirectInterceptor.class);
private final static String ERRORS_MAP_KEY = ErrorsRedirectInterceptor.class.getName()
+ "-errorsMapKey";
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response,
Object handler, ModelAndView mav)
throws Exception
{
if (mav == null) { return; }
if (request.getMethod().equalsIgnoreCase(HttpMethod.POST.toString())) {
// POST
if (log.isDebugEnabled()) { log.debug("Processing POST request"); }
if (SpringUtils.isRedirect(mav)) {
Map<String, Errors> sessionErrorsMap = new HashMap<String, Errors>();
// If there are any Errors in the model, store them in the session
for (Map.Entry<String, Object> entry : mav.getModel().entrySet()) {
Object obj = entry.getValue();
if (obj instanceof Errors) {
if (log.isDebugEnabled()) { log.debug("Adding errors to session errors map"); }
Errors errors = (Errors) obj;
sessionErrorsMap.put(entry.getKey(), errors);
}
}
if (!sessionErrorsMap.isEmpty()) {
request.getSession().setAttribute(ERRORS_MAP_KEY, sessionErrorsMap);
}
}
} else if (request.getMethod().equalsIgnoreCase(HttpMethod.GET.toString())) {
// GET
if (log.isDebugEnabled()) { log.debug("Processing GET request"); }
Map<String, Errors> sessionErrorsMap =
(Map<String, Errors>) request.getSession().getAttribute(ERRORS_MAP_KEY);
if (sessionErrorsMap != null) {
if (log.isDebugEnabled()) { log.debug("Adding all session errors to model"); }
mav.addAllObjects(sessionErrorsMap);
request.getSession().removeAttribute(ERRORS_MAP_KEY);
}
}
}
}
公共类错误DirectInterceptor扩展HandlerInterceptorAdapter{
私有最终静态记录器log=Logger.getLogger(ErrorsDirectInterceptor.class);
私有最终静态字符串错误\u MAP\u KEY=ErrorsDirectInterceptor.class.getName()
+“-errorsMapKey”;
@凌驾
public void postHandle(HttpServletRequest请求、HttpServletResponse响应、,
对象处理程序(ModelAndView mav)
抛出异常
{
如果(mav==null){return;}
if(request.getMethod().equalsIgnoreCase(HttpMethod.POST.toString())){
//职位
if(log.isDebugEnabled(){log.debug(“处理POST请求”);}
if(SpringUtils.isRedirect(mav)){
Map sessionErrorsMap=新HashMap();
//如果模型中存在任何错误,请将其存储在会话中
对于(Map.Entry:mav.getModel().entrySet()){
Object obj=entry.getValue();
如果(obj实例错误){
if(log.isDebugEnabled()){log.debug(“将错误添加到会话错误映射”);}
错误=(错误)obj;
sessionErrorsMap.put(entry.getKey(),错误);
}
}
如果(!sessionErrorsMap.isEmpty()){
request.getSession().setAttribute(ERRORS\u MAP\u KEY,sessionErrorsMap);
}
}
}else if(request.getMethod().equalsIgnoreCase(HttpMethod.GET.toString())){
//得到
if(log.isDebugEnabled(){log.debug(“处理GET请求”);}
映射sessionErrorsMap=
(映射)request.getSession().getAttribute(错误\u映射\u键);
if(sessionErrorsMap!=null){
if(log.isDebugEnabled()){log.debug(“将所有会话错误添加到模型”);}
mav.addAllObjects(sessionErrorsMap);
request.getSession().removeAttribute(错误\u映射\u键);
}
}
}
}
为了解决这个问题,我在帖子重定向后将Errors对象存储在会话中。然后,我把它放回模型中。这里有一些漏洞,但它应该在99.999%的时间内工作
public class ErrorsRedirectInterceptor extends HandlerInterceptorAdapter {
private final static Logger log = Logger.getLogger(ErrorsRedirectInterceptor.class);
private final static String ERRORS_MAP_KEY = ErrorsRedirectInterceptor.class.getName()
+ "-errorsMapKey";
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response,
Object handler, ModelAndView mav)
throws Exception
{
if (mav == null) { return; }
if (request.getMethod().equalsIgnoreCase(HttpMethod.POST.toString())) {
// POST
if (log.isDebugEnabled()) { log.debug("Processing POST request"); }
if (SpringUtils.isRedirect(mav)) {
Map<String, Errors> sessionErrorsMap = new HashMap<String, Errors>();
// If there are any Errors in the model, store them in the session
for (Map.Entry<String, Object> entry : mav.getModel().entrySet()) {
Object obj = entry.getValue();
if (obj instanceof Errors) {
if (log.isDebugEnabled()) { log.debug("Adding errors to session errors map"); }
Errors errors = (Errors) obj;
sessionErrorsMap.put(entry.getKey(), errors);
}
}
if (!sessionErrorsMap.isEmpty()) {
request.getSession().setAttribute(ERRORS_MAP_KEY, sessionErrorsMap);
}
}
} else if (request.getMethod().equalsIgnoreCase(HttpMethod.GET.toString())) {
// GET
if (log.isDebugEnabled()) { log.debug("Processing GET request"); }
Map<String, Errors> sessionErrorsMap =
(Map<String, Errors>) request.getSession().getAttribute(ERRORS_MAP_KEY);
if (sessionErrorsMap != null) {
if (log.isDebugEnabled()) { log.debug("Adding all session errors to model"); }
mav.addAllObjects(sessionErrorsMap);
request.getSession().removeAttribute(ERRORS_MAP_KEY);
}
}
}
}
公共类错误DirectInterceptor扩展HandlerInterceptorAdapter{
私有最终静态记录器log=Logger.getLogger(ErrorsDirectInterceptor.class);
私有最终静态字符串错误\u MAP\u KEY=ErrorsDirectInterceptor.class.getName()
+“-errorsMapKey”;
@凌驾
public void postHandle(HttpServletRequest请求、HttpServletResponse响应、,
对象处理程序(ModelAndView mav)
抛出异常
{
如果(mav==null){return;}
if(request.getMethod().equalsIgnoreCase(HttpMethod.POST.toString())){
//职位
if(log.isDebugEnabled(){log.debug(“处理POST请求”);}
if(SpringUtils.isRedirect(mav)){
Map sessionErrorsMap=新HashMap();
//如果模型中存在任何错误,请将其存储在会话中
对于(Map.Entry:mav.getModel().entrySet()){
Object obj=entry.getValue();
如果(obj实例错误){
if(log.isDebugEnabled()){log.debug(“将错误添加到会话错误映射”);}
错误=(错误)obj;
sessionErrorsMap.put(entry.getKey(),错误);
}
}
如果(!sessionErrorsMap.isEmpty()){
request.getSession().setAttribute(ERRORS\u MAP\u KEY,sessionErrorsMap);
}
}
}else if(request.getMethod().equalsIgnoreCase(HttpMethod.GET.toString())){
//得到
if(log.isDebugEnabled(){log.debug(“处理GET请求”);}
映射sessionErrorsMap=
(映射)request.getSession().getAttribute(错误\u映射\u键);
if(sessionErrorsMap!=null){
if(log.isDebugEnabled()){log.debug(“将所有会话错误添加到模型”);}
mav.addAllObjects(sessionErrorsMap);