Question

Hi I am new to struts 2 , Can somebody tell me how to get multiple checkbox value in struts 2. So far i have developed the code which is as follows. i am getting the error

tag 'checkboxlist', field 'list', name 'subscribe': The requested list key 'subscription' could not be resolved as a collection/array/map/enumeration/iterator type. Example: people or people.{name} - [unknown location]
    at org.apache.struts2.components.Component.fieldError(Component.java:240)
    at org.apache.struts2.components.Component.findValue(Component.java:333)
    at org.apache.struts2.components.ListUIBean.evaluateExtraParams(ListUIBean.java:80)
    at org.apache.struts2.components.UIBean.evaluateParams(UIBean.java:875)
    at org.apache.struts2.components.UIBean.end(UIBean.java:523)
    at org.apache.struts2.views.jsp.ComponentTagSupport.doEndTag(ComponentTagSupport.java:42)
    at jsp_servlet.__step1._jsp__tag6(__step1.java:371)
    at jsp_servlet.__step1._jsp__tag0(__step1.java:148)
    at jsp_servlet.__step1._jspService(__step1.java:86)
    at weblogic.servlet.jsp.JspBase.service(JspBase.java:34)
    at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)

My jsp page to get the values is

 <link href="style.css" rel="stylesheet" type="text/css"/>
 <%@ taglib uri="/struts-tags" prefix="s" %>

 <table class="profiletable" align="center">
<tr>
    <td align="center"><h2>Upload User Profile</h2></td>
</tr>
<tr>
    <td align="center"><s:form action="upload" method="post">
<s:textfield name="fname" label="First Name"></s:textfield>
<s:textfield name="lname" label="Last Name"></s:textfield>
<s:radio name="mstatus" label="Martial Status" list="{'Single','Married'}"></s:radio>
<s:radio name="gender" label="Gender" list="{'male','female'}"></s:radio>
<s:select list="{'India','USA','UK','Germany','France','Australia'}" label="Country" name="country"></s:select>
 <s:checkboxlist label="Letter you want to subscribe" name="subscribe" list="subscription" />
  <s:submit value="upload profile" align="center"></s:submit>
</s:form></td>
</tr>
 </table>

my jsp page to show the values is

<%@ taglib uri="/struts-tags" prefix="p" %>
<h2>Profile uploaded sucessfully</h2>
 First Name:<p:property value="fname"/><br/>
Last Name:<p:property value="lname"/><br/>
Martial status:<p:property value="mstatus"/><br/>
Gender:<p:property value="gender"/><br/>
Country:<p:property value="country"/><br/>
Letters Subscribed:<p:property value="subscribe"/>/>

my action file is

package com.javapoint;

import java.util.ArrayList;
 import java.util.List;

import com.opensymphony.xwork2.ActionSupport;



 public class UploadProfile extends ActionSupport {

/**
 * 
 */
private static final long serialVersionUID = 1L;
private String fname;
private String lname;
private String mstatus;
private String gender;
private String country;
private String subscribe;
private List<String> subscription;

public String getFname() {
    System.out.println("inside getfname");
    return fname;
}
public void setFname(String fname) {
    System.out.println("inside getfname");
    System.out.println("the firstname set is "+fname);
    this.fname = fname;
}
public String getLname() {

    return lname;
}
public void setLname(String lname) {
    this.lname = lname;
    System.out.println("the lastname set is "+ lname);
}
public String getMstatus() {
    return mstatus;
}
public void setMstatus(String mstatus) {
    this.mstatus = mstatus;
}
public String getGender() {
    return gender;
}
public void setGender(String gender) {
    this.gender = gender;
}
public String getCountry() {
    return country;
}
public void setCountry(String country) {
    this.country = country;
}

public String getSubscribe() {
    return subscribe;
}
public void setSubscribe(String subscribe) {
    this.subscribe = subscribe;
}
public List<String> getSubscription() {
    return subscription;
}
public void setSubscription(List<String> subscription) {
    this.subscription = subscription;
}
public UploadProfile(){
    subscription=new ArrayList<String>();
    subscription.add("Politics");
    subscription.add("Sports");
    subscription.add("Editorial");
    subscription.add("Gadgets");
    subscription.add("Overdrive");
}
public String execute()throws Exception{
    System.out.println("inside execute");
    if(fname!=null){
    return "profileuploaded";
    }else{
        return "error";

    }
}

public String display(){

    return NONE;
}


}

