XSRF令牌不匹配

时间:2019-01-23 23:52:14

标签: broadleaf-commerce

嗨,我正在尝试通过管理部分中的自定义控制器处理表单提交

这是我的测试控制器

@Controller
@RequestMapping("/" + TempController.SECTION_KEY)
public class TempController extends AdminAbstractController {
    protected static final String SECTION_KEY = "test2";

    @RequestMapping(value = "", method = RequestMethod.GET)
    public String test(HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {
        // This is expected by the modules/emptyContainer template, this is a custom template that gets included into the body
        model.addAttribute("customView", "views/test2");
        ShippingEntity shp=new ShippingEntity();
        model.addAttribute("shipping",shp);
        // ensure navigation gets set up correctly
        setModelAttributes(model, SECTION_KEY);

        // gets the scaffolding set up to display the template from the customView attribute above
        return "modules/emptyContainer";
    }

    @RequestMapping(value = "", method = RequestMethod.POST)
    public String testPost(HttpServletRequest request, HttpServletResponse response, Model model,@ModelAttribute ShippingEntity shp) throws Exception {
        // This is expected by the modules/emptyContainer template, this is a custom template that gets included into the body
        model.addAttribute("customView", "views/test2");
        System.out.println(shp.getLink());
        System.out.println(shp.getTrackingNumber());
        model.addAttribute("shipping",shp);
        // ensure navigation gets set up correctly
        setModelAttributes(model, SECTION_KEY);

        // gets the scaffolding set up to display the template from the customView attribute above
        return "modules/emptyContainer";
    }


}   

这是视图模板:

<div class="row">
    <div class="twelve columns">
           <form action="#" th:action="@{/test2}" th:object="${shipping}" method="post">
        <p>Id: <input type="text" th:field="*{trackingNumber}" /></p>
        <p>Message: <input type="text" th:field="*{link}" /></p>
        <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
    </form>

    </div>
</div>

问题是,当我提交值时出现错误:

XSRF令牌不匹配(空)。会话可能已过期

我知道这与安全性问题有关,但是我找不到使它起作用的方法。

任何提示如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

显然,它比我想象的要简单,也许它将对将来的某个人有所帮助。

只需将<form> </form>更改为<blc:form></blc:form>