Search code examples
javascripthtmljquerystruts2

How to set value to for a textarea, and get it with struts2 actions?


I am new to Struts2 web development. I'm trying to create a form where user can input the current status of an order.

In the form,

when user selects "Order Delivered", the textarea will shows "Order delivered";

when user selects "Order Canceled", the textarea will shows "Order canceled";

when user selects "Others", the textarea wil be unlocked and user can input something in it.

My HTML and Javascript codes are as below:

function setCompleteReason(reason){
        element = document.getElementById("complete_resaon");
        switch(reason){
            case "delivered":
                element.disabled = true;
                element.value = "Order Delivered";
                break;
            case "canceled":
                element.disabled = true;
                element.value = "Order Canceled";
                break;
            case "others":
                element.disabled = false;
                element.value = "";
                break;
            default:
                element.disabled = true;
                element.value = "";
        }
    }


 function completeOrderBtn() {
        element = document.getElementById("complete_resaon");

        if( element.value.length == 0){
            alert("Please input something!");
            return false;
        }
        $('#orderForm').attr('action','ordercomplete.action');
        $("#orderForm").submit();
    }
<select class="form-control" onchange="setCompleteReason(this.value)" id="selectReason">
                            <option value=""></option>
                            <option value="delivered">Order Delivered</option>
                            <option value="canceled">Order Canceled</option>
                            <option value="others">Other(Please Input)</option>
</select>

<textarea class="form-control" name="ordMVo.complete_reason" id="complete_resaon" cols="10" rows="5" maxlength="100" resize="false" disabled="true"></textarea>


<button type="button" class="btn btn-primary btn-dhl" data-dismiss="modal" onclick="completeOrderBtn(); return false;">Complete</button>

If users select "others" and input values by themselves. The Struts2 could get the value of the textarea via ordMVo.complete_reason in my Java actions without any problem.

However, if users selects "Order Delivered" or "Order Canceled" and let the Javascript to create value to the textarea, my Java actions only get NULL value.

Is there any special way to set a value to the text area and let Struts2 Java actions to get it ?


Solution

  • Disabled HTML input elements are not submitted to the Struts2. You should check parameters in request or better in action context where all parameters are stored before they populated to the action.

    To resolve the problem with disabled textarea element you can add a hidden field next to the element

    <textarea class="form-control" name="ordMVo.complete_reason" id="complete_resaon" cols="10" rows="5" maxlength="100" resize="false" disabled="true"></textarea>
    <input type="hidden" id="hiddenId" name="ordMVo.complete_reason">
    

    When you submit a form you should set the value to the hidden field

    function completeOrderBtn() {
        element = document.getElementById("complete_resaon");
    
        if( element.value.length == 0){
            alert("Please input something!");
            return false;
        }
        if (element.disabled){
          $("#hiddenId").val(element.value);
        }
        $('#orderForm').attr('action','ordercomplete.action');
    
        $("#orderForm").submit();
    }