【问题标题】:Return value from select using FreeMarker使用 FreeMarker 从选择返回值
【发布时间】:2018-11-20 12:39:58
【问题描述】:

我有一个表格:

     <form action="user-fonts" method="post" ">
        <select >
        <#list fonts as font>
        <option value=${font}>${font.nameFont?ifExists}</option>
        </#list>
        </select>
            <input type="hidden" name="_csrf" value="${_csrf.token}" />
            <div><input type="submit" value="Sign In"/></div>
        </form>

如何获取我在表单上选择的控制器中的值?

     @RequestMapping("/user-fonts")
public class MainController{
    @GetMapping
    public String main(@AuthenticationPrincipal User user, Model model)
    {
       Set<DBFont> fonts = user.getFont();
        model.addAttribute("fonts", fonts);
        return "Myfonts";
    }
    @PostMapping
    public String mainPost(@ModelAttribute DBFont DBfont)
    {

        System.out.println(DBfont.getNameFont());
         return "redirect:/user-fonts";
    }

数据库中有值,但是我得到null,如何返回值?

【问题讨论】:

    标签: java spring-boot freemarker select-options


    【解决方案1】:

    您需要为您的选择定义一个name 属性,例如nameFont:

     <select name="nameFont">
    

    这将发送字体选择值作为POST参数nameFont

    【讨论】:

    • @crazy_deviL 欢迎您,如果您觉得有帮助,可以点赞答案,请参阅stackoverflow.com/help/someone-answers
    • 为什么,除了名字之外的一切都返回给我NULL?((
    • @crazy_deviL 你能打开一个新问题吗?
    • 我写了一个新问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-02
    • 2015-12-05
    • 2014-08-16
    相关资源
    最近更新 更多