1

I created a model with a recursive relation, but when I tray to show it in a form:select in a JSP view, it returns an error about datatype conversions, how can I show recursive model in a JSP view?

I'm using Java 1.8 and spring-core 4.1.

When I show a database row, that the recursive field is null, it works, but when I try to show a row with the recursive field filled with a valid value, it doesn't works and generate the error.

this is the model field from the class Category

@OneToOne(fetch=FetchType.LAZY, cascade=CascadeType.ALL)
@JoinColumn(nullable=true)
private Category subCategory;

my controller send to the JSP view, a Category object named "category"

modelAndView.addObject("category", category);

in the view I'm showing the object by a form:select component

<form:select path="subCategory.id" 
             id="category_subCategory"
             multiple="false"
             cssClass="form-control">
  <form:option value="null">-</form:option>
  <form:options items="${categories}"
                itemValue="id"
                itemLabel="name"/>
</form:select>

Instead of displaying the component, it's breaking and generating the error below:

Jun 26, 2019 4:03:09 PM org.apache.catalina.core.ApplicationDispatcher invoke SEVERE: Servlet.service() for servlet [jsp] threw exception java.lang.NumberFormatException: For input string: "null" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)...

Jun 26, 2019 4:03:09 PM org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/ecommerce] threw exception [javax.servlet.ServletException: javax.servlet.jsp.JspException: org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type @javax.persistence.Id @javax.persistence.GeneratedValue java.lang.Integer for value 'null'; nested exception is java.lang.NumberFormatException: For input string: "null"] with root cause java.lang.NumberFormatException: For input string: "null"...

1 Answer 1

0

Well, I am doing a project in Java with Spring and I have to create a folder called Converters and and create a converter of all the domain classes that I have. I need to create two converters one from string to domain classe and viceversa

An exameple of this code is:

package converters;

import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import domain.Application;

@Component
@Transactional
public class ApplicationToStringConverter implements Converter<Application, String> {

    @Override
    public String convert(final Application application) {
        String result;

        if (application == null)
            result = null;
        else
            result = String.valueOf(application.getId());

        return result;
    }

}

-----------------------------AND---------------------------

package converters;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import repositories.ApplicationRepository;
import domain.Application;

@Component
@Transactional
public class StringToApplicationConverter implements Converter<String, Application> {

    @Autowired
    ApplicationRepository   applicationRepository;


    @Override
    public Application convert(final String text) {
        Application result;
        int id;

        try {
            if (StringUtils.isEmpty(text))
                result = null;
            else {
                id = Integer.valueOf(text);
                result = this.applicationRepository.findOne(id);
            }
        } catch (final Throwable oops) {
            throw new IllegalArgumentException(oops);
        }

        return result;
    }

}

---------Application class----------

@Entity
@Access(AccessType.PROPERTY)
public class Application extends DomainEntity {

    private Date        moment;
    private String      explication;
    private String      urlCode;


    @NotNull
    public Date getMoment() {
        return this.moment;
    }

    public void setMoment(final Date moment) {
        this.moment = moment;
    }

    @NotNull
    @SafeHtml(whitelistType = SafeHtml.WhiteListType.NONE)
    public String getExplication() {
        return this.explication;
    }

    public void setExplication(final String explication) {
        this.explication = explication;
    }

    @NotNull
    @URL
    @SafeHtml(whitelistType = SafeHtml.WhiteListType.NONE)
    public String getUrlCode() {
        return this.urlCode;
    }

    public void setUrlCode(final String urlCode) {
        this.urlCode = urlCode;
    }

    public Date getSubmitMoment() {
        return this.submitMoment;
    }

    public void setSubmitMoment(final Date submitMoment) {
        this.submitMoment = submitMoment;
    }

}

---- Repository class ---------

package repositories;

import java.util.Collection;
import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import domain.Application;

@Repository
public interface ApplicationRepository extends JpaRepository<Application, Integer> {

    @Query("select a from Application a where a")
    public Collection<Application> getAllApplications();

}
Sign up to request clarification or add additional context in comments.

5 Comments

in this case, what would be the Application and ApplicationRepository class? If you want to check the structure, I leave here the download link of the project. 1drv.ms/u/s!AgY6X8zeuYO6ggx_fEASxUwINdPZ?e=NTgGXF
I have added my domian Application class and ApplicationRepository
Sorry for the delay in answering, I gotta check only now, I did not understand your answer, I'm a beginner in java.
This error appear because the framework need to know how to convert your model that it is an object in a string and viceversa, it is to say, when you print the select you put item id and value but the framework need to know how to convert Category like a string this is my similar like my converter ApplicationToString
and the when you send the formulary it is probably that you catch failed to convert string in an object because the framework need to know how to convert the id (that is a string in the formulary) in your object Category, this is my converter StringToApplication. Application is my model object. My ApplicationRepository is only a interface where I do some queries to my database.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.