简体   繁体   中英

Checkboxlist with image struts2

I'll try to put an image in my checkboxlist like this

<s:checkbox name="evento.eve_tarjeta_cred1" id="chkAmex" />
<img alt="10" src="./images/tarjetasCredito/amex.png">
<s:checkbox name="evento.eve_tarjeta_cred2" id="chkCmr" />
<img alt="10" src="./images/tarjetasCredito/cmr.png">

But i need to pass a list in one field to save it in my DB like "true,false".

I'll try in this way

<s:checkboxlist id="chkTarjeta" name="evento.eve_tarjeta_cred" 
        list="{
            '<img alt="10" src="./images/tarjetasCredito/amex.png">',
            '<img alt="10" src="./images/tarjetasCredito/cmr.png">'
                   }"

/>

but it doesn't work. HELP

The error is probably in the <img/> tags declaration: when you open the first double quote , you are closing the list attribute.

Put them in the Action (and read the values with a getter from the list attribute), or try escaping them manually;

<s:checkboxlist id="chkTarjeta" name="evento.eve_tarjeta_cred" 
  list="{
    '<img alt=&quot;10&quot; src=&quot;./images/tarjetasCredito/amex.png&quot;>',
    '<img alt=&quot;10&quot; src=&quot;./images/tarjetasCredito/cmr.png&quot;>'
  }"
/>

I'm not sure if Struts2 will escape the values or not; if it will, the only chance would be to extend the Tag.

To submit values in one string line use the same names on both checkboxes and your evento.eve_tarjeta_cred variable must be a String. Use <label> tags to create labels of image for checkboxes.

Note: that unchecked chekboxes ( false values) won't be submitted.

<label>
  <s:checkbox name="evento.eve_tarjeta_cred" id="chkAmex" />
  <img alt="10" src="./images/tarjetasCredito/amex.png" />
</label>

<label>
  <s:checkbox name="evento.eve_tarjeta_cred" id="chkCmr" />
  <img alt="10" src="./images/tarjetasCredito/cmr.png" />
</label>

I solved it like this, in my JSP i got this

<s:checkbox name="t1" id="chkAmex" />
<img alt="10" src="./images/tarjetasCredito/amex.png">

<s:checkbox name="t2" id="chkCmr" />
<img alt="10" src="./images/tarjetasCredito/cmr.png">

and in my Action i declare a global variable "tarjetas", with get and set, obviously

tarjetas = t1 + "," + t2;
evento.setEve_tarjeta_cred(tarjetas);
eventoservice.RegistraEvento(evento);

and to show it in Update case, just do this

String[] tarjetasArray = evento.getEve_tarjeta_cred().split(",");
            for (int i = 0; i < tarjetasArray.length; i++) {
                t1 = tarjetasArray[0];
                t2 = tarjetasArray[1];
            }

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