2013-05-15 26 views
0

我正在开发一个web应用程序。在那个应用程序中,我有一个基于id的三个搜索选项。如果ID是错误的,我想在搜索文本框中保留该ID,并且我想显示一条错误消息。我试着用春季发生错误时保留文本框的值

ModelAndView.addObject(“id”,value);

而且这工作正常,现在我想知道是否有更好的方法来做到这一点,因为假设我有一个大的形式,我想保留每个领域的价值比它将难以使用上述接近。请帮忙!

,我使用的ID搜索和名称都被这就是为什么我有尝试和catch块

这是我的JSP文件

html> 
    <head> 
     <link rel="stylesheet" type="text/css" href="./css/style.css" /> 
     <link rel="stylesheet" type="text/css" href="./bootstrap/css/bootstrap.css" /> 
     <link rel="stylesheet" type="text/css" href="./bootstrap/css/bootstrap.min.css" /> 
     <link rel="stylesheet" type="text/css" href="./bootstrap/css/bootstrap-responsive.css" /> 
     <link rel="stylesheet" type="text/css" href="./bootstrap/css/bootstrap-responsive.min.css" /> 
     <script> 
      function redirectToSearchResult(textBoxId){ 
       window.location.href= document.getElementById(textBoxId).name+'.htm?'+'id='+document.getElementById(textBoxId).value; 
      } 
     </script> 
    </head> 
    <body> 
     <div class="page-header"></div> 

     <div id="searchByBuyer"> 
      <label id="E_B_I"><h2>Buyer Search<h2></label><br> 
      <input type="text" id="S_B_B" class="text-box" name="searchByBuyer" value=${buyerId} ></input> 
      <input type="button" id="BuyerSearch" class="btn-custom" value="search" onclick="redirectToSearchResult('S_B_B')"/> 
     </div> 

     <div id="searchByCategory"> 
      <label id="E_C_I"><h2>Category Search<h2></label><br> 
      <input type="text" id="S_B_C" class="text-box" name="searchByCategory" value=${categoryId} ></input> 
      <input type="button" id="CategorySearch" class="btn-custom" value="search" onclick="redirectToSearchResult('S_B_C')"/> 
     </div> 

     <div id="searchByArticle"> 
      <label id="E_A_I"><h2>Article Search<h2></label><br> 
      <input type="text" id="S_B_I" class="text-box" name="searchByArticle" value=${articleId} ></input> 
      <input type="button" id="ArticleSearch" class="btn-custom" value="search" onclick="redirectToSearchResult('S_B_I')"/><br> 
     </div> 

     <br> 
     <label style="color:red; padding-left:45em; padding-top:15em"><h4>${error}</h4></label> 
    </body> 

</html> 

这我控制器

`

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.RequestParam; 
import org.springframework.web.servlet.ModelAndView; 

@Controller 
public class BuyerController { 

    @Autowired 
    private BuyerRepo buyerRepo; 

    @RequestMapping(value = "/searchByBuyer.htm", method = RequestMethod.GET) 
    public ModelAndView searchFields(@RequestParam(value = "id") String buyerId) throws InvalidIdException { 
     Buyer buyer = null; 
     ModelAndView buyerSearch = new ModelAndView("buyerSearch"); 
     ModelAndView errorView = ErrorView.getErrorView("buyerId", buyerId, "Enter a valid Buyer id!"); 

     try { 
      buyer = buyerRepo.getBuyer(Long.parseLong(buyerId)); 

     } catch (NumberFormatException e) { 
      buyer = buyerRepo.getBuyer(buyerId); 
      if (buyer == null) return errorView; 

     } catch (Exception e) { 
      buyerSearch = errorView; 
     } 

     buyerSearch.addObject("buyer", buyer); 
     return buyerSearch; 
    } 

} 

`

这是错误和视图类来创建参数

`

import org.springframework.web.servlet.ModelAndView; 

public class ErrorView { 

    public static ModelAndView getErrorView(String key, String value, String message) { 
     ModelAndView errorView = new ModelAndView("index"); 
     errorView.addObject("error", message); 
     errorView.addObject(key, value); 
     return errorView; 
    } 
} 
错误观点

`

回答

1

Spring对Web表单JSR 303豆验证的支持。 这种构建方式比一些自己的实现更容易使用。

您需要:

  • 命令对象会从表格中的所有值。每个字段都可以具有JSR 303-Bean-Validation注释以指示要执行的约束
  • 您需要具有(至少)两个参数的Web控制器方法:@Valid命令对象,BindingResult(必须是BindingResult之后的参数命令对象)
  • 在你的网络控制器的方法,你需要检查BindingResult,如果有故障,您需要渲染的形式再次
  • 在你的表格你需要使用form:errors显示错误(form:errors也可以显示特定字段的错误)
  • 您需要一些弹簧配置:<mvc:annotation-driven />(有更多可能的,但是这应该是足够的开始)
  • 你需要一个JSR 303豆验证libary,例如:Hibernate验证(这不是Hibernate的ORM)

但一个例子说明它是最好的:http://www.mkyong.com/spring-mvc/spring-3-mvc-and-jsr303-valid-example/