【发布时间】:2018-04-04 09:38:52
【问题描述】:
我有这个页面,老师可以邀请学生加入平台。这工作得很好,但现在我想在同一页面上实现多个邀请。简而言之,现在有一种表格可以一次邀请 1 名学生,但我想将其扩展到 5 名学生的一次发布方法。
这是邀请1名学生的页面:
<div class="container">
<div class="wrapper">
<form class="form-activate" th:action="@{/invite}" method="post" th:object="${user}">
<h2 class="form-activate-heading" th:text="#{invite.title}">Nodig een student uit</h2>
<p th:text="#{invite.email}">Vul hier het e-mailadres in van de student die je wil uitnodigen:</p>
<div class="form-group">
<input type="text" name="email" id="email" class="form-control input-lg" data-validation="email"
placeholder="Email" tabindex="2" th:attr="placeholder=#{general.email}"/>
</div>
<div class="row">
<div class="col-xs-6 col-sm-6 col-md-6">
<input type="submit" class="btn btn-secondary" value="Invite" th:attr="value=#{invite.enter}"/>
</div>
</div>
</form>
</div>
现在我尝试为 2 个用户修改此页面:
<div class="container">
<div class="wrapper">
<form class="form-activate" th:action="@{/invite}" method="post">
<h2 class="form-activate-heading" th:text="#{invite.title}">Nodig een student uit</h2>
<p th:text="#{invite.email}">Vul hier het e-mailadres in van de student die je wil uitnodigen:</p>
<div class="form-group" th:object="${user}">
<input type="text" name="email" th:field="*{email}" class="form-control input-lg" data-validation="email"
placeholder="Email" tabindex="2" th:attr="placeholder=#{general.email}"/>
</div>
<div class="form-group" th:object="${user}">
<input type="text" name="email" th:field="*{email}" class="form-control input-lg" data-validation="email"
placeholder="Email" tabindex="2" th:attr="placeholder=#{general.email}"/>
</div>
<div class="row">
<div class="col-xs-6 col-sm-6 col-md-6">
<input type="submit" class="btn btn-secondary" value="Invite" th:attr="value=#{invite.enter}"/>
</div>
</div>
</form>
</div>
然后在我的 post 方法中,我尝试这样做:
@RequestMapping(value="/invite", method = RequestMethod.POST)
public ModelAndView SendInvite(ModelAndView modelAndView, @ModelAttribute User user1, @ModelAttribute User user2, BindingResult bindingResult, RedirectAttributes redirectAttributes, HttpServletRequest request){
List<User> students = new ArrayList<>();
students.add(user1);
students.add(user2);
for (User user : students) {
但页面甚至不再加载,这是我的 get 方法:
@RequestMapping(value="/invite", method = RequestMethod.GET)
public ModelAndView showInvitePage(ModelAndView modelAndView, @ModelAttribute User user){
return modelAndView;
}
这给了我以下错误:
Servlet.service() 用于路径 [] 上下文中的 servlet [dispatcherServlet] 引发异常 [请求处理失败;嵌套异常是 org.thymeleaf.exceptions.TemplateProcessingException: 在执行处理器 'org.thymeleaf.spring4.processor.attr.SpringInputGeneralFieldAttrProcessor' (teacher/invite:20)] 时出错,根本原因 java.lang.IllegalStateException:Bean 名称“用户”的 BindingResult 和普通目标对象都不能用作请求属性 在 org.springframework.web.servlet.support.BindStatus.(BindStatus.java:144)
有没有可能我正在尝试做的事情?
编辑:我正在尝试实施 Aleksandrs 给我的解决方案,但 Spring 抛出错误:
@RequestMapping(value="/invite", method = RequestMethod.GET)
public ModelAndView showInvitePage(ModelAndView modelAndView){
List<String> emailList = new ArrayList<>();
modelAndView.addObject(emailList);
return modelAndView;
}
@RequestMapping(value="/invite", method = RequestMethod.POST)
public ModelAndView SendInvite(ModelAndView modelAndView, List<String> emailList){
System.out.println(emailList.get(0));
尽管我已经在 Get 方法中实例化了 emailList,但我收到以下错误:
嵌套异常是 org.springframework.beans.BeanInstantiationException: 无法实例化 [java.util.List]: Specified class is an interface] 根本原因 org.springframework.beans.BeanInstantiationException: 无法实例化 [java.util.List]: 指定的类是一个接口
EDIT2:
终于成功了,感谢 Aleksandrs 的帮助!解决方案:
@RequestMapping(value="/teacher/invite", method = RequestMethod.GET)
public ModelAndView showInvitePage(ModelAndView modelAndView){
logger.info("Entered showInvitePage function");
List<String> email = new ArrayList<>();
modelAndView.addObject("email", email);
return modelAndView;
}
@RequestMapping(value="/invite", method = RequestMethod.POST)
public ModelAndView SendInvite(ModelAndView modelAndView, @AuthenticationPrincipal User currentUser, @RequestParam List<String> email){
email.forEach(address -> {
// send the invites
});
还有邀请页面:
<form class="form-activate" th:action="@{/invite}" method="post" >
<p th:text="#{invite.email}">Vul hier het e-mailadres in van de student die je wil uitnodigen:</p>
</div>
<div class="form-group">
<input type="text" name="email" id="email" class="form-control input-lg" data-validation="email"
placeholder="Email" tabindex="2" th:attr="placeholder=#{general.email}"/>
</div>
<div class="form-group">
<input type="text" name="email" id="email2" class="form-control input-lg" data-validation="email"
placeholder="Email" tabindex="2" th:attr="placeholder=#{general.email}"/>
</div>
<div class="form-group">
<input type="text" name="email" id="email3" class="form-control input-lg" data-validation="email"
placeholder="Email" tabindex="2" th:attr="placeholder=#{general.email}"/>
</div>
<div class="row">
<div class="col-xs-6 col-sm-6 col-md-6">
<input type="submit" class="btn btn-secondary" value="Invite" th:attr="value=#{invite.enter}"/>
</div>
</div>
</form>
【问题讨论】:
标签: java spring spring-mvc spring-boot thymeleaf