<i id='575S6'><tr id='575S6'><dt id='575S6'><q id='575S6'><span id='575S6'><b id='575S6'><form id='575S6'><ins id='575S6'></ins><ul id='575S6'></ul><sub id='575S6'></sub></form><legend id='575S6'></legend><bdo id='575S6'><pre id='575S6'><center id='575S6'></center></pre></bdo></b><th id='575S6'></th></span></q></dt></tr></i><div id='575S6'><tfoot id='575S6'></tfoot><dl id='575S6'><fieldset id='575S6'></fieldset></dl></div>

    <small id='575S6'></small><noframes id='575S6'>

      • <bdo id='575S6'></bdo><ul id='575S6'></ul>
      <tfoot id='575S6'></tfoot>

      1. <legend id='575S6'><style id='575S6'><dir id='575S6'><q id='575S6'></q></dir></style></legend>

        如何在 Struts 2 和 JSP 的 Action 类中仅取值

        时间:2023-09-24
      2. <small id='TrmzI'></small><noframes id='TrmzI'>

          <i id='TrmzI'><tr id='TrmzI'><dt id='TrmzI'><q id='TrmzI'><span id='TrmzI'><b id='TrmzI'><form id='TrmzI'><ins id='TrmzI'></ins><ul id='TrmzI'></ul><sub id='TrmzI'></sub></form><legend id='TrmzI'></legend><bdo id='TrmzI'><pre id='TrmzI'><center id='TrmzI'></center></pre></bdo></b><th id='TrmzI'></th></span></q></dt></tr></i><div id='TrmzI'><tfoot id='TrmzI'></tfoot><dl id='TrmzI'><fieldset id='TrmzI'></fieldset></dl></div>
          <tfoot id='TrmzI'></tfoot>
              <bdo id='TrmzI'></bdo><ul id='TrmzI'></ul>

              <legend id='TrmzI'><style id='TrmzI'><dir id='TrmzI'><q id='TrmzI'></q></dir></style></legend>
                <tbody id='TrmzI'></tbody>

                • 本文介绍了如何在 Struts 2 和 JSP 的 Action 类中仅取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在显示 24 个复选框.我想获取动作类中选中复选框的所有值并将其作为新记录插入数据库中.一旦我成功获取按钮单击时选中复选框的值,插入将完成.

                  我提到了 this 链接我遵循了 steven sir 的回答,但在此帮助下,我只能显示布尔值,但在这里我希望选中复选框的文本值.

                  下面是我的 JSP 页面.

                  <%@ page language="java"内容类型=文本/html;字符集=ISO-8859-1";pageEncoding="ISO-8859-1"%><%@taglib uri="/struts-tags";前缀=s";%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 过渡//EN"http://www.w3.org/TR/html4/loose.dtd"><html><头></头><身体><s:form action="eventInsertAction"><!-- 主要内容--><section class=内容"><!-- 小盒子(Stat box)--><div 类=行"><div 类=contetpanel">

                  <div 类=crevtbl"><div 类=crevtblRow"><div class=crevtblCell">事件名称</div><div class=crevtblCell1">:</div><div class="crevtblCell2"><输入名称="event.eventName"类=formtextfield";type="text"><s:fielderror fieldName="event.eventName"/></div></div><div 类=crevtblRow"><div class=<crevtblCell>公司名称</div><div class=crevtblCell1">:</div><div class="crevtblCell2"><输入名称="event.companyName";类=formtextfield";类型=文本";><s:fielderror fieldName="event.companyName"/></div></div><div 类=crevtblRow"><div class="crevtblCell">联系人姓名</div><div class=crevtblCell1">:</div><div class="crevtblCell2"><输入名称="event.contactPerson"类=formtextfield";类型=文本";><s:fielderror fieldName="event.contactPerson"/></div></div><div 类=crevtblRow"><div class="crevtblCell">联系方式</div><div class=crevtblCell1">:</div><div class="crevtblCell2"><输入名称="event.contactNumber"类=formtextfield";类型=文本";><s:fielderror fieldName="event.contactNumber"/></div></div><div 类=crevtblRow"><div class="crevtblCell">电子邮件</div><div class=crevtblCell1">:</div><div class="crevtblCell2"><输入名称="event.emailId"类=formtextfield";类型=文本";><s:fielderror fieldName="event.emailId"/></div></div><div 类=crevtblRow"><div class=crevtblCell">活动地点</div><div class=crevtblCell1">:</div><div class="crevtblCell2"><输入名称="event.eventVenue";类=formtextfield";类型=文本";><s:fielderror fieldName="event.eventVenue"/></div></div><div 类=crevtblRow"><div class=crevtblCell">事件日期</div><div class=crevtblCell1">:</div><div class="crevtblCell2">From : <input name="event.fromDate"类=formtextfield1";类型=文本";占位符=YYYY/MM/DD">&nbsp;&nbsp;&nbsp;&nbsp;到:<输入名称=event.toDate";类=formtextfield1";类型=文本";placeholder="YYYY/MM/DD"></div></div><div 类=crevtblRow"><div class="crevtblCell">事件时间</div><div class=crevtblCell1">:</div><div class="crevtblCell2"><输入名称="event.eventTime"类=formtextfield1";类型=文本";占位符=HH:MM AM/PM"><s:fielderror fieldName="event.eventTime"/></div></div><div 类=crevtblRow"><div class=crevtblCell">需要许可证</div><div class=crevtblCell1">:</div><div 类=crevtblCell2"><输入名称=event.licenserequired";类型=复选框";类=表单复选框";value=""><span class="formcheckbox_content">Rangabhoomi</span><输入名称=event.licenserequired";类型=复选框";类=表单复选框";value=""><span class="formcheckbox_content">Fire NOC</span><输入名称=event.licenserequired";类型=复选框";类=表单复选框";value=""><span class="formcheckbox_content">Fire Engine</span><输入名称=event.licenserequired";类型=复选框";类=表单复选框";value=""><span class="formcheckbox_content">Premises &amp;NOC</span><输入名称=event.licenserequired";类型=复选框";类=表单复选框";value=""><span class="formcheckbox_content">性能</span><输入名称=event.licenserequired";类型=复选框";类=表单复选框";value=""><span class="formcheckbox_content">PWD</span><输入名称=event.licenserequired";类型=复选框";类=表单复选框";value=""><span class="formcheckbox_content">当地警察</span><br/><输入名称=event.licenserequired";类型=复选框";类=表单复选框";value=""><span class="formcheckbox_content">收集器</span><输入名称=event.licenserequired";类型=复选框";类=表单复选框";value=""><span class="formcheckbox_content">PPL</span><输入名称=event.licenserequired";类型=复选框";类=表单复选框";value=""><span class="formcheckbox_content">IPRS</span><输入名称=event.licenserequired";类型=复选框";类=表单复选框";value=""><span class="formcheckbox_content">流量</span><输入名称=event.licenserequired";类型=复选框";类=表单复选框";value=""><span class="formcheckbox_content">Liquor License</span><输入名称=event.licenserequired";类型=复选框";类=表单复选框";value=""><span class="formcheckbox_content">售票许可证</span><输入名称=event.licenserequired";类型=复选框";类=表单复选框";value=""><span class="formcheckbox_content">BMC Parking</span><输入名称=event.licenserequired";类型=复选框";类=表单复选框";value=""><span class="formcheckbox_content">停车</span><br/><输入名称=event.licenserequired";类型=复选框";类=表单复选框";value=""><span class="formcheckbox_content">Port Trust</span><输入名称=event.licenserequired";类型=复选框";类=表单复选框";value=""><span class="formcheckbox_content">Novex</span><输入名称=event.licenserequired";类型=复选框";类=表单复选框";value=""><span class="formcheckbox_content">外国艺术家</span><输入名称=event.licenserequired";类型=复选框";类=表单复选框";value=""><span class="formcheckbox_content">DCP 办公室</span><输入名称=event.licenserequired";类型=复选框";类=表单复选框";value=""><span class="formcheckbox_content>Fire Marshal</span><输入名称=event.licenserequired";类型=复选框";类=表单复选框";value=""><span class="formcheckbox_content>Sale Tax NOC</span><输入名称=event.licenserequired";类型=复选框";类=表单复选框";value=""><span class="formcheckbox_content">Other</span><输入名称=event.licenserequired";类型=复选框";类=表单复选框";value=""><span class="formcheckbox_content">Extra</span><输入名称=event.licenserequired";类型=复选框";类=表单复选框";value=""><span class="formcheckbox_content">Commission</span></div></div><div 类=crevtblRow"><div 类=crevtblCell"></div><div 类=crevtblCell1"></div><div 类=crevtblCell2"><按钮类型=提交"class="btn btn-primary">创建事件</button></div></div></div></div></div></div></部分></s:form></身体></html>

                  下面是我的 setter 和 getter 类

                  package com.ca.pojo;公共类事件{公共事件(){//TODO 自动生成的构造函数存根}私人字符串事件名称;私人字符串公司名称;私人字符串联系人;私人字符串联系人号码;私人字符串 emailId;私人字符串事件地点;私人字符串 fromDate;私有字符串 toDate;私人字符串事件时间;需要私有字符串许可证;公共字符串 getEventName() {返回事件名称;}公共无效 setEventName(字符串事件名称){this.eventName = 事件名;}公共字符串 getCompanyName() {返回公司名称;}公共无效 setCompanyName(字符串公司名称){this.companyName = 公司名称;}公共字符串 getContactPerson() {返回联系人;}公共无效setContactPerson(字符串联系人){this.contactPerson = 联系人;}公共字符串 getContactNumber() {返回联系人号码;}公共无效setContactNumber(字符串联系人号码){this.contactNumber = 联系人号码;}公共字符串 getEmailId() {返回电子邮件ID;}公共无效 setEmailId(字符串 emailId){this.emailId = emailId;}公共字符串 getEventVenue() {返回事件地点;}公共无效 setEventVenue(字符串 eventVenue){this.eventVenue = eventVenue;}公共字符串 getFromDate() {从日期返回;}公共无效 setFromDate(字符串 fromDate){this.fromDate = fromDate;}公共字符串 getToDate() {返回日期;}公共无效 setToDate(字符串 toDate){this.toDate = toDate;}公共字符串 getEventTime() {返回事件时间;}公共无效setEventTime(字符串事件时间){this.eventTime = 事件时间;}公共字符串 getLicenserequired() {需要返回许可证;}public void setLicenserequired(String licenserequired) {this.licenserequired = 需要许可;}}

                  下面是我的 Action 类

                  package com.ca.actions;导入java.sql.Connection;导入 java.sql.DriverManager;导入 java.sql.PreparedStatement;导入 java.util.ArrayList;导入 java.util.List;导入 java.util.regex.Matcher;导入 java.util.regex.Pattern;导入 com.ca.database.Database;导入 com.ca.pojo.Event;导入 com.opensymphony.xwork2.ActionSupport;公共类 EventInsertAction 扩展 ActionSupport {私人字符串事件ID;事件事件;字符串名称;公共事件插入操作(){//TODO 自动生成的构造函数存根}公共字符串 getName() {返回名称;}公共无效集合名称(字符串名称){this.name = 名称;}公共字符串 getEventId() {返回事件ID;}公共无效 setEventId(字符串 eventId){this.eventId = eventId;}公共事件 getEvent() {返回事件;}公共无效setEvent(事件事件){this.event = 事件;}@覆盖公共字符串执行()抛出异常{System.out.println(事件"+event.getLicenserequired());//TODO 自动生成的方法存根System.out.println("Hi Mahi"+name);列表<整数>ints = new ArrayList<整数>();诠释 i = 0;for (int i1 = 0; i1 <10000; i1++) {ints.add(i1);}//Collections.shuffle(ints);字符串 ID = String.valueOf(ints.get(i++));eventId = event.getEventName() + Id;System.out.println(eventId);尝试 {数据库数据库 = 新数据库();连接 con = database.Get_Connection();System.out.println(驱动加载");PreparedStatement st = con.prepareStatement("插入事件(EVENT_ID,EVENT_NAME,COMPANY_NAME,CONTACT_PERSON,CONTACT_NO,EMAIL_ID,EVENT_VENUE,FROM_DATE,TO_DATE,EVENT_TIME)"+ "值(?,?,?,?,?,?,?,?,?,?)");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(成功");con.close();} 捕捉(异常 e){System.out.println(e);}返回成功";}@覆盖公共无效验证(){//TODO 自动生成的方法存根超级验证();if (event.getEventName().isEmpty()) {System.out.println(事件名称");addFieldError("event.eventName", "请输入事件名称..");}if (event.getCompanyName().isEmpty()) {addFieldError("event.companyName", "请输入公司名称..");}if (event.getContactNumber().isEmpty()) {addFieldError("event.contactNumber",请输入联系电话..");} 别的 {字符串表达式 = "^\+?[0-9\-]+\*?$";CharSequence inputStr = event.getContactNumber();模式模式 = Pattern.compile(表达式,Pattern.CASE_INSENSITIVE);匹配器 matcher = pattern.matcher(inputStr);如果(!matcher.matches())addFieldError("event.contactNumber", "Invalid Contact Number..");}if (event.getContactPerson().isEmpty()) {addFieldError("event.contactPerson",请输入联系人姓名..");}if (event.getEmailId().isEmpty()) {addFieldError("event.emailId", "请输入电子邮件 ID..");} 别的 {字符串表达式 = "^[\w\-]([\.\w])+[\w]+@([\w\-]+\.)+[AZ]{2,4}$";CharSequence inputStr = event.getEmailId();模式模式 = Pattern.compile(表达式,Pattern.CASE_INSENSITIVE);匹配器 matcher = pattern.matcher(inputStr);如果(!matcher.matches())addFieldError("event.emailId", "Invalid Email Address..");}if (event.getEventVenue().isEmpty()) {addFieldError("event.eventVenue", "请输入活动地点..");}if (event.getFromDate().isEmpty()) {addFieldError("event.fromDate", "请输入日期..");}if (event.getToDate().isEmpty()) {addFieldError("event.toDate", "请输入截止日期..");}if (event.getEventTime().isEmpty()) {addFieldError("event.eventTime", "请输入事件时间..");}}}

                  解决方案

                  value 属性不能为空value="".如果您遵循链接的答案,您会看到使用了 s:checkbox 标记.呈现此标记时,它为选中和未选中的复选框提供了字段值.并评论戴夫牛顿在他的书中的通知它应该具有值 truefalse 是目前唯一可行的值.这些只是与 checkbox 一起正常工作的值 拦截器."

                  <块引用>

                  标签呈现单个复选框.在撰写本文时,仅与布尔值(或布尔)属性.尝试预选复选框例如,字符串值仅在字符串为真的或者错误的.

                  <小时>

                  有一个示例 Struts 2 复选框示例,这是使用这样的 fieldValue 属性.

                  如果您想深入了解 checkbox 拦截器的工作原理,可以阅读 <s:checkbox> 不会自动填充到 struts2 表单中.

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

                  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="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>  
                  

                  Below is my setters and getters class

                  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;
                      }
                      
                  }  
                  

                  Below is my Action class

                  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..");
                          }
                  
                      }
                  
                  }  
                  

                  解决方案

                  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.

                  这篇关于如何在 Struts 2 和 JSP 的 Action 类中仅取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何分配 &lt;s:property value="a"&gt;一个jsp变量的值 下一篇:获取操作类中 i18n 拦截器检测到的用户语言环境

                  相关文章

                  • <bdo id='jIBy4'></bdo><ul id='jIBy4'></ul>

                  <i id='jIBy4'><tr id='jIBy4'><dt id='jIBy4'><q id='jIBy4'><span id='jIBy4'><b id='jIBy4'><form id='jIBy4'><ins id='jIBy4'></ins><ul id='jIBy4'></ul><sub id='jIBy4'></sub></form><legend id='jIBy4'></legend><bdo id='jIBy4'><pre id='jIBy4'><center id='jIBy4'></center></pre></bdo></b><th id='jIBy4'></th></span></q></dt></tr></i><div id='jIBy4'><tfoot id='jIBy4'></tfoot><dl id='jIBy4'><fieldset id='jIBy4'></fieldset></dl></div>
                  1. <legend id='jIBy4'><style id='jIBy4'><dir id='jIBy4'><q id='jIBy4'></q></dir></style></legend>

                      <small id='jIBy4'></small><noframes id='jIBy4'>

                    1. <tfoot id='jIBy4'></tfoot>