【发布时间】:2021-02-18 23:35:03
【问题描述】:
我有一个包裹实体,我正在通过@PostMapping 填充字段。值 Volume 应该从一个方法中获取,该方法从构造函数中的 agruments 中获取值。
控制器类:
@Controller
public class Controller {
@Value("#{${listOfBases}}")
private List<String> listOfBases;
@Value("#{${listOfDestinations}}")
private List<String> listOfDestinations;
@Autowired
ParcelRepository parcelRepository;
@GetMapping("/register_parcel")
public String showParcelRegistrationForm(Model model) {
Parcel parcel = new Parcel();
model.addAttribute("parcel", parcel);
model.addAttribute("listOfBases", listOfBases);
model.addAttribute("listOfDestinations", listOfDestinations);
return "parcel/register_form_parcel";
}
@PostMapping("register_parcel")
public String registerParcel(@ModelAttribute("parcel") Parcel parcel) {
System.out.println(parcel);
parcelRepository.save(parcel);
return "user/user_registration_success_form";
}
}
包裹类别:
@Entity
public class Parcel {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
int id;
String length;
String width;
String height;
String weight;
String base;
String destination;
String creationDate;
String volume;
无参数构造函数:
public Parcel() {
creationDate = getCreationDateAndTime();
}
public Parcel(int id, String length, String width, String height, String weight, String base, String destination) {
this.id = id;
this.length = length;
this.width = width;
this.height = height;
this.weight = weight;
this.base = base;
this.destination = destination;
creationDate = getCreationDateAndTime();
volume = String.valueOf(calculateVolume(width, height, length));
}
Getter 和 Setter:
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getLength() {
return length;
}
public void setLength(String length) {
this.length = length;
}
public String getWidth() {
return width;
}
public void setWidth(String width) {
this.width = width;
}
public String getHeight() {
return height;
}
public void setHeight(String height) {
this.height = height;
}
public String getWeight() {
return weight;
}
public void setWeight(String weight) {
this.weight = weight;
}
public String getBase() {
return base;
}
public void setBase(String base) {
this.base = base;
}
public String getDestination() {
return destination;
}
public void setDestination(String destination) {
this.destination = destination;
}
public String getCreationDate() {
return creationDate;
}
public void setCreationDate(String creationDate) {
this.creationDate = creationDate;
}
public String getVolume() {
return volume;
}
public void setVolume(String volume) {
this.volume = volume;
}
private String getCreationDateAndTime() {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime now = LocalDateTime.now();
return dtf.format(now);
}
CalculateVolume 方法是问题的根源:
private int calculateVolume(String width, String height, String length) {
int w = Integer.parseInt(width);
int h = Integer.parseInt(height);
int l = Integer.parseInt(length);
return w * h * l;
}
但我的数据库中的值 volume 为空。甚至 System.out.println();在 calculateVolume 中不会在控制台上打印任何内容,但是当我在 main 中运行创建实例时,一切运行良好且花花公子。
关于我应该如何进行的任何想法,或者我的问题是否过于模糊?
谢谢
@Override
public String toString() {
return "Parcel{" +
"id=" + id +
", length='" + length + '\'' +
", width='" + width + '\'' +
", height='" + height + '\'' +
", weight='" + weight + '\'' +
", base='" + base + '\'' +
", destination='" + destination + '\'' +
", creationDate='" + creationDate + '\'' +
", volume='" + volume + '\'' +
'}';
}
}
【问题讨论】:
-
当你打印这个 System.out.println(parcel);在你的后期控制器中,你有音量的价值吗? (顺便说一句,我会用 @PostMapping("/register_parcel") 替换您的注释)
标签: spring spring-boot model-view-controller thymeleaf h2