一尘不染

如何在Struts2和JSP中的Action类中仅获取选定复选框的值

jsp

我在这里显示了24个复选框。我想获取动作类中所有已选中复选框的值并将其作为新记录插入数据库中。一旦我在单击按钮后成功获取已选中复选框的值,就将完成插入。这个链接中我遵循了由史蒂文先生回答的答案,但在此帮助下,我只能显示布尔值,但在这里我希望选中复选框的文本值。因此,以下是我的jsp页面。

<%@ 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="crevtblRow"> 
                            <div class="crevtblCell"></div> 
                            <div class="crevtblCell1"></div> 
                            <div class="crevtblCell2"><button type="submit" class="btn btn-primary">Create Event</button></div> 
                        </div>
                    </div>
                </div>
            </div>
          </div>


            </section>
            </s:form>
</body>
</html>

以下是我的二传手课程

package com.ca.pojo;

public class Event {
    public Event() {
        // TODO Auto-generated constructor stub
    }

    private String eventName;
    private String companyName;
    private String contactPerson;
    private String contactNumber;
    private String emailId;
    private String eventVenue;
    private String fromDate;
    private String toDate;
    private String eventTime;
    private String licenserequired;

    public String getEventName() {
        return eventName;
    }

    public void setEventName(String eventName) {
        this.eventName = eventName;
    }

    public String getCompanyName() {
        return companyName;
    }

    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }

    public String getContactPerson() {
        return contactPerson;
    }

    public void setContactPerson(String contactPerson) {
        this.contactPerson = contactPerson;
    }

    public String getContactNumber() {
        return contactNumber;
    }

    public void setContactNumber(String contactNumber) {
        this.contactNumber = contactNumber;
    }

    public String getEmailId() {
        return emailId;
    }

    public void setEmailId(String emailId) {
        this.emailId = emailId;
    }

    public String getEventVenue() {
        return eventVenue;
    }

    public void setEventVenue(String eventVenue) {
        this.eventVenue = eventVenue;
    }

    public String getFromDate() {
        return fromDate;
    }

    public void setFromDate(String fromDate) {
        this.fromDate = fromDate;
    }

    public String getToDate() {
        return toDate;
    }

    public void setToDate(String toDate) {
        this.toDate = toDate;
    }

    public String getEventTime() {
        return eventTime;
    }

    public void setEventTime(String eventTime) {
        this.eventTime = eventTime;
    }

    public String getLicenserequired() {
        return licenserequired;
    }

    public void setLicenserequired(String licenserequired) {
        this.licenserequired = licenserequired;
    }

}

以下是我的动作课

package com.ca.actions;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.ca.database.Database;
import com.ca.pojo.Event;
import com.opensymphony.xwork2.ActionSupport;

public class EventInsertAction extends ActionSupport {

    private String eventId;
    Event event;
    String name;

    public EventInsertAction() {
        // TODO Auto-generated constructor stub
    }


    public String getName() {
        return name;
    }


    public void setName(String name) {
        this.name = name;
    }


    public String getEventId() {
        return eventId;
    }

    public void setEventId(String eventId) {
        this.eventId = eventId;
    }

    public Event getEvent() {
        return event;
    }

    public void setEvent(Event event) {
        this.event = event;
    }

    @Override
    public String execute() throws Exception {

        System.out.println("Event"+event.getLicenserequired());

        // TODO Auto-generated method stub
        System.out.println("Hi Mahi"+name);

        List<Integer> ints = new ArrayList<Integer>();
        int i = 0;

        for (int i1 = 0; i1 < 10000; i1++) {
            ints.add(i1);
        }
        // Collections.shuffle(ints);

        String Id = String.valueOf(ints.get(i++));

        eventId = event.getEventName() + Id;

        System.out.println(eventId);
        try {
            Database database = new Database();
            Connection con = database.Get_Connection();
            System.out.println("Driver Loaded");
            PreparedStatement st = con
                    .prepareStatement("insert into event(EVENT_ID,EVENT_NAME,COMPANY_NAME,CONTACT_PERSON,CONTACT_NO,EMAIL_ID,EVENT_VENUE,FROM_DATE,TO_DATE,EVENT_TIME)"
                            + "values(?,?,?,?,?,?,?,?,?,?)");
            st.setString(1, eventId);
            st.setString(2, event.getEventName());
            st.setString(3, event.getCompanyName());
            st.setString(4, event.getContactPerson());
            st.setString(5, event.getContactNumber());
            st.setString(6, event.getEmailId());
            st.setString(7, event.getEventVenue());
            st.setString(8, event.getFromDate());
            st.setString(9, event.getToDate());
            st.setString(10, event.getEventTime());
            st.executeUpdate();
            System.out.println("success");
            con.close();
        } catch (Exception e) {
            System.out.println(e);
        }

        return "success";
    }

    @Override
    public void validate() {
        // TODO Auto-generated method stub
        super.validate();
        if (event.getEventName().isEmpty()) {
            System.out.println("Event Name");
            addFieldError("event.eventName", "Please Enter Event Name ..");
        }

        if (event.getCompanyName().isEmpty()) {
            addFieldError("event.companyName", "Please Enter Company Name.. ");
        }

        if (event.getContactNumber().isEmpty()) {
            addFieldError("event.contactNumber",
                    "Please Enter Contact Number..");
        } else {
            String expression = "^\\+?[0-9\\-]+\\*?$";
            CharSequence inputStr = event.getContactNumber();
            Pattern pattern = Pattern.compile(expression,
                    Pattern.CASE_INSENSITIVE);
            Matcher matcher = pattern.matcher(inputStr);
            if (!matcher.matches())
                addFieldError("event.contactNumber", "Invalid Contact Number..");

        }

        if (event.getContactPerson().isEmpty()) {
            addFieldError("event.contactPerson",
                    "Please Enter Contact Person Name..");
        }

        if (event.getEmailId().isEmpty()) {
            addFieldError("event.emailId", "Please Enter Email ID..");
        } else {
            String expression = "^[\\w\\-]([\\.\\w])+[\\w]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
            CharSequence inputStr = event.getEmailId();
            Pattern pattern = Pattern.compile(expression,
                    Pattern.CASE_INSENSITIVE);
            Matcher matcher = pattern.matcher(inputStr);
            if (!matcher.matches())
                addFieldError("event.emailId", "Invalid Email Address..");
        }

        if (event.getEventVenue().isEmpty()) {
            addFieldError("event.eventVenue", "Please Enter Event Venue..");
        }

        if (event.getFromDate().isEmpty()) {
            addFieldError("event.fromDate", "Please Enter Date..");
        }

        if (event.getToDate().isEmpty()) {
            addFieldError("event.toDate", "Please Enter To Date..");
        }

        if (event.getEventTime().isEmpty()) {
            addFieldError("event.eventTime", "Please Enter Event Time..");
        }

    }

}

阅读 211

收藏
2020-06-08

共1个答案

一尘不染

value属性不能为空
value=""。如果您遵循链接的答案,您会看到s:checkbox已使用该标签。呈现此标签时,它提供了选中和未选中复选框的字段值。并在Dave
Newton的书中提到“它应该具有值truefalse目前唯一可行的值。这些值只能与checkbox拦截器一起正常工作。”

标签呈现一个复选框。在撰写本文时,它仅适用于布尔(或布尔)属性。尝试预选带有例如字符串值的复选框仅在字符串为true或false时有效。


有一个示例Struts 2复选框示例,它使用了这样的fieldValue属性。

另一个使用多个复选框的示例是Struts2多个复选框的示例。不同于,s:checkbox它不适用于任何未经检查的值,您可以使用listKeylistValue属性直接映射值。

2020-06-08