我有一个带有hibernate web应用程序的spring。在其中一个页面中,我有列表框,其中包含多个主题。在同一页面中,我有一个textarea
和4个text-box
,其中我将在4个文本框中输入问题文本textarea
和问题选项。我的目的是当我点击提交按钮时,我想将问题和它的4个选项以及所选主题存储到数据库中。
问题模型类:
@Entity
@Table(name="QUESTION")
public class Question
{
@Id
@Column(name="questionId")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private long questionId;
@Column(length=1000)
private String questionText;
private String choiceOne;
private String choiceTwo;
private String choiceThree;
private String choiceFour;
private String choiceFive;
private String answer;
private String explanation;
private boolean status;
@ManyToMany(fetch = FetchType.LAZY , cascade = CascadeType.ALL)
@JoinTable(name = "QSUBJECTTAG", joinColumns = { @JoinColumn (name = "questionId")}, inverseJoinColumns = { @JoinColumn(name = "subjectId")})
private Set<Subject> subjects = new HashSet<Subject>();
//setter and getter methods for the fields
}
查看:question.jsp:
<c:url var="addAction" value="/question/add" ></c:url>
<form:form action="${addAction}" commandName="question">
<table border="1">
<tr>
<c:if test="${!empty listSubjects}">
<td colspan="2">
<form:label path="subjects">
<spring:message text="Subjects List"/>
</form:label>
</td>
<td colspan="4">
<form:select path="subjects" size="3" >
<form:options items="${listSubjects}" itemValue="subjectId" itemLabel="subjectDesc"/>
</form:select>
</td>
</c:if>
<c:if test="${empty listSubjects}">
<td>
<form:label path="questionId">
<spring:message text="Question ID"/>
</form:label>
</td>
<td>
<form:select path="subjects">
<form:option value="NONE">--Select Subject--</form:option>
</form:select>
</td>
</c:if>
</tr>
<c:if test="${!empty question.questionText}">
<tr>
<td>
<form:label path="questionId">
<spring:message text="Question ID"/>
</form:label>
</td>
<td>
<form:input path="questionId" readonly="true" size="8" disabled="true" />
<form:hidden path="questionId" />
</td>
</tr>
</c:if>
<tr rowspan="4">
<td>
<form:label path="questionText">
<spring:message text="Question"/>
</form:label>
</td>
<td colspan="6">
<form:textarea path="questionText" size="1000" rows="20" cols="50"/>
</td>
</tr>
<tr>
<td>
<form:label path="choiceOne">
<spring:message text="Choice One"/>
</form:label>
</td>
<td>
<form:textarea path="choiceOne" size="200"/>
</td>
<td>
<form:label path="choiceTwo">
<spring:message text="Choice Two"/>
</form:label>
</td>
<td>
<form:textarea path="choiceTwo" size="200"/>
</td>
<td>
<form:label path="choiceThree">
<spring:message text="Choice Three"/>
</form:label>
</td>
<td>
<form:textarea path="choiceThree" size="200"/>
</td>
<td>
<form:label path="choiceFour">
<spring:message text="Choice Four"/>
</form:label>
</td>
<td>
<form:textarea path="choiceFour" size="200"/>
</td>
</tr>
<tr>
<td>
<form:label path="answer">
<spring:message text="Select Answer"/>
</form:label>
</td>
<td>
<form:select path="answer">
<form:option value="NONE">--select the anser--</form:option>
<form:option value="CHOICE_ONE">Choice One</form:option>
<form:option value="CHOICE_TWO">Choice TWO</form:option>
<form:option value="CHOICE_THREE">Choice Three</form:option>
<form:option value="CHOICE_FOUR">Choice Four</form:option>
</form:select>
</td>
</tr>
<tr>
<td colspan="2">
<c:if test="${!empty question.answer}">
<input type="submit"
value="<spring:message text="Edit Question"/>" />
</c:if>
<c:if test="${empty question.answer}">
<input type="submit"
value="<spring:message text="Add Question"/>" />
</c:if>
</td>
</tr>
</table>
</form:form>
控制器:QuestionController.java
@Controller
public class QuestionController {
private QuestionService questionService;
private SubjectService subjectService;
@Autowired(required=true)
@Qualifier(value="questionService")
public void setQuestionService(QuestionService questionService)
{
this.questionService = questionService;
}
@Autowired(required=true)
@Qualifier(value="subjectService")
public void setSubjectService(SubjectService subjectService)
{
this.subjectService = subjectService;
}
@RequestMapping(value="/questions", method = RequestMethod.GET)
public String listQuestions(Model model)
{
model.addAttribute("question",new Question());
model.addAttribute("listSubjects", this.subjectService.listSubjects());
return "questions";
}
//For add and update subject both
@RequestMapping(value= "/question/add", method = RequestMethod.POST)
public String addQuestion(@ModelAttribute("question") Question question){
if(question.getQuestionId() == 0){
//new Subject, add it
this.questionService.addQuestion(question);
}else{
//existing subject, call update
this.questionService.updateQuestion(question);
}
return "questions";
}
}
问题:我已将一些主题插入Subject
表,并且视图页面中填充了带有插入主题的ListBox
。现在,当我从ListBox中选择多个值并将一些数据输入到textarea和text-box时,所选主题不会被插入&#34;主题&#34; &#34;问题&#34;类。如何捕获选定的选项并将其放入我的Question
模型类字段Set<Subject> subjects = new HashSet<Subject>();
?
当我点击我的视图页面中的添加问题提交按钮时,我收到404 error with message "The request sent by the client was syntactically incorrect ()"
答案 0 :(得分:0)
<强>解决方案强>
@InitBinder("question")
public void dataBinding(WebDataBinder binder){
binder.registerCustomEditor(Set.class,"subjects", new CustomCollectionEditor(Set.class){
@Override
protected Object convertElement(Object element){
if (element!=null && element instanceof String){
return subjectService.getSubjectById(Long.parseLong((String)element));
}
return null;
}
});
}
@RequestMapping(value= "/questions/addQuestion", method = RequestMethod.POST)
public String addQuestion(@ModelAttribute("question") Question question,BindingResult result,Model model){
if(question.getQuestionId() == 0){
//new Subject, add it
this.questionService.addQuestion(question);
}else{
//existing subject, call update
this.questionService.updateQuestion(question);
}
return "redirect:/questions";
}