struts2 - How to fetch checkbox (generated inside a iterator tag) values in Action Class -


background

we have list of items , each item can selected checking checkbox. if submit button clicked after selecting necessary checkboxes. now, in our action class, values of checkboxes had been selected, can store selection check box values in database.

environment: struts 2.3.20

when run below mentioned code, jsp creates dynamic checkboxes fine. when select checkboxes , hit save button below mentioned error. if has encountered similar issue can share experience on how resolved issue highly appreciated.

error:

null pointer exception @ line 54 of action class.

( com.testapp.struts2.actions.addproductsaction.execute(addproductsaction.java:54)

addproducts.jsp :

<s:form action="addproducts2campaign" method="post">  <s:iterator value="productslist" status="stat">                        <tr bgcolor="<s:if test="#productstatus.odd == true">#999999</s:if><s:else>#ceddf4</s:else>">     <td align="center">         <s:checkbox name="selectedproductslist[%{#stat.index}]" theme="simple" fieldvalue="%{productid}"/>     </td>                      <td align="center"><s:property value="title" /></td>     <td align="center"><s:property value="sku" /></td>     <td align="center"><s:property value="description" /></td>     <td align="center"><s:property value="price" /></td>    </tr>  </s:iterator>    </s:form> 

addproductsaction.java

public class addproductsaction extends actionsupport implements modeldriven<products>{  private products products = new products();  private map<integer, boolean> selectedproducts;  @override public products getmodel() {   // todo auto-generated method stub   return products; }  public string execute() throws exception {      productdao productdao = new productdao();     arraylist<integer> productids2add = new arraylist();       (line 54:>> below)     (entry<integer, boolean> entry : selectedproducts.entryset()) {         integer key=entry.getkey();         boolean value=entry.getvalue();         if(value == true){             productids2add.add(key);         }     }     products.setproductids2add(productids2add);     productdao.addproducts(products);     return iconstants.success; }// end of execute()    public products getproducts() {     return products; }    public void setproducts(products products) {     this.products = products; }   public void setselectedproducts(map<integer, boolean> selectedproducts) {     this.selectedproducts = selectedproducts; }   } 

product.java

public class products {  private arraylist<integer> productids2add;  public arraylist<integer> getproductids2add() {     return productids2add; }  public void setproductids2add(arraylist<integer> productids2add) {     this.productids2add = productids2add; } } 

i figured out cause. struts.xml didn't have the input property set because of model not getting set values. once added worked.    <?xml version="1.0" encoding="utf-8" ?> <!doctype struts public         "-//apache software foundation//dtd struts configuration 2.0//en"         "http://struts.apache.org/dtds/struts-2.0.dtd">  <struts>    <constant name="struts.devmode" value="true" />    <constant name="struts.custom.i18n.resources" value="applicationresources" />    <constant name="struts.ui.theme" value="simple" />    <package name="default" extends="struts-default">      <action name="addproducts2campaign" method="execute" class="com.myapp.actions.addproductsaction">         **<result name="input">/products/addproducts.jsp</result>**         <result name="success">/products/confirmation.jsp</result>         <result name="error">/error.jsp</result>     </action>          </package>     </struts> 

Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

node.js - How to abort query on demand using Neo4j drivers -