0

我已经在我的模型中注释了我的字段,并在我的 post 控制器上使用了 @Valid 注释,但它似乎没有执行任何验证(result.errors 为空)

有什么想法可能导致这种情况吗?

基于Java的配置:

@FeatureConfiguration
public class MvcFeatures {

    @Feature
    public MvcAnnotationDriven annotationDriven() {
        return new MvcAnnotationDriven();
    }

    @Feature
    public MvcResources css() {
        return new MvcResources("/css/**", "/css/");
    }

    @Feature
    public MvcResources js() {
        return new MvcResources("/js/**", "/js/");
    }

    @Feature
    public MvcViewControllers viewController() {
        return new MvcViewControllers("/", "home");
    }

    @Feature
    public ComponentScanSpec componentScan() {
        return new ComponentScanSpec("com.webapp")
                .excludeFilters(new AnnotationTypeFilter(Configuration.class),
                        new AnnotationTypeFilter(FeatureConfiguration.class));
    }

}



控制器:

@Controller
public class UserController extends AppController
{
    static Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private IUserService userService;

    @RequestMapping(value = "/registration", method = RequestMethod.GET)
    public ModelAndView get()
    {

        ModelAndView modelAndView = new ModelAndView(Consts.MODEL_RESISTER_PAGE);
        modelAndView.addObject("user", new User());

        return modelAndView;
    }

    @RequestMapping(value = "/registration", method = RequestMethod.POST)
    public ModelAndView post(@Valid User user, BindingResult result)
    {

        if (result.hasErrors())
        {
            ModelAndView modelAndView = new ModelAndView(
                    Consts.MODEL_RESISTER_PAGE);
            modelAndView.addObject("user", result.getModel());
            return modelAndView;
        }
        else
        {
            // SAVE HERE
            ModelAndView modelAndView = new ModelAndView(
                    Consts.MODEL_RESISTER_PAGE);
            return modelAndView;
        }
    }
}



模型:

@Entity
public class User implements Serializable
{
    /**
     * 
     */
    private static final long serialVersionUID = -5232533507244034448L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @NotEmpty
    @Size(min=2, max=15)
    private String firstname;

    @NotEmpty
    @Size(min=2, max=15)
    private String surname;

    @NotEmpty
    @Email
    private String email;

    @NotEmpty
    @Size(min=6, max=10)
    private String password;

    public Long getId()
    {
        return id;
    }

    public void setId(Long id)
    {
        this.id = id;
    }

    public String getFirstname()
    {
        return firstname;
    }

    public void setFirstname(String firstname)
    {
        this.firstname = firstname;
    }

    public String getSurname()
    {
        return surname;
    }

    public void setSurname(String surname)
    {
        this.surname = surname;
    }

    public String getEmail()
    {
        return email;
    }

    public void setEmail(String email)
    {
        this.email = email;
    }

    public String getPassword()
    {
        return password;
    }

    public void setPassword(String password)
    {
        this.password = password;
    }
}



add.html(使用 thymeleaf 视图解析器)

<form id="registrationForm" action="#"
        th:action="@{/registration}" th:object="${user}" method="post"
        class="clearfix">

         <legend>Registration</legend>

        <div class="input">
          <input type="text" th:field="*{firstname}"
            placeholder="Firstname" th:class="${#fields.hasErrors('firstname')}? 'fieldError'"
             />
        </div>

        <div class="input">
          <input type="text" th:field="*{surname}" placeholder="Surname" />
        </div>

        <div class="input">
          <input type="text" th:field="*{email}" placeholder="Email" />
        </div>

        <div class="input">
          <input type="password" th:field="*{password}"
            placeholder="Password" />
        </div>

        <div class="clearfix">
          <input type="submit" class="btn btn-success btn-large" value="Register" />
        </div>

      </form>



绒球:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>ConsumerApplicaton</groupId>
    <artifactId>ConsumerApplicaton</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <repositories>
        <repository>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
            <id>springsource-milestone</id>
            <name>Spring Framework Milestone Repository</name>
            <url>http://maven.springframework.org/milestone</url>
        </repository>
    </repositories>

    <dependencies>
        <!-- spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-oxm</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- thymeleaf -->
        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf</artifactId>
            <version>${thymeleaf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf-spring3</artifactId>
            <version>${thymeleaf.version}</version>
        </dependency>

        <!-- persistence -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>3.1.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>3.6.9.Final</version>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>4.2.0.Final</version>
        </dependency>

        <!-- json, xml, atom -->
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>${jackson-mapper-asl.version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>${jaxb-api.version}</version>
            <scope>runtime</scope>
        </dependency>

        <!-- DB -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.18</version>
            <scope>runtime</scope>
        </dependency>

        <!-- logging -->

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${org.slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.0.1</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>${org.slf4j.version}</version>
            <scope>runtime</scope>
        </dependency>

        <!-- Various -->
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib-nodep</artifactId>
            <version>${cglib.version}</version>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

    <build>
        <finalName>ConsumerApplicaton</finalName>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <properties>
        <!-- VERSIONS -->
        <!-- <spring.version>3.1.0.RELEASE</spring.version> -->
        <spring.version>3.1.0.M1</spring.version>
        <cglib.version>2.2.2</cglib.version>

        <thymeleaf.version>2.0.11</thymeleaf.version>

        <jackson-mapper-asl.version>1.9.5</jackson-mapper-asl.version>
        <jaxb-api.version>2.2.6</jaxb-api.version>

        <!-- VERSIONS - persistence -->
        <mysql-connector-java.version>5.1.18</mysql-connector-java.version> <!-- latest version on: 02.10.2011 - http://dev.mysql.com/downloads/connector/j/ -->
        <hibernate.version>4.1.0.Final</hibernate.version>

        <!-- VERSIONS - logging -->
        <org.slf4j.version>1.6.4</org.slf4j.version>
    </properties>

</project>
4

1 回答 1

0

@Feature不再支持。这是在 Spring 3.1.0.M1 中实现的,然后在 3.1.0.M2 中删除以支持@Enable样式注释。

这里最好的方法是移到 3.1.1.RELEASE 并使用@Configuration和朋友。

于 2012-08-23T16:55:58.403 回答