簡體   English   中英

如何將多個參數從視圖傳遞到控制器的方法

[英]How to pass several parameters from view to controller's method

您能告訴我如何以最方便的方式將多個參數從視圖傳遞到控制器的功能嗎?

JSP視圖:

<h2>${topic.getName()}</h2>
<h3>${topic.getText()}</h2>

<form:form method="post" commandName="newComment">
    <fieldset>
        <div class="editor-label">
            <td><form:label path="text">Input comment</form:label></td>
        </div>

        <div class="textarea">
            <form:textarea path="text" />
        </div>

        <p>
            <input type="submit" value="Comment" />
        </p>
    </fieldset>
</form:form>

如您所見,我們具有topicnewComent屬性,它們代表主題和評論實體。
這是一個控制器:

@RequestMapping(value = "/addComment/{topicId}", method = RequestMethod.POST)
public String saveComment(@ModelAttribute("newComment")Comment comment, BindingResult result, Model model){

    validate(comment, result);
    if (result.hasErrors() )
        {
            return "//";
        }
        return "redirect:details/";
    }
}

評論實體被認為很好,但我也需要Topic對象(或其ID)的實例。 在視圖中可以訪問主題對象的實例,主題ID是響應的一部分。 您能給我一個想法,我該如何解決這個問題?

如果您具有topicId可以獲取主題模型嗎? 在你的路上。 您可以通過在方法參數中添加@PathVariable批注來獲取它。

public String saveComment(@PathVariable String topicId, @ModelAttribute("newComment")Comment comment, BindingResult result, Model model){

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM