您必须使用控制器在jsp上显示错误消息
这是控制器的代码
@RequestMapping(value = "/addUrl", method = RequestMethod.POST)
public String addUrl(WebRequest request) {
logger.info("Calling Add Filters in POST mode");
String serverUrl = request.getParameter("serverUrl");
logger.info("ServerUrl" + serverUrl);
if(serverUrl!=null)
{
mongoService.setServerUrl(serverUrl);
}
else
{
//show message on UI if serverUrl is null
}
return "redirect:config.do";
}
很遗憾地说,但我对春天来说是全新的,所以不知道这样做?。提前感谢
答案 0 :(得分:1)
使用ModelMap
,
@RequestMapping(value = "/addUrl", method = RequestMethod.POST)
public String addUrl(WebRequest request,ModelMap map) {
logger.info("Calling Add Filters in POST mode");
String serverUrl = request.getParameter("serverUrl");
logger.info("ServerUrl" + serverUrl);
if(serverUrl!=null)
{
mongoService.setServerUrl(serverUrl);
}
else
{
String errormsg="sorry un expected error has occured"
map.put("errMsg",errormsg)
}
return "redirect:config.do";
}
也可以使用
完成redirectAttributes.addFlashAttribute("errMsg",errormsg);
redirectAttributes.addAttribute
从您的属性中构造请求参数,并使用请求参数重定向到所需的页面
在你的jsp中,
可以使用EL ${errMsg}
答案 1 :(得分:1)
它在redirectAttributes上显示错误 我想在用户输入密码错误
时在我的登录屏幕上显示错误消息我的代码在下面给出; -
@RequestMapping(value="/login", method= RequestMethod.POST)
public ModelAndView login(@RequestParam(value= "name") String name, @RequestParam(value= "password") String password,ModelMap m) throws SQLException{
String uname= name;
String pass= password;
Connection connection = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/sbank","root","root");
Statement statement = (Statement) connection.createStatement();
ResultSet resultSet = (ResultSet) statement.executeQuery("select * from account where name='"+uname+"' and password='"+password+"'");
if(resultSet.next()){
return new ModelAndView("home","command",resultSet);
}
else{
String message="Sorry UserName Or Password Is Wrong";
ModelAndView model= new ModelAndView();
redirectAttributes.addFlashAttribute("error", message);
model.setViewName("redirect:/Index.jsp");
return model;
}
}
答案 2 :(得分:0)
您可以使用RedirectAttributes,如下所示:
@RequestMapping(value = "/failure")
public ModelAndView loginFailure(final RedirectAttributes redirectAttributes) {
String message = "Invalid username or password!";
ModelAndView model = new ModelAndView();
redirectAttributes.addFlashAttribute("error", message);
// model.addObject("error",message);
model.setViewName("redirect:/login");
return model;
}
在视图中,您只需将其显示如下:
<c:if test="${not empty error}">
<div class="alert alert-danger"><c:out value="${error}"/></div>
</c:if>
希望这个帮助