我的控制器没有看到参数

时间:2018-07-07 14:12:37

标签: html spring-mvc thymeleaf

我想通过html模板获取名字,并通过Get方法显示它。 我做了控制器,但没有看到html模板给出的名称。 该控制器应从模板获取此参数。我的浏览器There was an unexpected error (type=Bad Request, status=400). Required String parameter 'first_name' is not present

请你告诉我我在哪里做错了吗?

这是我的服务

   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();
    }
}

控制器:

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

和模板:

<!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>

1 个答案:

答案 0 :(得分:1)

更改

@RequestParam ("first_name") String firstName

@RequestParam ("firstName") String firstName