Java Spring中提交表单后的空对象(H2、SpringBoot、Thymeleaf、CRUD)
我正在创建一个Spring Boot应用程序,用户可以在其中创建许可证。用户可以键入名称、purchaseDate、renewalDate和expirationDate 我的问题是,当我试图保存数据时,它会为名称、purchaseDate、renewalDate和expirationDate返回null 我真的不知道该怎么办了,请帮忙。Model:license中的“//Comment”值是那些在执行post时得到null的值 型号:许可证Java Spring中提交表单后的空对象(H2、SpringBoot、Thymeleaf、CRUD),java,spring-boot,thymeleaf,h2,Java,Spring Boot,Thymeleaf,H2,我正在创建一个Spring Boot应用程序,用户可以在其中创建许可证。用户可以键入名称、purchaseDate、renewalDate和expirationDate 我的问题是,当我试图保存数据时,它会为名称、purchaseDate、renewalDate和expirationDate返回null 我真的不知道该怎么办了,请帮忙。Model:license中的“//Comment”值是那些在执行post时得到null的值 型号:许可证 @Entity @Table(name = "TBL_
@Entity
@Table(name = "TBL_LICENCES")
public class Licence {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long Id;
private String licenceName; // --> null after the post
@DateTimeFormat(pattern = "yyyy-MM-dd")
private String purchaseDate; // --> null after the post
@DateTimeFormat(pattern = "yyyy-MM-dd")
private String renewalDate; // --> null after the post
@DateTimeFormat(pattern = "yyyy-MM-dd")
private String expirationDate; // --> null after the post
public Licence() {}
public Licence(Long id, String licenceName, String purchaseDate, String renewalDate, String expirationDate) {
Id = id;
this.licenceName = licenceName;
this.purchaseDate = purchaseDate;
this.renewalDate = renewalDate;
this.expirationDate = expirationDate;
}
// Getter and Setter
}
@Service
public class LicenceService {
@Autowired
private LicenceRepository licenceRepository;
public List<Licence> getLicences() {
return (List<Licence>) licenceRepository.findAll();
}
public Optional<Licence> getLicenceById(Long id) {
return licenceRepository.findById(id);
}
public void addLicence(Licence licence) {
licenceRepository.save(licence);
}
public void updateLicence(Licence licence) {
licenceRepository.save(licence);
}
public void deleteLicenceById(Long id) {
licenceRepository.deleteById(id);
}
}
型号:Licenserepository
@Repository
public interface LicenceRepository extends CrudRepository<Licence, Long> { }
data.sql-->此操作有效,并显示数据
INSERT INTO
TBL_LICENCES (licence_name, purchase_date, renewal_date, expiration_date)
VALUES
('Test1', '2020-01-31', '2020-06-31', '2020-12-31'),
('Test', '2021-01-31', '2021-06-31', '2021-12-31');
属性:application.Properties
spring.h2.console.enabled=true
spring.datasource.platform=h2
spring.datasource.url=jdbc:h2:mem:<dbLicences>
spring.jpa.hibernate.ddl-auto=update
spring.h2.console.enabled=true
spring.datasource.platform=h2
spring.datasource.url=jdbc:h2:mem:
spring.jpa.hibernate.ddl auto=update
输入控件的名称
属性应与许可证
类中字段的名称匹配。当前,您的id
属性与字段名称匹配,但在提交表单时,它使用name
属性来构建请求参数
更新HTML以匹配以下内容:
<input type="text" class="form-control" id="licenceName" name="licenceName" />
licenceName
用@DateTimeFormat
注释。我想这是个错误,请也纠正一下。也许我是盲人,但我看不到任何粗体值我的意思是。
Lizenz Name
。div class=“form group”后面的每个标签都表示一个变量,并且在发布时变为null。
INSERT INTO
TBL_LICENCES (licence_name, purchase_date, renewal_date, expiration_date)
VALUES
('Test1', '2020-01-31', '2020-06-31', '2020-12-31'),
('Test', '2021-01-31', '2021-06-31', '2021-12-31');
spring.h2.console.enabled=true
spring.datasource.platform=h2
spring.datasource.url=jdbc:h2:mem:<dbLicences>
spring.jpa.hibernate.ddl-auto=update
<input type="text" class="form-control" id="licenceName" name="licenceName" />
@DateTimeFormat(pattern = "yyyy-MM-dd")
private String licenceName;