How to use Localdate in Vraptor4?

Asked

Viewed 91 times

1

I am trying to make a simple application in Vraptor 4.

In an Entity, I have two attributes, one of the kind LocalDate and another of the kind LocalTime. I added the Vraptor Java 8 plugin, which should convert the data to the above types, but this does not happen.

I looked in the plugin and have nothing teaching how to use it. It seems that the converters are not even being activated. How should I use this plugin? I must create a convert even using the plugin?

The pom.xml:

<properties>
        <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>br.com.caelum</groupId>
            <artifactId>vraptor</artifactId>
            <version>4.2.0-RC5</version>
        </dependency>
        <dependency>
            <groupId>br.com.caelum.vraptor</groupId>
            <artifactId>vraptor-java8</artifactId>
            <version>4.0.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.5</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.11</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.3.3.Final</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>my-tasks</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <compilerArguments>
                        <parameters />
                    </compilerArguments>
                </configuration>
            </plugin>
        </plugins>
    </build>

My Entity:

@RequestScoped
@Entity
public class Task {
    @Id
    @GeneratedValue
    private Long id;

    @Basic(optional = false)
    @Column(nullable = false, length = 50)
    private String name;

    @Basic(optional = false)
    @Column(nullable = false, columnDefinition = "DATE")
    private LocalDate date;

    @Basic(optional = true)
    @Column(nullable = true, columnDefinition = "TIME")
    private LocalTime hour;

    @Basic(optional = true)
    @Column(nullable = true, length = 100)
    private String details;

    @Basic(optional = false)
    @Column(nullable = false)
    private boolean finished = false;

    @Basic(optional = false)
    @ManyToOne
    private User user;
//getters and setters
}

My form:

<form method="POST" action="${linkTo[TaskController].register}">
    <c:if test="${not empty errorMessage}">
        <c:import url="../commons/msgErrorAlert.jsp" />
    </c:if>

    <!--tarefa-->
    <div>
        <label for="task.name">Tarefa</label>
        <input type="text" name="task.name" value="${task.name}">
        <span>${errors.from('task.name')}</span>
    </div>
    <!--data-->
    <div>
        <label for="task.date">Data</label>
        <input type="date" name="task.date" value="${task.date}"> 
        <span>${errors.from('task.date')}</span>
    </div>
    <!--horario-->
    <div>
        <label for="task.hour">Horário</label>
        <input type="time" name="task.hour" value="${task.hour}"> 
        <span>${errors.from('task.hour')}</span>
    </div>
    <!--observações-->
    <div>
        <label for="task.details">Observações</label>
        <textarea name="task.details" rows="3">${task.details}</textarea>
        <span>${errors.from('task.details')}</span>
    </div>

    <div>
        <button type="submit">
            Cadastrar
        </button>
        <button type="reset">
            Limpar
        </button>
    </div>
</form>

My controller:

@Controller
@Path("/tarefa")
public class TaskController {

    private Result result;
    private ISession session;
    private ITaskValidator validator;
    private ITaskService taskService;

    @Deprecated
    public TaskController() {
        this(null, null, null, null);
    }

    @Inject
    public TaskController(Result result, ISession session, ITaskValidator validator, ITaskService taskService) {
        this.result = result;
        this.session = session;
        this.validator = validator;
        this.taskService = taskService;
    }

    @Get("/cadastro")
    public void register() {
        result.include("title", "Cadastro de tarefa");
    }

    @Post("/cadastro")
    public void register(Task task) {
        validator.validate(task);
        validator.onErrorRedirectTo(this).register();

        task.setFinished(false);
        task.setUser(session.getUser());
        /*
        método ainda não implementado
        taskservice.createTask(task);
        */
    }   
}

The Validator:

public class TaskValidator implements ITaskValidator {

    private Validator validator;

    @Deprecated
    public TaskValidator() {
        this(null);
    }

    @Inject
    public TaskValidator(Validator validator) {
        this.validator = validator;
    }

    @Override
    public void validate(Task task) {
        validator.validate(task);

        //nome
        if(task.getName() == null || task.getName().length() < 5 || task.getName().length() > 50) {
            validator.add(new I18nMessage("task.name", "task.name.invalid"));
        }
        //data
        if(task.getDate() == null) {
            validator.add(new I18nMessage("task.date", "task.date.null"));
        }
        if(task.getDate() != null && task.getDate().isBefore(LocalDate.now())) {
            validator.add(new I18nMessage("task.date", "task.date.past"));
        }
        //horario
        if((task.getDate() != null && !task.getDate().isBefore(LocalDate.now())) && task.getHour().isBefore(LocalTime.now())) {
            validator.add(new I18nMessage("task.hour", "task.hour"));
        } 
        if(task.getDate() == null && task.getHour() != null) {
            validator.add(new I18nMessage("task.date", "task.hour.without.date"));
        }
        //observaçoes
        if(task.getDetails() != null && (task.getDetails().length() < 5 || task.getDetails().length() > 100)) {
            validator.add(new I18nMessage("task.details", "task.details.length"));
        }
    }

    @Override
    public <T> T onErrorRedirectTo(T controller) {
        return validator.onErrorRedirectTo(controller);
    }
}

When I send the data by the form, returns the warning task.getDate() == null, raised in the validator.

  • Your problem is persisting LocalDate and LocalTime of Entity in the database with JPA and you also use Vraptor or your problem is to make Vraptor understand the LocalTime and the LocalDate regardless of whether he is in an Entity? Or are the two things the problem? What version of Java, Hibernate, etc you are using and on which application server?

  • Hello Victor! My problem is to make Vraptor recognize Localdate and Localtime, nor did I get to the persist part of the object. I made a custom Validator for my class, and it returns me the error of a null data.

  • So put the code there, 'cause it could be some silly little thing.

  • It’s as if Vraptor didn’t even activate the plugin’s converter. I’m using Java 8, Vraptor 4.2.0-RC5, Vraptor plugin java-8 4.0.0.Final, Hibernate 5.3.3.Final and Wildfly 8.1

  • Edited topic Victor, I think that would be it.

1 answer

1


To solve this problem you have to use the plugin vraptor-javatime if you are using the package java.time.LocalDateTime or if using the jodatime, you have to add the vraptor plugin of jodatime.

<dependency>
    <groupId>br.com.caelum.vraptor</groupId>
    <artifactId>vraptor-javatime</artifactId>
    <version>4.1.0-RC3</version>
</dependency>

Browser other questions tagged

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