Home > Back-end >  How to populate dropdown list from one entity class to another in spring-boot?
How to populate dropdown list from one entity class to another in spring-boot?

Time:11-30

Customer.java file


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


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



@Controller
public class TownController {

    @Autowired
    private TownService townService;

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

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

}

My customer.html file

<!-- Multi Columns Form -->
    <form  th:action="@{/customer/addnew}" method="post">
        <div >
                <label for="aadhar" >Aadhar No.</label>
        <input type="number" min="0" max="999999999999"  id="aadhar">
        </div>
                    
                                
            <div >
        <label for="customername" >Customer Name</label>
        <input type="text"  id="name"onKeyup="this.value = this.value.toUpperCase()" required>
        </div>
        
                <div >
            <label for="relative" >S/O,D/O,C/O</label>
        <input type="text"  id="relative"onKeyup="this.value = this.value.toUpperCase()" required>
        </div>
                            
                <div >
        <label for="contact" >Contact No.</label>
        <input type="number" max="9999999999"  id="contact">
        </div>
                
                <div >
        <label for="inputAddress5" >Address</label>
        <input type="text"  id="address"onKeyup="this.value = this.value.toUpperCase()" placeholder="1234 Main St" required>
        </div>
        <div >
        <label for="inputTown"  id="selecttown">Town/Area</label>
            <select  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  style="margin-bottom:10px">
                <button type="submit" >Submit</button>

i am expecting populate the names of towns in dropdown list of customer modal form. But i am getting this error

Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback.

Wed Nov 30 12:18:23 IST 2022 There was an unexpected error (type=Internal Server Error, status=500). An error happened during template parsing (template: "class path resource [templates/customer.html]") org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/customer.html]")

Caused by: org.attoparser.ParseException: Error during execution of processor 'org.thymeleaf.spring6.processor.SpringSelectFieldTagProcessor' (template: "customer" - line 473, col 70)

Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring6.processor.SpringSelectFieldTagProcessor' (template: "customer" - line 473, col 70)

Caused by: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'townname' available as request attribute

CodePudding user response:

Your are referencing wrong variable. Modify your CustomerController class like this:

@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";
}
  • Related