gpt4 book ai didi

spring - 如何在 Spring MVC simpleformcontroller 上添加错误?

转载 作者:行者123 更新时间:2023-12-02 06:25:22 25 4
gpt4 key购买 nike

我的 Spring MVC 2.5 应用程序遇到这个问题,我不知道该怎么办。

这是我的代码:

public class AddStationController extends SimpleFormController {
private SimpleStationManager stationManager;

protected ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response, Object command, BindException errors)
throws Exception {
StationDetails detail = (StationDetails) command;
//add to DB
int return = stationManager.addStation(detail);

//return value: 1 = successful,
// if not = unsuccessful

if(return != 1){
//how can I add error so that when I display my formview ,
//I could notify the user that saving to the db is not successful?
showform();
}
return new ModelAndView("redirect:" + getSuccessView());
}
}

如何在再次显示表单 View 时添加一些消息,以便告诉用户添加电台未成功?

如何在我的 jsp 中处理它?<​​/p>

最佳答案

我起初以为您可能想使用验证器,但我认为您可以执行以下操作:

public class AddStationController extends SimpleFormController {
private SimpleStationManager stationManager;

protected ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response, Object command, BindException errors)
throws Exception {
StationDetails detail = (StationDetails) command;
//add to DB
int return = stationManager.addStation(detail);

//return value: 1 = successful,
// if not = unsuccessful

if(return != 1){
//Account for failure in adding station
errors.reject("exception.station.submitFailure", "Adding the station was not successful");
showform(request, response, errors);
}
return new ModelAndView("redirect:" + getSuccessView());
}
}

然后在您的 JSP 中您可以执行以下操作:

<form:errors path="*">

然后您绑定(bind)的任何错误都会显示在那里。

关于spring - 如何在 Spring MVC simpleformcontroller 上添加错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3101148/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com