gpt4 book ai didi

java - 在 JSP View 中显示递归模型时如何修复 "Failed to convert from type java.lang.String to type @javax.persistence.Id"

转载 作者:太空宇宙 更新时间:2023-11-04 09:33:39 25 4
gpt4 key购买 nike

我创建了一个具有递归关系的模型,但是当我在 JSP View 中的 form:select 中显示它时,它返回有关数据类型转换的错误,如何在 JSP View 中显示递归模型?

我使用的是 Java 1.8 和 spring-core 4.1。

当我显示数据库行时,递归字段为 null,它可以工作,但是当我尝试显示递归字段填充了有效值的行时,它不起作用并生成错误。

这是类别类别中的模型字段

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

我的 Controller 向 JSP View 发送一个名为“category”的 Category 对象

modelAndView.addObject("category", category);

在 View 中,我通过 form:select 组件显示对象

<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>

它没有显示组件,而是破坏并生成以下错误:

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"...

最佳答案

嗯,我正在使用 Spring 用 Ja​​va 做一个项目,我必须创建一个名为 Converters 的文件夹,并创建我拥有的所有域类的转换器。我需要创建两个转换器,一个从字符串到域类,反之亦然

此代码的示例是:

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;
}

}

------------------------------并且-----------------------------

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;
}

}

---------应用程序类----------

@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;
}

}

---- 存储库类 ---------

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();

}

关于java - 在 JSP View 中显示递归模型时如何修复 "Failed to convert from type java.lang.String to type @javax.persistence.Id",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56775985/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com