如何用spring形式的变量计算textarea?

vjhs03f7  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(356)

我正在使用springmvc和mysql来制作测验web应用程序,我一直在尝试为管理员制作表单,以便在创建测验时正确添加答案。
在我的表单中,我有四个可能的答案,我可以将所有内容插入mysql,但是我总是在我的文本列中把四个答案放在一起,我想把它们分开。
例如在mysql表中:
id |文本|正确|问题id
1 |答案1、答案2、答案3、答案4 |正确| 1
我每行只需要一个答案。
我的jsp和控制器代码:

<s:url var="url_create_answer" value="/admin/createAnswer"/>
                <f:form action="${url_create_answer}" commandName="command">
                    <table border="1">
                        <c:forEach var="i" begin="1" end="4">

                            <tr>
                                <td>Answer${i}</td>
                                <td><f:textarea rows="5" cols="100"  path="answer.text" placeholder="text answer ${i}"/></td>
                            </tr>
                            <tr>
                                <td>Is Correct?</td>
                                <td><f:radiobutton  path="answer.correct" value="${i}" /></td>
                            </tr>                                
                        </c:forEach>
                        <tr>
                            <td colspan="2" align="right"><button>Create</button>
                            </td>
                        </tr>                          
                    </table>                                
                </td>               
            </tr>
        </f:form>

@Controller
public class CreateAnswerController {

@Autowired
AnswerService answerService;

@RequestMapping(value = "admin/addAnswer", method = RequestMethod.GET)
public String createQuestionForm(Model m) {
    AnswerCommand cmd = new AnswerCommand();
    m.addAttribute("command", cmd);
    return "addAnswer"; // JSP view
}

@RequestMapping(value = "admin/createAnswer", method = RequestMethod.POST)
public String handleCreateAnswer(@ModelAttribute("command") AnswerCommand cmd,BindingResult result, Model m,HttpSession session) {

    for(int i = 1; i <= 4; i++){
    try {           
        Integer questionId = (Integer)(session.getAttribute("questionId"));            
        cmd.getAnswer().setQuestion_id(questionId);
        if(!cmd.getAnswer().getText().equals(""))
            if(cmd.getAnswer().getCorrect() == i){
                cmd.getAnswer().setCorrect(1);
                int answerId = answerService.createAnswer(cmd.getAnswer()); 
            }else{
                cmd.getAnswer().setCorrect(0);
                int answerId = answerService.createAnswer(cmd.getAnswer()); 
            }

    } catch (Exception ex) {
        ex.printStackTrace();
        m.addAttribute("err", "Could not add question to quiz");
        return "addAnswer";
    }
    }
    return "redirect:addQuestion"; //JSP view
}

}

twh00eeo

twh00eeo1#

你可以用一些独特的分隔符来存储答案,比如@$%!当您从数据库中检索它时,您可以拆分它并选择合适的答案并使用它。

相关问题