我希望我能清楚地解释我自己,因为我刚刚从 Spring 开始。
假设我有以下 bean:
public class UserAnswer {
private String answer;
public UserAnswer() {
}
public String getAnswer() {
return answer;
}
public void setAnswer(String answer) {
this.answer = answer;
}
这是一个重定向到我的视图的控制器方法。请注意传递给模型的参数
@RequestMapping(value="/usuarios/cambiar_estado", method=RequestMethod.POST)
public String bloquearDesbloquearUsuario(@RequestParam (value="usuarioPinIdHidden", required=false) String pinId, Model model){
try{
List<Question> questionList = servicioAcceso.getSecurityQuestionsByUser(Long.parseLong(pinId));
model.addAttribute("userAnswer", new UserAnswer());
model.addAttribute("questionList", questionList);
}catch(Exception e){
e.printStackTrace();
}
return "login/pregunta_seguridad";
}
这是呈现问题列表的视图 login/pregunta_seguridad.jsp。我需要将列表的每个元素绑定到 UserAnswer 类型的项目列表:
<c:url var="url" value="/usuarios/compare_answers"/>
<form:form modelAttribute="userAnswer" action="${url}" method="POST">
<c:if test="${not empty answerList}">
<table class="summary" style="border: none;">
<c:forEach var="question" items="${questionList}">
<tr>
<td colspan="2">${question.description}</td>
</tr>
<tr>
<form:input path="answer" id="answer"/>
</tr>
</c:forEach>
</table>
</c:if>
<div class="span-3 last">
<button type="submit">Confirm</button>
</div>
</form:form>
我需要一种获取 UserAnswer 列表的方法
@RequestMapping(value="/usuarios/compare_answers", method=RequestMethod.POST)
public String compareAnswer(Model model, UserAnswer userAnswer){
try{
//this is where I should receive a list of UserAnswer
}catch(Exception e){
e.printStackTrace();
}
return null;
}
我怎么能在春天做这样的事情?提前致谢!