简体   繁体   English

在提交之前显示模态页面中控制器的值

[英]show values from controller in modal page before submit

Trying to add a model confirmation page (continue/cancel) with some info in it. 尝试添加模型确认页面(继续/取消),其中包含一些信息。

The info comes from a form with a couple of inputs... 信息来自一个带有几个输入的表格......

I have created a litte project, I think it ilustrates this requirement. 我创建了一个litte项目,我认为它说明了这个要求。

Before submit I want to show in modal page a value depending on the values entered in the form. 在提交之前,我想在模态页面中显示一个值,具体取决于在表单中输入的值。 In this case I have 3 input to make a simple sum. 在这种情况下,我有3个输入来做一个简单的总和。

So the modal page show the sum, and the user can decide to continue or not. 因此模态页面显示总和,用户可以决定是否继续。

This is the link of the project. 这是该项目的链接。 I'm stucked in index.html jquery section. 我被困在index.html jquery部分。

[ https://github.com/davisoski/action-listener][1] [ https://github.com/davisoski/action-listener] [1 ]

I think this is the correct approach, but I'm not able to make it in my case 我认为这是正确的方法,但我不能在我的情况下做到这一点

[ https://qtzar.com/2017/03/24/ajax-and-thymeleaf-for-modal-dialogs/][2] [ https://qtzar.com/2017/03/24/ajax-and-thymeleaf-for-modal-dialogs/] [2 ]

Any idea 任何的想法

UPDATE 1 更新1

I have a simple form with 3 fields. 我有一个简单的表格,有3个字段。 I have a controller with a postmapping for /saveform, This method just call a service and make a sum. 我有一个带有/ saveform后映射的控制器,这个方法只是调用一个服务并进行总结。 No problem with that. 没问题。

<form action="#" th:action="@{/saveform}" th:object="${modelform}"
                    method="post">

    <div class="form-group row">
        <div class="col-12 col-md-6  col-lg-3">
            <label class="col-form-label col-form-label-sm" for="value1">Value1</label>
            <input class="form-control form-control-sm" th:field="*{value1}"
                id="value1" type="text" />
        </div>
    </div>
    <div class="form-group row">
        <div class="col-12 col-md-6  col-lg-3">
            <label class="col-form-label col-form-label-sm" for="value2">Value2</label>
            <input class="form-control form-control-sm" th:field="*{value2}"
                id="value2" type="text" />
        </div>
    </div>
    <div class="form-group row">
        <div class="col-12 col-md-6  col-lg-3">
            <label class="col-form-label col-form-label-sm" for="value3">Value3</label>
            <input class="form-control form-control-sm" th:field="*{value3}"
                id="value3" type="text" />
        </div>
    </div>

    <div class="form-group row">
        <div class="col-12 col-md-6  col-lg-3">

            <a data-toggle="modal" href="#myModal" class="btn btn-primary"><i
                class="icon-plus-squared"></i><span>Calculate Sum</span></a> <a
                data-toggle="modal" href="#myModal" class="btn btn-primary"
                th:onclick="'javascript:openModal();'"><i
                class="icon-plus-squared"></i><span>Calculate Sum2</span></a>



        </div>
    </div>

</form>

What I want to do is place in the middle of the process a modal page with that sum, so the user can decide if continue or cancel 我想要做的是在进程的中间放置一个带有该总和的模态页面,以便用户可以决定是继续还是取消

I added a model like this (see index.html in the github project), I cant see the way to make a previus call to a method or something to show the value of sum in the model page. 我添加了一个这样的模型(参见github项目中的index.html),我无法看到对方法或其他东西进行普通调用以在模型页面中显示sum值的方法。 I used to do in JSF using actionListeners, but here I dont know how to do it. 我以前在JSF中使用actionListeners,但在这里我不知道如何做到这一点。 The blog from qtzar.com gave me an idea 来自qtzar.com的博客给了我一个想法

<div class="modal" id="myModal">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">We have calculated the sum of
                    values</h5>
                <button type="button" class="close" id="close"
                    data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <p>Are you agree with this sum???</p>
            </div>
            <div class="modal-footer">
                <!-- 
                <a href="#" data-dismiss="modal" class="btn">Close</a> <a
                    href="#" class="btn btn-primary">Save changes</a>
-->
                <button type="button" class="btn btn-primary btn-ok"
                    id="continue">Continue</button>
                <button type="button" class="btn btn-danger"
                    data-dismiss="modal" id="cancel">Cancel</button>
            </div>
        </div>
    </div>
</div>

UPDATE 2 更新2

It should be something like this: 它应该是这样的:

$(function() {
        $.ajax({
            url : "/sum",
            success : function(data) {
                $("#modalHolder").html(data);
                $("#myModal").modal("show");
            }

        });

    }

and define in the controller a @RequestMapping 并在控制器中定义@RequestMapping

@RequestMapping("/sum")

but I can't see how to pass parameters and where to put the return values to to modal 但我看不出如何传递参数以及将返回值放到模态的位置

UPDATE 3: 更新3:

Modified modal page, added th:fragment: 修改后的模态页面,添加了:fragment:

        <!--  Modal -->
        <div class="modal" id="personModal" role="dialog"
            th:fragment="modalContents">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title">We have calculated the sum of values</h5>
                        <button type="button" class="close" id="close"
                            data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                    <div class="modal-body">
                        <p>Are you agree with this sum???</p>
                        <h4 class="btn" th:text="${sum}"></h4>
                    </div>
                    <div class="modal-footer">

                        <button type="button" class="btn btn-primary btn-ok" id="continue">Continue</button>
                        <button type="button" class="btn btn-danger" data-dismiss="modal"
                            id="cancel">Cancel</button>
                    </div>
                </div>
            </div>
        </div>

Added two method in controller, one to calculate the sum to show in modal dialog and another to send to the server. 在控制器中添加了两个方法,一个用于计算在模态对话框中显示的总和,另一个用于发送到服务器。

@PostMapping({ "/saveform" })
    public String saveForm(Model model, RedirectAttributes ra) {
        LOG.info("HomeController.saveForm");

        //Simulate the sum
        model.addAttribute("sum", "25");

        return "/::modalContents";

    }

    @RequestMapping("/sum")
    public String mySum(Model model) {

        model.addAttribute("sum", "24");

        return "/::modalContents";

    }

I'm think I'm close, but I'm getting error 500.


  [1]: https://github.com/davisoski/action-listener
  [2]: https://qtzar.com/2017/03/24/ajax-and-thymeleaf-for-modal-dialogs/

I'm not sure if this is the answer you're looking for since it doesn't give you the Model object directly, but since nobody has answered this yet I will at least give you a way to get the parameters of the request. 我不确定这是否是你正在寻找的答案,因为它没有直接给你Model对象,但由于没有人回答这个问题,我至少会给你一个获取请求参数的方法。

Here's a part of some code I wrote some time ago, you can use @RequestParam to get parameters. 这是我前一段时间编写的一些代码的一部分,你可以使用@RequestParam来获取参数。 An example request for the database/addUser method would be database/addUser?id=12&bookingIds=1&bookingIds=11&name=Olafur&email=classified@nothere.com which you can of course create with ajax database/addUser方法的示例请求是database/addUser?id=12&bookingIds=1&bookingIds=11&name=Olafur&email=classified@nothere.com ,您当然可以使用ajax创建

Also, could it be that you're just simple sending Spring "/sum" without any data. 而且,你可能只是简单地发送没有任何数据的Spring “/ sum”。

I'm really sorry if this doesn't help you, so feel free to downvote in that case. 如果这对你没有帮助,我真的很抱歉,所以在那种情况下请随意投票。 I will leave you a snippet that explains a lot of the syntax of @ReqeuestParam here. 我将留下一个解释@ReqeuestParam的大量语法的@ReqeuestParam :) :)

@RestController
@RequestMapping(path="/database") 
public class MainController {

    @CrossOrigin
    @GetMapping(path = "/addUser") // Map ONLY GET Requests
    public @ResponseBody
    String addNewUser(
         @RequestParam(required = false) Long id,
         @RequestParam(required = false) ArrayList<Long> bookingIds,
         @RequestParam(required = false) ArrayList<Long> reviewIds,
         @RequestParam String name,
         @RequestParam String email
    ) {
        UserEntity u = new UserEntity();
        if(id != null)        u.setId(id);
        if(bookingIds != null) u.setBookingIds((Map<Integer, Long>) Converter.arrayListToMap(bookingIds));
        if(reviewIds != null) u.setReviewIds((Map<Integer, Long>) Converter.arrayListToMap(bookingIds));
        u.setName(name);
        u.setEmail(email);
        u = userRepository.save(u);
        return u.getId().toString();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM