Jakub Madej
Jakub Madej

Reputation: 19

My Controller doesnt see parametr

I want to get first name by html template with i show it by method Get. I did controller and it doesnt't see name which I give by the html template. This controller should get this paramert from template. I have exeption on my browser There was an unexpected error (type=Bad Request, status=400). Required String parameter 'first_name' is not present

Please can u tell me where i did mistake ?

this is my service:

   public class ReadFamily {

@Autowired
    ChildRespository childRespository;


    private RestTemplate restTemplate;

    public ReadFamily(){
        restTemplate = new RestTemplate();
    }
public ChildForm findChild(String firstName){

    return restTemplate.getForObject("http://localhost:8080/findP/"+firstName,ChildForm.class);

}
    public String firstNameFormat(ChildForm childForm) {
        return childForm.getFirstName();
    }
    public String secondNameFormat(ChildForm childForm) {
        return childForm.getSecondName();
    }
    public String sexFormat(ChildForm childForm) {
        return childForm.getSex();
    }
    public String peselFormat(ChildForm childForm) {
        return childForm.getPesel();
    }
}

controller:

   @Autowired
    ReadFamily readFamily;

    @GetMapping("findP")
    public String findPerson(Model model){
        model.addAttribute("childForm",new ChildForm());
        return"Find";
    }
    @RequestMapping (value = "findPersonResult", method = RequestMethod.POST)
    public String findPerson(Model model,
                             @RequestParam ("first_name") String firstName) {
        System.out.println(firstName);
    ChildForm childInfo = readFamily.findChild(firstName);
            model.addAttribute("firstName",readFamily.firstNameFormat(childInfo));
            model.addAttribute("secondName",readFamily.secondNameFormat(childInfo));
            model.addAttribute("pesel",readFamily.peselFormat(childInfo));
            model.addAttribute("sex",readFamily.sexFormat(childInfo));
        return "Find";
    }

and template:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Find person</title>
</head>
<body>
<form  th:object="${childForm}" action="/findPersonResult" method="post">
    <input type="text" th:field="*{firstName}" placeholder="firstName"> <br/>
    <input type="submit" value="Find">
</form>
<h2>Persons</h2>
<form action="/findP" method="get">
    <div id="show" >
        <h1 th:text="${firstName} "></h1>
        <h1 th:text="${secondName} "></h1>
        <h1 th:text="${pesel} "></h1>
        <h1 th:text="${sex} "></h1>

    </div>
</form>
</body>
</html>

Upvotes: 0

Views: 42

Answers (1)

Alien
Alien

Reputation: 15878

Change

@RequestParam ("first_name") String firstName

to

@RequestParam ("firstName") String firstName

Upvotes: 1

Related Questions