当前位置: 首页 > 知识库问答 >
问题:

在null上找不到属性或字段“email”

施飞雨
2023-03-14

我正在尝试从一个Boostrap模式中的表单发布。

这是我的表格:

            <form role="form" id="emailForm" action="#" th:action="@{/emailSubmission}" th:object="${university}" method="post">
                <div class="form-group">
                    <label for="emailID"><span class="glyphicon glyphicon-user"></span> Username</label>
                    <input type="text" class="form-control" id="emailID" th:value="*{email}" placeholder="Enter email"></input>
                </div>
                <button type="submit" value="Submit" id="submitButton"  class="btn btn-default btn-success btn-block" ><span class="glyphicon glyphicon-check"></span> Register</button>
            </form>
@Controller
public class RegistrationController {


    @RequestMapping(value = "/emailSubmission", method = RequestMethod.POST)
    public String registerEmail(@ModelAttribute("university") University uni, BindingResult result, Model model)
    {
        System.out.println(uni.getEmail());
        return "index";
    }
}
public class University {

    private String email;

    public University(){
    }

    public String getEmail(){
        return email;
    }

    public void setEmail(String email){
        this.email = email;
    }
} 
th:value="*{email}"

致:

th:field="*{email}"

给我“bean名称'university'的BindingResult或plain target object都不作为请求属性可用”错误。

共有1个答案

易祯
2023-03-14

您必须将university对象作为属性添加到控制器中的模型中:

@GetMapping(value = "/index") 
public String login(Model model) { 
    model.addAttribute("university", new University()); 
    return "index"; 
}
 类似资料: