2014-10-29 51 views
1
上一個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"; 
} 

遺憾地說,但我顯示在一個jsp的錯誤消息完全新的彈簧,不知道該怎麼呢??在此先感謝

回答

0

使用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}

+0

個感謝會告訴你結果在某一時刻 – edward 2014-10-29 06:58:58

+0

歡迎您:)快樂幫 – 2014-10-29 07:21:39

+0

//的serverUrl是輸入框的ID它應該工作正確嗎?但它不起作用。 – edward 2014-10-29 07:42:30

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> 

希望這有助於

+0

謝謝我會在代碼中嘗試它並告訴你結果:) – edward 2014-10-29 06:58:37

0

它顯示redirectAttributes 我想告訴我的登錄屏幕錯誤信息錯誤,當用戶inser密碼錯誤

我的代碼如下; -

@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; 
    } 
}