如何在spring-boot中将下拉列表从一个实体类填充到另一个实体类?

7vux5j2d  于 2022-12-04  发布在  Spring
关注(0)|答案(1)|浏览(163)

Customer.java 文件

@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Customer {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)

private Long id;
private String name;
private String relative;
private String address;
private Long aadhar;
private Long contact;

public Long getAadhar() {
    return aadhar;
}
public void setAadhar(Long aadhar) {
    this.aadhar = aadhar;
}
@ManyToOne
@JoinColumn(name="town_name",insertable = false,updatable = false)
private Town town;
private String town_name;

public Long getId() {
    return id;
}
public void setId(Long id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getRelative() {
    return relative;
}
public void setRelative(String relative) {
    this.relative = relative;
}
public String getAddress() {
    return address;
}
public void setAddress(String address) {
    this.address = address;
}
public Town getTown() {
    return town;
}
public void setTown(Town town) {
    this.town = town;
}
public String getTown_name() {
    return town_name;
}
public void setTown_name(String town_name) {
    this.town_name = town_name;
}
public Long getContact() {
    return contact;
}
public void setContact(Long contact) {
    this.contact = contact;
}

}

Town.Java

@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Town {

    
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @Id
    private String townname;
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getTownname() {
        return townname;
    }
    public void setTownname(String townname) {
        this.townname = townname;
    }
}

CustomerController.java 文件

@Controller
public class CutomerController {
    
@Autowired 
private CutomerService customerService;

    
@GetMapping("/customer")
    public String findAllCustomers(Model model) {
    model.addAttribute("customers", customerService.findAllCustomers());
    return "customer";
}

@PostMapping("/customer/addnew")
public String addNew(Customer customer) {
    
    customerService.saveCustomer(customer);
    return "redirect:/customer";
}

}

TownController.java 文件

@Controller
public class TownController {

    @Autowired
    private TownService townService;

    @GetMapping("/town")
    public String findAllTowns(Model model) {

        model.addAttribute("towns", townService.findAllTown());
        return "town";
    }

}

我的customer.html文件

<!-- Multi Columns Form -->
    <form class="row g-3" th:action="@{/customer/addnew}" method="post">
        <div class="col-md-4">
                <label for="aadhar" class="form-label">Aadhar No.</label>
        <input type="number" min="0" max="999999999999" class="form-control" id="aadhar">
        </div>
                    
                                
            <div class="col-md-8">
        <label for="customername" class="form-label">Customer Name</label>
        <input type="text" class="form-control" id="name"onKeyup="this.value = this.value.toUpperCase()" required>
        </div>
        
                <div class="col-md-6">
            <label for="relative" class="form-label">S/O,D/O,C/O</label>
        <input type="text" class="form-control" id="relative"onKeyup="this.value = this.value.toUpperCase()" required>
        </div>
                            
                <div class="col-md-6">
        <label for="contact" class="form-label">Contact No.</label>
        <input type="number" max="9999999999" class="form-control" id="contact">
        </div>
                
                <div class="col-12">
        <label for="inputAddress5" class="form-label">Address</label>
        <input type="text" class="form-control" id="address"onKeyup="this.value = this.value.toUpperCase()" placeholder="1234 Main St" required>
        </div>
        <div class="col-md-4">
        <label for="inputTown" class="form-label" id="selecttown">Town/Area</label>
            <select class="form-control" id="selecttown" name="townname" th:field="*{townname}" required>
        <option selected>Choose...</option>
            <option th:each="town:${towns}" th:value="${town.towname}" th:text="${town.towname}"></option>
        </select>
        </div>
        
        <div class="text-center" style="margin-bottom:10px">
                <button type="submit" class="btn btn-primary mx-1 my-1">Submit</button>

我期望在客户模态表单的下拉列表中填充城镇的名称。但是我得到这个错误
Whitelabel Error Page此应用程序没有/error的显式Map,因此您将其视为回退。
11月30日星期三12:18:23 IST 2022出现意外错误(类型=Internal Server Error,状态=500)。模板解析期间出错(模板:“类路径资源[模板/客户.html]”)组织.thymeleaf.异常.模板输入异常:模板解析期间出错(模板:“类路径资源[模板/客户.html]”)
导致错误:org.attoparser.分析异常:执行处理器“org.thymeleaf.spring6.processor.SpringSelectFieldTagProcessor”期间出错(模板:“客户”-第473行,第70列)
原因:org.thymeleaf.异常。模板处理异常:执行处理器“org.thymeleaf.spring6.processor.SpringSelectFieldTagProcessor”期间出错(模板:“客户”-第473行,第70列)
导致错误的原因:java.lang. Bean名称'townname'的BindingResult和普通目标对象都不能用作请求属性

roejwanj

roejwanj1#

您引用了错误的变量。请按如下方式修改您的CustomerController类:

@Autowired 
private CutomerService customerService;
@Autowired
private TownService townService;

    
@GetMapping("/customer")
    public String findAllCustomers(Model model) {
    model.addAttribute("customers", customerService.findAllCustomers());
    model.addAttribute("towns", townService.findAllTown());
    return "customer";
}

@PostMapping("/customer/addnew")
public String addNew(Customer customer) {
    customerService.saveCustomer(customer);
    return "redirect:/customer";
}

相关问题