【发布时间】:2014-02-05 03:04:18
【问题描述】:
我是 Spring MVC 的新手。我的 abc 页面只有一个提交按钮。单击提交按钮时,将调用位于 xyzController 内的 abcHandler。 只是想知道如何实现这种情况。
-- 如果发生错误,它应该返回一些消息。
-- 它应该留在同一页,对我来说是它的 abc 页。
我已经尝试过了。我面临的问题是,我收到警报消息“你必须有东西......”, 但它正在导航到错误页面意味着一个空白页面。这不应该发生。我想显示消息并留在同一页面上。 如何使用 Spring MVC 轻松实现。 请提出一些想法。
public class xyzController extends MultiActionController implements InitializingBean {
public ModelAndView abcHandler(HttpServletRequest request,HttpServletResponse response)throws Exception {
// session
HttpSession session = request.getSession(true);
String abc = "";
if(abc != ""){
}else{
String error = "You must have xyzzzzzz";
return new ModelAndView("2.0/error", "downloaderror", error);
}
return new ModelAndView();
}
}
我的error.jsp是这样的
<!DOCTYPE html>
<html>
<head>
<script src="abc/js/jquery-1.9.0.min.js"></script>
</head>
<body>
<c:if test="${not empty downloaderror}">
<script>
alert("You must have something...");
</script>
</c:if>
</body>
</html>
【问题讨论】:
-
您的
abc != ""比较将始终解析为true。将其更改为"".equals(abc)或abc.isEmpty()你知道它不能是null。 -
你说你正在提交一些东西。如果您使用的是表单,则应使用
BindingResult进行验证并通过<form:errors path="property" />显示错误
标签: java spring jsp spring-mvc