简体   繁体   English

如何检查表单是否以Java提交

[英]How to check if form was submitted in Java

I have a form in jsp: 我在jsp中有一个表格:

<form id="emailForm" data-role="form">
      <input type="text" class="form-control" id="name" name="name" placeholder="Enter full name..">
   <input type="submit" id="emailSubmit" name="emailSubmit" class="btn btn-default" value="submit">
</form>

I send the form to controller using AJAX: 我使用AJAX将表单发送给控制器:

$("#emailSubmit").click(function(e){
        e.preventDefault(); //STOP default action
    var postData    = $("#emailForm").serializeArray();
        $.ajax(
            {
                type: "POST",
                url : "HomeController",
                data : postData,
                success: function(data) 
                {
                    $("#emailResult").html("<p>Thank your for submitting</p>);

                },
                error: function(jqXHR, textStatus, errorThrown) 
                {
                    $("#emailResult").html("<p>ss"+errorThrown+textStatus+jqXHR+"</p>");
                }
            });
});

I check if it has been submitted in Controller here: 我在这里检查是否已在Controller中提交:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    String emailSubmit = request.getParameter("emailSubmit");

    if(emailSubmit != null){
        // continue
    }}

Can someone please tell me why when it checks if form was submitted in the controller that it is null ? 有人可以告诉我为什么检查表单是否在控制器中提交时为null吗?

For forms the standard way is to catch the submit event instead of the click event of the button: 对于表单,标准方法是捕获submit事件而不是按钮的click事件:

$("#emailForm").submit(function(e){
    e.preventDefault();
    var postData = $(this).serializeArray(); // or: $(this).serialize();
    $.ajax({
        type: "POST",
        url : "HomeController",
        data : postData,
        success: function(data) 
        {
            $("#emailResult").html("<p>Thank your for submitting</p>);

        },
        error: function(jqXHR, textStatus, errorThrown) 
        {
            $("#emailResult").html("<p>ss"+errorThrown+textStatus+jqXHR+"</p>");
        }
    });
});

I have tried several methods to be able to check the submit button isn't null and can't solve that issue. 我尝试了几种方法来检查“提交”按钮是否为null并不能解决该问题。 For now I have set a hidden input field in the form like so: 现在,我以如下形式设置了一个隐藏的输入字段:

<input type="hidden" name="form" value="contactForm">

In controller I check for the form: 在控制器中,我检查以下形式:

String form     = request.getParameter("form");
    if(form.equals("contactForm")){
        // continue
    }

Doing this enables me to know which form has been posted to the controller. 这样做使我知道哪个表格已过帐到控制器。

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

相关问题 检查表单元素是否已提交并处理提交的表单 - Check if form element is submitted and handling submitted form 如何检查是否单击了按钮,如果表单已提交Selenium / TestNG - How to check if button clicked, IF form is actually submitted Selenium / TestNG 检查表单是否实际提交-Java / Selenium / TestNG - Checking if form was actually submitted - Java/Selenium/TestNG 如何检查表单是否存在并打印表单的所有属性HTMLUNIT JAVA - how to check if the form exists and print all the attributes of the form, HTMLUNIT JAVA 如何在Java / Scala中中断提交给newSingleThreadExecutor的线程? - How to interrupt a thread submitted to newSingleThreadExecutor in Java/Scala? 将Servlet中提交的表单值传递给Java类,从而插入数据库 - passing form values submitted in a servlet to a java class and hence inserting into database 在 Java Spring MVC bootstrap modal 中显示提交的表单数据 - Display submitted form data in Java Spring MVC bootstrap modal 如何控制Java Futures“提交”的订单? - How to control orders in which Java Futures are “submitted”? 通过Java中的表单提交数据时捕获Web服务响应 - Capture Webservice response when data submitted through form in java 如何查看风暴拓扑提交成功或失败? - How to check storm topology submitted successfully or got failed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM