简体   繁体   中英

getting disabled true data from jsp to struts2 action class

I have disabled=true textboxes and combox boxes in my jsp.

When I try to map those value back to action, they disappear.

I don't want to call to DB again.

How can I set those disabled=true textboxes and combo boxes 's data value into hidden values ?

Thanks ahead.

The property of disabled elements' value not being submitted with the form is not an issue with struts2, but an HTML behavior. To handle this behavior, use the following implementations:

  1. Use readonly="readonly" attribute to prevent modification rather than using disabled. (This won't be available for few elements)
  2. Use onfocus="return false" to prevent any focus on html elements. This will prevent the modification of their value. You can use CSS to make these elements look like readonly or disabled.
  3. Before you disable an element, create a hidden element and attach the value of the element you are about to disable. This will make sure that the item gets submitted.

See the following implementation for select element. You may make use of the id attribute of s:select to set the html id of select element.

<select id="demoSelect" class="readonly">
    <option value="0">A</option>
    <option value="1">B</option>
    <option value="2" selected="selected">C</option>
    <option value="3">D</option>
    <option value="4">E</option>
    <option value="5">F</option>
</select>
<input type="hidden" value="2" name="demoSelectDefault"/>

jQuery:

$(document).ready(

    function() {
        $("select.readonly").live('change', function() { //live() makes sure that this is executed if you apply the class to the element even after the initial load. So, if you set the readonly class to a select element, you are done.
            var selectElement = this;       
            $("input[type=hidden][name=" + this.id + "Default]").each( //This is implemented in case of multiple select support. You will need to select nothing at first and then make this select each of this element
                function() {
                    selectElement.value = this.value;
                }
            );

        });
    }

);

Here, when you implement this using struts, populate the s:select, then use an s:hidden element to generate a corresponding default value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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