当前位置: 首页 > 知识库问答 >
问题:

Spring验证异常:BindException

施子民
2023-03-14

向addUser控制器提交表单时发生异常

获取窗体的控制器

    @RequestMapping(method = RequestMethod.GET, value = "register")
public String addUser(Model model) {
    if (!model.containsAttribute("wrongLink")) {
        System.out.println("not wrong Link");
        model.addAttribute(new UserBean());
    } else {
        System.out.println("wrong Link");
    }
    return "user/register";
}
@RequestMapping(method = RequestMethod.POST, value = "register")
public String addUser(@Valid UserBean userBean, Model model,
        RedirectAttributes redirectAttrs, BindingResult bindingResult) {
    System.out.println("in addUser form");
    if (bindingResult.hasErrors()) {
        System.out.println("ERROR in user Form");
        return "user/edit";
    }
    return "redirect:/users/" + user.getDisplayName();
}
import org.hibernate.validator.constraints.Email;
public class UserBean {

private Integer id;

@Email(message = "Not a vaild Email Address")
private String email;
//getter and setter
}
<div id="container">
    <sf:form method="POST" modelAttribute="userBean">
            <div class="form">
                <sf:input path="email" type="text" id="email"
                    placeholder="email address" />
                <sf:errors path="email" cssClass="error" />
                <input class="send submit" type="submit" name="submit_first"
                    id="submit_first" value="" />
            </div>
    </sf:form>
</div>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans     
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"
xmlns:tx="http://www.springframework.org/schema/tx">

<context:component-scan base-package="com.example" />

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="host" value="${smtp.host}" />
    <property name="port" value="${smtp.port}" />
    <property name="username" value="${smtp.username}" />
    <property name="password" value="${smtp.password}" />
    <property name="javaMailProperties">
        <props>
            <!-- Use SMTP transport protocol -->
            <prop key="mail.transport.protocol">smtp</prop>
            <!-- Use SMTP-AUTH to authenticate to SMTP server -->
            <prop key="mail.smtp.auth">true</prop>
            <!-- Use TLS to encrypt communication with SMTP server -->
            <prop key="mail.smtp.starttls.enable">true</prop>
            <prop key="mail.debug">true</prop>
        </props>
    </property>
</bean>

<bean id="alertMailMessage" class="org.springframework.mail.SimpleMailMessage">
    <property name="from" value="${alertMailMessage.from}" />
    <property name="to" value="${alertMailMessage.to}" />
    <property name="subject" value="${alertMailMessage.subject}" />
</bean>

<bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <!-- one of the properties available; the maximum file size in bytes -->
    <property name="maxUploadSize" value="1000000" />
</bean>


<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/WEB-INF/pages/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>

<mvc:resources mapping="/**" location="/resources/" />

<mvc:annotation-driven />

<bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>/WEB-INF/properties/database.properties</value>
            <value>/WEB-INF/properties/smtp.properties</value>
        </list>
    </property>
</bean>

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driver}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.user}" />
    <property name="password" value="${jdbc.password}" />
</bean>

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="com.example.model" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
            <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
        </props>

    </property>
</bean>


<tx:annotation-driven transaction-manager="transactionManager" />

<bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

为什么会出现此异常,输入无效电子邮件而不是验证必须执行。

共有1个答案

刘昌翰
2023-03-14

控制器的addUser方法中,bindingresult需要紧跟在bean之后:

public String addUser(@Valid UserBean userBean, BindingResult bindingResult,
                      Model model, RedirectAttributes redirectAttrs) {
    ...
}
 类似资料:
  • 我正在测试带注释的javax验证。它们在应用程序中工作良好,方法参数上有注释。但是当我试图通过手动构建验证工厂来测试它们时 我得到以下错误。 javax。验证。ValidationException:HV000183:无法初始化“javax”。艾尔。“表达工厂”。检查类路径上是否有EL依赖项,或者改用ParameterMessageInterpolator 收到这个错误后,我在gradle文件中添

  • 下面是我的代码: 这里有个例外: 对象“users”中字段“salary”上的字段错误:拒绝值[null];代码[NotNull.Users.Salary,NotNull.Salary,NotNull.java.lang.Integer,NotNull];参数[org.springframework.context.support.defaultmessageSourceResolvable:代码

  • 我正在测试一个具有预期异常的方法。我还需要验证在抛出异常后是否调用了一些清理代码(在模拟对象上),但看起来该验证被忽略了。这是代码。我正在使用 Junit 来验证预期的异常。 似乎完全被忽略了。无论我在中使用什么方法,我的测试都通过了,这不是我想要的。 你知道为什么会发生这种情况吗?

  • 我正在使用Spring MVC和Hibernate Validator4.2.0。在/web-inf/classes/validationmessages.properties中的类路径上有一个validationmessages.properties: 这是JavaConfig中的一个bean: 使用CustomValidator进行验证工作良好(我手动插入错误消息,而不使用消息源),但是对于绑

  • 我正在创建一个Spring Boot项目来测试我的rest API(非常非常小的项目)。该项目包含三个文件。 1)app.java(主程序)2)AppConfig(配置文件)3)主控制器(简单Rest控制器) 我添加了Spring-boot-starter-parent(1.4.0.release)和spring-boot-starter-web作为依赖项。 但是在执行时,会得到如下所示的异常。

  • 在dynamodb表上运行batchGetItem函数时获取ValidationException。我在密钥列表下同时提供哈希密钥和范围密钥。以下是请求和响应。 请求: 答复: