Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
464 views
in Technique[技术] by (71.8m points)

java - How to take values of only selected checkbox in Action class in Struts 2 and JSP

I am displaying 24 checkboxes. I want to get all the values of checked checkboxes in action class and insert it as a new record inside database.Inserting will be done once I succeed in getting the values of checked checkboxes on button click.

I referred this link in which I followed the answer answered by steven sir but with help of that I can display only boolean values but here I want text values of checkboxes selected.

So below is my JSP page.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
</head>
<body>
<s:form action="eventInsertAction">
        <!-- Main content -->
        <section class="content">
          <!-- Small boxes (Stat box) -->
          
          
          <div class="row">
            <div class="contetpanel">
                <div>
                    <div class="crevtbl"> 
                        <div class="crevtblRow"> 
                            <div class="crevtblCell">Event Name</div> 
                            <div class="crevtblCell1">:</div> 
                            <div class="crevtblCell2"><input name="event.eventName" class="formtextfield" type="text"><s:fielderror fieldName="event.eventName"/></div> 
                        </div>
                        <div class="crevtblRow"> 
                            <div class="crevtblCell">Company Name</div> 
                            <div class="crevtblCell1">:</div> 
                            <div class="crevtblCell2"><input name="event.companyName" class="formtextfield" type="text" ><s:fielderror fieldName="event.companyName"/></div> 
                        </div>
                        <div class="crevtblRow"> 
                            <div class="crevtblCell">Contact Person Name</div> 
                            <div class="crevtblCell1">:</div> 
                            <div class="crevtblCell2"><input name="event.contactPerson" class="formtextfield" type="text" ><s:fielderror fieldName="event.contactPerson"/></div> 
                        </div>
                        <div class="crevtblRow"> 
                            <div class="crevtblCell">Contact</div> 
                            <div class="crevtblCell1">:</div> 
                            <div class="crevtblCell2"><input name="event.contactNumber" class="formtextfield" type="text" ><s:fielderror fieldName="event.contactNumber"/></div> 
                        </div>
                        <div class="crevtblRow"> 
                            <div class="crevtblCell">Email</div> 
                            <div class="crevtblCell1">:</div> 
                            <div class="crevtblCell2"><input name="event.emailId" class="formtextfield" type="text" ><s:fielderror fieldName="event.emailId"/></div> 
                        </div>
                        <div class="crevtblRow"> 
                            <div class="crevtblCell">Event Venue</div> 
                            <div class="crevtblCell1">:</div> 
                            <div class="crevtblCell2"><input name="event.eventVenue" class="formtextfield" type="text" ><s:fielderror fieldName="event.eventVenue"/></div> 
                        </div>
                        <div class="crevtblRow"> 
                            <div class="crevtblCell">Event Date</div> 
                            <div class="crevtblCell1">:</div> 
                            <div class="crevtblCell2">From : <input name="event.fromDate" class="formtextfield1" type="text" placeholder="YYYY/MM/DD"> &nbsp; &nbsp; &nbsp; &nbsp; To : <input name="event.toDate" class="formtextfield1" type="text" placeholder="YYYY/MM/DD"></div> 
                        </div>
                        <div class="crevtblRow"> 
                            <div class="crevtblCell">Event Time</div> 
                            <div class="crevtblCell1">:</div> 
                            <div class="crevtblCell2"><input name="event.eventTime" class="formtextfield1" type="text" placeholder="HH:MM  AM/PM" ><s:fielderror fieldName="event.eventTime"/></div> 
                        </div>
                        <div class="crevtblRow">
                            <div class="crevtblCell">License Required</div> 
                            <div class="crevtblCell1">:</div> 
                            <div class="crevtblCell2">
                            <input name="event.licenserequired" type="checkbox" class="formcheckbox" value=""><span class="formcheckbox_content">Rangabhoomi</span>
                            <input name="event.licenserequired" type="checkbox" class="formcheckbox" value=""><span class="formcheckbox_content">Fire NOC</span>
                            <input name="event.licenserequired" type="checkbox" class="formcheckbox" value=""><span class="formcheckbox_content">Fire Engine</span>
                            <input name="event.licenserequired" type="checkbox" class="formcheckbox" value=""><span class="formcheckbox_content">Premises &amp; NOC</span>
                            <input name="event.licenserequired" type="checkbox" class="formcheckbox" value=""><span class="formcheckbox_content">Performance</span>
                            <input name="event.licenserequired" type="checkbox" class="formcheckbox" value=""><span class="formcheckbox_content">PWD</span>
                            <input name="event.licenserequired" type="checkbox" class="formcheckbox" value=""><span class="formcheckbox_content">Local Police</span><br />
                            <input name="event.licenserequired" type="checkbox" class="formcheckbox" value=""><span class="formcheckbox_content">Collector</span>
                            <input name="event.licenserequired" type="checkbox" class="formcheckbox" value=""><span class="formcheckbox_content">PPL</span>
                            <input name="event.licenserequired" type="checkbox" class="formcheckbox" value=""><span class="formcheckbox_content">IPRS</span>
                            <input name="event.licenserequired" type="checkbox" class="formcheckbox" value=""><span class="formcheckbox_content">Traffic</span>
                            <input name="event.licenserequired" type="checkbox" class="formcheckbox" value=""><span class="formcheckbox_content">Liquor License</span>
                            <input name="event.licenserequired" type="checkbox" class="formcheckbox" value=""><span class="formcheckbox_content">Ticket Selling License</span>
                            <input name="event.licenserequired" type="checkbox" class="formcheckbox" value=""><span class="formcheckbox_content">BMC Parking</span>
                            <input name="event.licenserequired" type="checkbox" class="formcheckbox" value=""><span class="formcheckbox_content">Parking</span><br />
                            <input name="event.licenserequired" type="checkbox" class="formcheckbox" value=""><span class="formcheckbox_content">Port Trust</span>
                            <input name="event.licenserequired" type="checkbox" class="formcheckbox" value=""><span class="formcheckbox_content">Novex</span>
                            <input name="event.licenserequired" type="checkbox" class="formcheckbox" value=""><span class="formcheckbox_content">Foreign Artist</span>
                            <input name="event.licenserequired" type="checkbox" class="formcheckbox" value=""><span class="formcheckbox_content">DCP Office</span>
                            <input name="event.licenserequired" type="checkbox" class="formcheckbox" value=""><span class="formcheckbox_content">Fire Marshal</span>
                            <input name="event.licenserequired" type="checkbox" class="formcheckbox" value=""><span class="formcheckbox_content">Sale Tax NOC</span>
                            <input name="event.licenserequired" type="checkbox" class="formcheckbox" value=""><span class="formcheckbox_content">Other</span>
                            <input name="event.licenserequired" type="checkbox" class="formcheckbox" value=""><span class="formcheckbox_content">Extra</span>
                            <input name="event.licenserequired" type="checkbox" class="formcheckbox" value=""><span class="formcheckbox_content">Commission</span>
                                
                            </div> 
                        </div>
                        <div class="c

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The value attribute should not be empty value="". If you followed the linked answer you have seen that s:checkbox tag is used. When this tag is rendered it provided the field value for both checked and unchecked checkbox. And remarking Dave Newton's notice in his book "It should have values true or false the only viable values for the moment. These are only values that work correctly with checkbox interceptor."

The tag renders a single checkbox . At the time of this writing it only works well with Boolean (or boolean) properties. Attempting to preselect a checkbox with, for example, a string value only works if the string is true or false .


There's an example Struts 2 checkbox example, which is using such fieldValue attribute.

If you want a deep incite into how checkbox interceptor is working you can read <s:checkbox> not auto populating in struts2 form.

Another example that is using multiple checkboxes Struts 2 multiple check boxes example. Unlike the s:checkbox it doesn't work with any unchecked values and you can map values directly using listKey and listValue attributes.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...