My struts.xml file is

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts
Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
 <package name="default" extends="struts-default">

<action name="product" class="com.javapoint.Product">
<result name="success">welcome.jsp</result>
</action>
<action name="upload" class="com.javapoint.UploadProfile">
<result name="profileuploaded">ProfileUploaded.jsp</result>
 <result name="error">index.jsp</result>
</action>
</package>
  </struts>    

and my web.xml file is

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
  </filter-class>
 </filter>
  <filter-mapping>
   <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <welcome-file-list>
  <welcome-file>step1.jsp</welcome-file>
   </welcome-file-list>

Was it helpful?

Solution

  1. struts.xml:

<action name="subscription_listing" method="UploadProfile" class="com.javapoint.UploadProfile" > <result name="none">/your_jsppage_where_you_have_checkboxlist</result> </action>

2.in Uploadprofile Action class modified Uploadprofile method:

public String UploadProfile(){
subscription=new ArrayList<String>();
subscription.add("Politics");
subscription.add("Sports");
subscription.add("Editorial");
subscription.add("Gadgets");
subscription.add("Overdrive");
return NONE;

}

[3]. Now Directly call subscription_listing action.

Do you got it?

OTHER TIPS

As you are new to Struts2 hopefully this example will help you working with multiple checkbox in struts2

After surfing and implementing the above sugesstion ,I have come to solution that call the action file directly in case of chekbox,although there can be alternates but as of now i am unaware of it.

the problem is with my struts.xml it should be

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts
 Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
 <struts>
 <package name="default" extends="struts-default">

 <action name="upload" class="com.javapoint.UploadProfile">
 <result name="profileuploaded">ProfileUploaded.jsp</result>
  <result name="error">index.jsp</result>


 </action>
<action name="UploadProfile" class="com.javapoint.UploadProfile" method="display">
    <result name="none">/step1.jsp</result>
   </action>

</package>
</struts>    

and the final action file is

package com.javapoint;

import java.util.ArrayList; import java.util.List;

import com.opensymphony.xwork2.ActionSupport;

public class UploadProfile extends ActionSupport {

/**
 * 
 */
private static final long serialVersionUID = 1L;
private String fname;
private String lname;
private String mstatus;
private String gender;
private String country;
private String[] defaultsubscription;
private String[] yoursubscription;
private List<String> subscription;

public String getFname() {
    System.out.println("inside getfname");
    return fname;
}
public void setFname(String fname) {
    System.out.println("inside getfname");
    System.out.println("the firstname set is "+fname);
    this.fname = fname;
}
public String getLname() {

    return lname;
}
public void setLname(String lname) {
    this.lname = lname;
    System.out.println("the lastname set is "+ lname);
}
public String getMstatus() {
    return mstatus;
}
public void setMstatus(String mstatus) {
    this.mstatus = mstatus;
}
public String getGender() {
    return gender;
}
public void setGender(String gender) {
    this.gender = gender;
}
public String getCountry() {
    return country;
}
public void setCountry(String country) {
    this.country = country;
System.out.println(country);
}

public String[] getYoursubscription() {
    return yoursubscription;
}
public void setYoursubscription(String[] yoursubscription) {
    this.yoursubscription = yoursubscription;
}


public UploadProfile(){
    System.out.println("inside constructor");
    subscription=new ArrayList<String>();
    subscription.add("Politics");
    subscription.add("Sports");
    subscription.add("Editorial");
    subscription.add("Gadgets");
    subscription.add("Overdrive");
    }

public String[] getDefaultSubscription(){
    return new String [] {"Politics", "Sports"};
}
public List<String> getSubscription() {
    return subscription;
}
public void setSubscription(List<String> subscription) {
    this.subscription = subscription;
}


public String execute()throws Exception{
    System.out.println("inside execute");
    System.out.println(yoursubscription.toString()+"   "+subscription+"    "+defaultsubscription);
    if(fname!=null){
    return "profileuploaded";
    }else{
        return "error";

    }
}
public String display() {
    return NONE;
}



}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top