簡體   English   中英

在MultiActionController上進行Spring驗證,BindException:如何使用自定義對象名稱代替命令

[英]Spring Validation on MultiActionController, BindException : how to use custom object name instead of command

對不起,我的問題很愚蠢,英語不好。 我試圖使用Spring MVC在新的Developed Web中對用戶更改密碼菜單進行一些驗證。

我遵循如何在MultiActionController中執行Spring驗證中的說明?

我寫這段代碼:

public ModelAndView hanldeBindException(HttpServletRequest request, HttpServletResponse response, ServletRequestBindingException bindingException) {
    BindException bindException = (BindException) bindingException.getRootCause();
    ModelMap modelmap = new ModelMap();
    ChangePasswordForm cpForm = new ChangePasswordForm();
    cpForm.setUsername(SessionHelper.getUsername());
    modelmap.addAttribute("ChangePasswordForm", cpForm);
    return new ModelAndView("changepassword").addAllObjects(bindException.getModel()).addObject("ChangePasswordForm", cpForm);
}     

在我的jsp中:

<form:form action="update.htm" commandName="ChangePasswordForm" >
    <form:errors path="*" cssClass="errorblock" element="div" />
    <table  class="form">
        <tr>
            <td class="col1"><label>User Name :</label></td>
            <td class="col2"><form:label path="username" >${ChangePasswordForm.username}</form:label></td>
        </tr>
        <tr>
            <td><label>Old Password :</label></td>
            <td><form:password path="oldPassword" id="oldPassword"></form:password>
                <form:errors path="oldPassword" cssClass="error" /></td>
        </tr>
        <tr>
            <td><label>New Password :</label></td>
            <td><form:password path="newPassword"></form:password>
            <form:errors path="newPassword" cssClass="error" /></td>
        </tr>
        <tr>
            <td><label>Confirm New Password :</label></td>
            <td><form:password path="confirmNewPassword"></form:password>
            <form:errors path="confirmNewPassword" cssClass="error" /></td>
        </tr>
        <tr>
            <td colspan="2"><input type="submit" value="submit" ></td>
        </tr>
    </table>
</form:form>    

但是當我嘗試像空字段這樣的負面測試時,它在屏幕上沒有出現任何錯誤。 當我嘗試打印時

System.out.println("UserController::hanldeBindException::1" + bindException.getObjectName());

它顯示對象名稱為“ command”,而不是我在jsp中的命令名稱為“ ChangePasswordForm”。 當我嘗試將該commandName更改為“ command”時,它可以工作。 但是我只是很好奇,是否有某種方法可以在BindException類中設置setObjectName,或者您還有另一種使用自定義名稱而不是默認“命令”對象名稱的方法?

更新

我的驗證有效,因為我將commandName更改為“ command”,如下所示:

<form:form action="update.htm" commandName="command">
   <!-- Form Field and Error define here -->
</form:form>

當然還要對我的控制器進行一些調整。 如果我將其更改為其他名稱,它將不再起作用。

我使用Spring Framework 3.0.2 RELEASE。

一個簡單的答案是,很可能您是以某種形式(使用AbstractCommandController或其他方式)擴展BaseCommandController,在這種情況下,您可能應該覆蓋“ getCommandName()”和“ getCommand()”方法。 但是我無法從您提供的代碼中分辨出來。

您正在使用哪個版本的spring? 從3.0+開始,“命令”的概念相對於基類已經過時了。 而是使用基於注釋的控制器和模型屬性:

@Controller
@SessionAttributes({"changePasswordForm"})
public class MyController {
    @InitBinder
    public void initBinder(WebDataBinder binder) {
       binder.setValidator(new ChangePasswordFormValidator());
    }

    @ModelAttribute("changePasswordForm")
    public ChangePasswordForm createChangePasswordForm() {
      ...
    }


    @RequestMapping(...)
    public ModelAndView controllerMethod(
          @Valid @ModelAttribute changePasswordForm ChangePasswordForm) {
      ...
    }
}

在調用控制器方法之前,這應該會自動驗證您的表單,然后退回到調用頁面。

在您的驗證器中,您將在validate方法中提供錯誤消息代碼:

public void validate(Object target, Errors errors) {
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "username", "username.required");
}

然后,您將確保在提供給spring的任何MessageSource中,“ username.required”是鍵。

表單標簽應與您擁有的標簽完全一樣。

這是控制器的彈簧參考手冊:

http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/mvc.html#mvc-controller

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM