这里我显示了24个复选框。我想获取操作类中已选中复选框的所有值,并将其作为新记录插入到数据库中。插入按钮后,我将成功获取复选框上的复选框值。我提到了this链接,其中我按照史蒂文先生回答的答案,但在我的帮助下,我只能显示布尔值,但在这里我想要选中复选框的文本值。所以下面是我的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"> 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 & 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>
以下是我的setter和getters类
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;
}
}
以下是我的Action类
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..");
}
}
}
答案 0 :(得分:0)
value
属性不应为空value=""
。如果您按照链接的答案进行了操作,则会看到使用了s:checkbox
标记。呈现此标记时,它为已选中和未选中复选框提供了字段值。并且在Dave Newton的书“它应该具有值true
或false
当前唯一可行的值中注意到Dave Newton的注意。这些只是与checkbox
拦截器一起正常工作的值。”
在 标签呈现单一 复选框 。在撰写本文时,它只是 适用于 布尔 (或布尔)属性。试图预选复选框 例如,字符串值仅在字符串为时才有效 真正 要么 假
有一个示例Struts 2 checkbox example,它正在使用此类fieldValue
属性。
如果你想深入了解checkbox
拦截器如何工作,你可以阅读<s:checkbox>
not auto populating in struts2 form。
使用多个复选框Struts 2 multiple check boxes example的另一个示例。与s:checkbox
不同,它不适用于任何未经检查的值,您可以使用listKey
和listValue
属性直接映射值。
答案 1 :(得分:0)
我刚刚将变量的数据类型从boolean更改为string []。它将存储所选复选框的值。下面是我的jsp。
<s:form action="resultAction" namespace="/">
<h2>
<input name="checkMe" type="checkbox"
class="formcheckbox" value="Rangabhoomi"><span
class="formcheckbox_content">Rangabhoomi</span>
<input
name="checkMe" type="checkbox" class="formcheckbox"
value="Fire Noc"><span class="formcheckbox_content">Fire NOC</span>
</h2>
<input type="submit" value="Submit" />
</s:form>
以下是我的动作类
package com.checkboxInStruts2;
import com.opensymphony.xwork2.ActionSupport;
public class CheckboxAction extends ActionSupport
{
private String checkMe[];
public CheckboxAction() {
// TODO Auto-generated constructor stub
}
public String[] getCheckMe() {
return checkMe;
}
public void setCheckMe(String[] checkMe) {
this.checkMe = checkMe;
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
for(int i=0;i<checkMe.length;i++)
{
System.out.println(checkMe[i]);
}
return "success";
}
}
用于显示在jsp上的标签下面。它将显示所选复选框的字符串值数组。
<s:property value="checkMe"/>