1
我需要从我的视图中传递控制器中的对象列表(其中包含自身的引用)。我经历了这么多的联系,但没有得到满意的解决方案。 以下是我的场景和代码片段。 我有UI与问题和答案列表。每个问题都可以有子问题,这也是主要问题的子问题。我如何将复杂对象列表从我的视图(jsp)传递给控制器
我想将完整的数据作为问题列表传递给我的控制器。 我的用户界面是这样的: https://i.stack.imgur.com/yA8hN.png
型号有:
问题型号:
public class Question {
private String question;
private String question_type;
private List<String> answers;
private List<Question> subQuestions;
.. getter and setter and constructors...
}
public class SurveyData {
private List<Question> data = new ArrayList<Question>();
... getter and setter and constructors...
}
控制器:
@RequestMapping(value = "save", method = RequestMethod.POST)
public ModelAndView saveData(HttpServletRequest request, HttpServletResponse response, @ModelAttribute("surveyData") SurveyData data) {
// code to save the data....
}
在我的jsp我这是获取所有的JavaScript方法数据并保存在隐藏的输入值中。
JSP /查看:
<form id="surveyForm" modelAttribute="surveyData" method="POST">
<table style="height: 200px;" border="0" width="70" cellspacing="0">
<tbody>
<tr style="height: 50px;">
<td>
<h2>
<span style="color: #00ccff;"> ADD NEW CALL </span>
</h2>
</td>
</tr>
<tr style="height: 30px;">
<td>
<div id="question-container">
<p style="padding-left: 1000px;">
<span style="color: #999999;"><a style="color: #999999;"
title="+ ADD NEW QUESTION" href="javascript:void(0);"
onclick="javascript:addNewQuestion();">+ ADD NEW QUESTION</a></span>
</p>
</div>
</td>
</tr>
<tr style="height: 70px;">
<td>
<input type="hidden" id="surveyData" name="surveyData" value="" />
<input type = "submit" value = "Save" onclick="javascript:saveData();"/>
<input type = "submit" value = "Cancel" onclick="javascript:cancel();"/>
</td>
</tr>
</tbody>
</table>
</form>
<script>
function saveData() {
var surveyData = {};
var questions = [];
// code to fetch the data and save it in questions array
.
.
.
surveyData.data = questions;
$("#surveyData").val(surveyData);
$("#surveyForm").submit();
}
</script>
我没有收到在modelAttribuite参数的任何数据。 如何将这些数据传递给我的控制器?
在此先感谢!
你能告诉'addNewQuestion()'javascript函数? –