Java 在spring中发生错误时保留textbox的值
我正在开发一个网络应用程序。在该应用程序中,我有一个页面,其中有三个基于id的搜索选项。如果id错误,我希望在搜索文本框中保留id,并显示错误消息。我试过了 添加对象(“id”,值) 这很好,现在我想知道是否有更好的方法来实现这一点,因为假设我有一个大的表单,我想保留每个字段的值,而不是使用上述方法将很困难。请帮忙 我正在使用ID和名称搜索,这就是为什么我要尝试捕获块的原因 这是我的jsp文件Java 在spring中发生错误时保留textbox的值,java,spring,jsp,controller,Java,Spring,Jsp,Controller,我正在开发一个网络应用程序。在该应用程序中,我有一个页面,其中有三个基于id的搜索选项。如果id错误,我希望在搜索文本框中保留id,并显示错误消息。我试过了 添加对象(“id”,值) 这很好,现在我想知道是否有更好的方法来实现这一点,因为假设我有一个大的表单,我想保留每个字段的值,而不是使用上述方法将很困难。请帮忙 我正在使用ID和名称搜索,这就是为什么我要尝试捕获块的原因 这是我的jsp文件 html> <head> <link rel=
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>
`
这是用于创建带有参数的错误视图的error and view类
`
`Spring对web表单提供JSR303bean验证支持。 这种内置方式比某些自己的实现更易于使用 你需要:
- 从窗体获取所有值的命令对象。每个字段都可以有JSR303bean验证注释,以指示要强制执行的约束
- 您需要一个具有(至少)两个参数的web控制器方法:
命令对象,@Valid
(BindingResult
必须是命令对象后面的参数)BindingResult
- 在web控制器方法中,需要检查
,如果失败,则需要再次呈现表单BindingResult
- 在表单中,您需要使用
来显示错误(form:errors
也可以显示特定字段的错误)form:errors
- 您需要一些spring配置:
(还有更多的可能,但这应该足够开始了) - 您需要一个JSR303bean验证库,例如:HibernateValidator(这不是HibernateORM)
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;
}
}