Validation Bean Is Not Validating

Asked

Viewed 172 times

0

Hi.

I would like to create a program, where I write a task and it would save it in a database. With this I have a class called, which asks for the description to have at least 5 characters

package br.com.domain;

import javax.validation.constraints.Size;

public class Tarefa {

    private int id;

    @Size(min=5, message="Deve ter pelo menos 5 caracteres")
    private String descricao;

    private boolean finalizado;

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }

    public String getDescricao() {
        return descricao;
    }
    public void setDescricao(String descricao) {
        this.descricao = descricao;
    }
    public boolean isFinalizado() {
        return finalizado;
    }
    public void setFinalizado(boolean finalizado) {
        this.finalizado = finalizado;
    }




}

The.jsp form is the field to enter the job description

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="prefix"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <form:errors path="tarefa.descricao" cssStyle="color:red"/>
    <h3>Adicionar Tarefas</h3>
    <form action="adcionaTarefa" method="post">
        Descrição:</br>
        <textarea rows="5" cols="100" name="descricao"></textarea>
        <input type="submit" value="Adcionar">
    </form>

</body>
</html>

And Sistemacontroller.java serves to make your action, if the description is valid it will go to a page saying that the task was successfully added, if not, back to the form page with the error message.

package br.com.Sistema.Controller;

import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import br.com.domain.Tarefa;

@Controller
@RequestMapping("/sistema")
@Validated
public class SistemaController {
    @RequestMapping("/index")
    public ModelAndView teste() {
        ModelAndView mav = new ModelAndView("tarefa/teste");
        return mav;
    }

    @RequestMapping("/formulario")
    public ModelAndView formulario() {
        ModelAndView mav = new ModelAndView("tarefa/formulario");
        return mav;
    }

    @RequestMapping("/adcionaTarefa")
    public ModelAndView adciona(@Valid Tarefa tarefa, BindingResult result) {
        if(result.hasFieldErrors()) {
            ModelAndView mav = new ModelAndView("tarefa/formulario");
            return mav;
        }
        ModelAndView mav = new ModelAndView("tarefa/adcionada");
        return mav;
    }

}

The problem: The Controller is not doing the job of validating the Task, so it goes to the successfully saved task page, regardless of the description, even if I write nothing in the field, what I do to make the program "validate" the task?

his pom.xml:


<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>br.com.Sistema</groupId>
  <artifactId>SistamaMaven</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.0</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
       <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.4</version>
        <configuration>
          <warSourceDirectory>WebContent</warSourceDirectory>
          <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
      </plugin>
    </plugins>
  </build>

  <properties>
        <spring.version>4.2.5.RELEASE</spring.version>
 </properties>

 <dependencies>     
    <!-- DEPENDÊNCIAS DO 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>
    </dependency>

<dependency>
    <groupId>javax.validation</groupId>
    <artifactId>validation-api</artifactId>
    <version>2.0.1.Final</version>
</dependency>


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


<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-validator-annotation-processor</artifactId>
  <version>6.0.11.Final</version>
</dependency>


<dependency>
    <groupId>javax.el</groupId>
    <artifactId>javax.el-api</artifactId>
    <version>3.0.1-b06</version>
</dependency>


<dependency>
    <groupId>org.glassfish.web</groupId>
    <artifactId>javax.el</artifactId>
    <version>2.2.6</version>
</dependency>

    <dependency>
        <groupId>taglibs</groupId>
        <artifactId>standard</artifactId>
        <version>1.1.2</version>
   </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
 </dependencies>    

</project>

1 answer

0

It seems to me a problem of configuration of spring Validator, try to change the settings as below:

Configuration by xml:

<mvc:annotation-driven />
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>

Utilizing @Configuration:

@Configuration
public class ValidatorConfig {
    @Bean
    public javax.validation.Validator localValidatorFactoryBean() {
        return new LocalValidatorFactoryBean();
    }   
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.