简体   繁体   English

如何在Spring Boot中将数据从视图发送到控制器

[英]How to send data from view to controller in spring boot

Objective: We have a view page -> index.html (consists of javascript functions) We have a spring boot controller class. 目标:我们有一个查看页面-> index.html(由javascript函数组成)我们有一个spring boot controller类。

We trigger localhost:port in web browser, the server captures the url and via controller class, we receive the trigger and execute a function to just display the web page (as mentioned above index.html). 我们在网络浏览器中触发localhost:port,服务器捕获URL并通过控制器类,我们接收到触发器并执行一个仅显示网页的函数(如上所述,index.html)。

Now after the index.html page is displayed in the web browser, we trigger numerous functionalities through the buttons defined (in javascript) in the index.html file. 现在,在Web浏览器中显示index.html页面之后,我们通过在index.html文件中定义的按钮(在javascript中)触发了许多功能。 Consider we have a text box in the index.html page where we generate few text data. 考虑一下我们在index.html页面中有一个文本框,我们在其中生成少量文本数据。

Problem: Objective is to save the data in the above mentioned text box into the database via backend technology. 问题:目标是通过后端技术将上述文本框中的数据保存到数据库中。 How to pass the data in the text box to the controller class (may be). 如何将文本框中的数据传递给控制器​​类(可能是)。

index.html: index.html的:

<html>
<body>

 <script type="text/javascript">
     //definition of save()
 </script>
<button id="btnNew" onclick="new()">New</button>
<button id="btnSave" onclick="save()">Save</button>
<input style="width: 300px" type="text" id="textBox" value=""/>

</body>
</html>

Controller Class: 控制器类别:

@Controller
public class WelcomeController {

    private String input;

    @GetMapping("/")
    public String main(Model model) {
        this.input= retrieveInputParameters();
        model.addAttribute("token", this.input);
        return "index"; //view
    }
}

Description of Code: 代码说明:

Controller Class: As mentioned above, currently the controller class have only 1 method main, which is triggered when the server is hit: http://localhost:8080 控制器类:如上所述,当前控制器类只有1个方法main,当服务器被点击时会触发该方法: http:// localhost:8080

The method retrieves an input parameter and then renders the index.html page. 该方法检索输入参数,然后呈现index.html页面。

index.html: It consists of multiple buttons with multiple functionality. index.html:它由具有多个功能的多个按钮组成。 After clicking on Save button, it triggers a 3rd party url and retrieves data (which has no connection with the server it is running in). 单击“保存”按钮后,它将触发第三方URL并检索数据(该数据与运行它的服务器没有连接)。 Now We want the data retrieved, in the server (controller class). 现在,我们希望在服务器(控制器类)中检索数据。

Any suggestion will be helpful. 任何建议都会有所帮助。

We can send the data from view to controller via Ajax: 我们可以通过Ajax将数据从视图发送到控制器:

For example: 例如:

$.ajax({
                type:"POST",
                contentType : 'application/xml; charset=utf-8',
                dataType : 'json',
                url: "/xyz",
                data: document.getElementById('abc').value,
                success:function(result){

                }
            });

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

相关问题 如何将基本 HTML 表单数据发送到 Spring 启动 controller? - How to send Basic HTML Form data to Spring Boot controller? 如何将数据从Ajax发送到Spring Controller - How to send data from ajax to spring controller LARAVEL:如何将数据从视图发送到控制器 - LARAVEL: how to send data from view to controller 如何在Codeigniter中将数据从视图发送到控制器 - How to send data from view to controller in codeigniter 如何通过数据响应将数据从Spring控制器发送到angularjs控制器 - how to send data from spring controller to angularjs controller with response of data 从 javascript 发送请求到 Spring 引导 controller - Send request from javascript to Spring boot controller 在Spring Boot中如何从控制器向网页发送值或从网页上的控制器获取值? - How send value from controller to web page or get value from controller on web page in Spring boot? 如何在没有Ajax的情况下将数据从View发送到Controller? - How to send data from View to Controller without ajax? 如何将AJAX数据从VIEW发送到CONTROLLER? (PHP(MVC)+ AJAX) - How to send AJAX data from VIEW to CONTROLLER? (PHP(MVC)+AJAX) 我如何从我的控制器发送 json 数据来查看? - how can i send json data from my controller to view?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM