2017-03-11 3 views
0

私は休止状態のWebアプリケーションで春があります。 1つのページでは、複数のサブジェクトで構成されたリストボックスがあります。同じページにはtextareaと4 があります。私はQuestionText textareaと4つのテキストボックスの質問のオプションを入力します。私の意向は、私が質問を保存したいと思う、私は選択した科目と一緒にデータベースに4つのオプションです。Spring MVC:リストボックスをバインドする(複数の)値をコントローラクラスのメソッドに設定しました

質問モデルクラス

@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で表示されています。今、私はリストボックスから複数の値を選択し、テキストエリアとテキストボックスにいくつかのデータを入力します。選択された科目は、 "質問"クラスの "科目"フィールドに挿入されていません。選択したオプションをキャプチャしてモデルクラスフィールド(Set<Subject> subjects = new HashSet<Subject>();)に入れる方法私は404 error with message "The request sent by the client was syntactically incorrect()"

答えて

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= "https://stackoverflow.com/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"; 

} 
を取得しています私のビューページで[送信]ボタンを質問を追加]をクリックします
関連する問題