我无法在 jsp 页面中收到错误消息。实际上,我试图检查数据库中的用户名和密码是否正确。我从 dao 抛出数据异常并在控制器处捕获检查以下代码
简单表单控制器
public ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response,Object command,BindException errors) throws Exception {
log.info("onSubmit handleRequest method"+request.getParameter("username"));
boolean isError=false;
String username="",password="",errMessage="";
username=request.getParameter("username");
password=request.getParameter("password");
UserBean ubean=null;
try{
ubean=userservice.chkUsername(username,password);
}catch(DataException ex){
errMessage=ex.getMessage();
isError=true;
//throw ex;
}
if(isError==true){
System.out.println("errMessage handleRequest method"+errMessage);
errors.reject(errMessage);
return showForm(request, response, errors);
}
log.info("After shownform method called");
HttpSession session=request.getSession(true);
session.setAttribute("User",ubean);
return new ModelAndView("UserPage");
}
public ModelAndView showForm(HttpServletRequest request, HttpServletResponse response,BindException errors) throws Exception {
//Displaying Count size
log.info("Show form Error Count"+errors.getErrorCount()+getFormView());
return new ModelAndView(getFormView());
}
我试图在 jsp 页面中以两种方式显示,即
Jsp页面: -
1)
<spring:hasBindErrors name="UserBean">
<h3>You have errors in your input!</h3>
<font color="red">
<c:forEach items="${errors.allErrors}" var="error">
<spring:message code="${error.code}" text="${error.defaultMessage}"/>
</c:forEach>
</font>
</spring:hasBindErrors>
2) <form:form action="userpage.htm" method="post" onsubmit="javascript:fun()">
<tr>
<form:errors path="*"/>
--------
--------
</form:form>
我试图在jsp中显示以上两种方式。但最后我没有得到任何东西。上面的代码有什么问题吗。请帮助我