【问题标题】:Required String parameter 'email' is not present error in Spring MVCSpring MVC 中不存在必需的字符串参数“电子邮件”错误
【发布时间】:2014-09-18 09:51:00
【问题描述】:

我尝试在 Spring MVC 中对我的控制器进行查询。

我的操作代码是:

@RequestMapping(value = "/compile/{applicationId}", method = RequestMethod.GET)
    public String compileApplication(@PathVariable Long applicationId, @RequestParam("email") String email) throws ApplicationNotFoundException {
    ...
}

但我得到一个错误:

必需的字符串参数“电子邮件”不存在

我的 main.js:

 var runCompileModal = function (options) {
        $(".modal-compile").each(function () {
            var modalNode = $('<div class="modal fade" id="compileModal" tabindex="-1" role="dialog" aria-hidden="true">' +
                '<div class="modal-dialog modal-dialog-sm">' +
                '    <div class="modal-content">' +
                '        <div class="modal-header">' +
                '            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>' +
                '            <h4 class="modal-title text-center">compile ?</h4>' +
                '        </div>' +
                '        <div class="modal-body">' +
                '            <p class="margin-bottom-lg">Are you sure ?</p>' +
                '            <div class="form-group text-center">' +
                '               <label for="email">@ Mail:</label>' +
                '               <input  class="form-control" name="email" id="email" type="text" style="border:solid 1px black; border-radius:5px; text-align:center; box-shadow:0 0 6px;" />                                  ' +
                '            </div>' +
                '            <div class="form-group text-center">' +

                '                <button type="button" class="modal-btn-yes btn btn-success btn-gradient margin-right-lg">' +
                '                    <i class="fa fa-check"></i>' +
                '                    <span>' + options.compileModal.yes + '</span>' +
                '                </button>' +
                '                <button type="button" class="modal-btn-no btn btn-danger btn-gradient" data-dismiss="modal">' +
                '                    <i class="fa fa-warning"></i>' +
                '                    <span>' + options.compileModal.no + '</span>' +
                '                </button>' +
                '            </div>' +
                '        </div>' +
                '    </div>' +
                '</div>' +
                '</div>');

            $('body').append(modalNode);

            modalNode.find('.modal-btn-yes').click(function () {
                var email =  document.getElementById('email');
                location.href = $(this).data('url') + "/" + email.value;
            });
            modalNode.on('show.bs.modal', function (e) {
                var button = $(e.relatedTarget);
                var modal = $('#compileModal');
                modal.find('.modal-title').html(button.data('title'));
                modal.find('.modal-body p').html(button.data('content'));
                modal.find('.modal-btn-yes').attr('data-url', button.data('url'));
            });
        });
    }

我的 app.html:

            '       <button title="' + compileTitle + '" class="btn btn-compile btn-gradient" data-toggle="modal" data-target="#compileModal" data-url="' + compileUrl + object.id + '" + data-title="' + compileModalTitle + '" + data-content="' + compileModalContent + '">' +
            '           <span class="glyphicon glyphicon-share"></span>' +
            '       </button>' 

当我在 url 中包含电子邮件参数时出现新错误:

类型错误:e 为空

【问题讨论】:

  • 执行 HTTP get 的代码是什么样的?

标签: java spring spring-mvc


【解决方案1】:

在我看来是这样的

location.href = $(this).data('url') + "/" + email.value;

应该是

location.href = $(this).data('url') + "/?email="
    + URLEncoder.encode(email.value, "UTF-8");

虽然我可能走错了路。 (URLEncoder 也需要修改。)

附言

带有 TamperData 插件或其他工具的 FireFox 允许拦截进出浏览器的流量。

【讨论】:

  • URLncoder 是一个 JS 函数,因为我有这个错误: URLEncoder is not defined
  • 是的 URLEncoder 是 java,不是 JS。在 JS 中,escape(email.value) 就足够了,也请参见 here
【解决方案2】:

这意味着您没有从前端(从客户端)传递电子邮件参数。

【讨论】:

  • 我怎样才能传递这个参数?
  • 更改 location.href = $(this).data('url');在 url 中包含电子邮件参数。
  • 我不知道该怎么做。?
猜你喜欢
  • 2021-01-12
  • 2013-11-06
  • 1970-01-01
  • 2018-05-10
  • 1970-01-01
  • 1970-01-01
  • 2019-09-23
  • 2021-04-01
  • 1970-01-01
相关资源
最近更新 更多