Thymeleaf-表单中的多个提交按钮

时间:2019-07-25 05:16:25

标签: java spring thymeleaf

我有一个当前仅具有文本输入字段的表单。用户有3个关于如何处理输入的选项:

  1. 首先,“发送”将接受输入并将其保存(发送)到另一个 存储变量,然后在输入字段下方将其显示为: “当前有效值为:输入”。
  2. “清除”按钮应清除此字段,以便在 按下它,存储变量将显示“ null”,例如:“当前活动 值是:null”。
  3. 最后一个“查看结果”应采用输入内容,并将其与 其他值列表,如果有匹配项,则打开新页面 并列出列表中的结果。

当前,只有“发送”按钮可以正常使用。 “清除”和“查看结果”按钮完全没有响应,但是显示在页面上并且可以单击。

有什么想法可以解决最后2个按钮吗?

<body>
    <p>
        <a href='/NewTest'>New Test upload</a>
    </p>

    <div>
        <form th:action="@{/sendTest}" th:object="${Test}" method="post">
        <label for="name" id="name" >Name</label>
        <input type="text" id="name" name="name"/>

        <input type="submit" name="action" value="Send"/></form>
        <input type="submit" name="action" value="Clear"/></form>
        <input type="submit" name="action" value="See Results"/></form>
        <br>
    </div>

    <a> Current test for students: </a>
    <p th:text="${currentTest}" ></p>
    <div>
    <a>Available tests for students:</a>
    <th:block th:each="test : ${tests}">
        <tr>
            <td th:text="${test.getName()}">...</td>
            <td th:text="${test.getFile().getName()}">...</td>
        </tr>
    </div>
</body>

控制器

@Controller
public class TeacherController {

    TestController testcont = TestController.getInstance();

    @RequestMapping(value = {"/Teacher"}, method = RequestMethod.GET)
    public String teacher(Model model) {

        model.addAttribute("Test", new Test());
        model.addAttribute("tests", testcont.showAllTests());
        model.addAttribute("currentTest", testcont.getActiveTest());


        return "Teacher";
    }

    @RequestMapping(value = {"/sendTest"}, method = RequestMethod.GET)
    public String currentTestOptions(Model model) {

        model.addAttribute("Test", new Test());
        model.addAttribute("tests", testcont.showAllTests());
        model.addAttribute("currentTest", testcont.getActiveTest());


        return "Teacher";
    }

    @RequestMapping(value = {"/sendTest"}, method = RequestMethod.POST, params="action=Send")
    public String sendTest(@Valid @ModelAttribute("test") Test test, BindingResult bindingResult, Model model){


        for(Test i : testcont.showAllTests()){
            if(i.getName().equals(test.getName())) {
                testcont.SetActiveTest(i);
                model.addAttribute("currentTest", testcont.getActiveTest());
            }
        }

        return "sendTest";
    }

    @RequestMapping(value = {"/sendTest"}, method = RequestMethod.POST, params="action=Clear")
    public String clearTest(){

        testcont.SetActiveTest(null);


        return "Teacher";

    }

    //***********Test Results************

    @RequestMapping(value = {"/TestResults"}, method = RequestMethod.GET)
    public String testResultsData(@Valid @ModelAttribute("test") Test test, BindingResult bindingResult, Model model){

        model.addAttribute("Students", testcont.showAllStudents());

        return "TestResults";

    }

    @RequestMapping(value = {"/sendTest"}, method = RequestMethod.POST, params="action=See Results")
    public String testResults(@Valid @ModelAttribute("test") Test test, BindingResult bindingResult, Model model){

        for(Test i : testcont.showAllTests()){
            if(i.getName().equals(test.getName())) {
                model.addAttribute("Answer", i.getStudentAnswer());
            }
        }

        return "TestResults";

    }
}

0 个答案:

没有答案