Loading...

Saturday, August 9, 2014

struts multibox example





Struts multibox [multiple check boxes] example


1. create a memeber variables and respective setters and getters in any form class like below

import org.apache.struts.action.ActionForm;
   
   public class LanguageForm extends ActionForm
    private String[] selectedLanguages = {}; 
	private String[] languages = {"Java","J2EE","JSP","STRUTS","Spring"}; 
	
	public String[] getSelectedLanguages() {
		return selectedLanguages;
	}

	public void setSelectedLanguages(String[] selectedLanguages) {
		this.selectedLanguages = selectedLanguages;
	}

	public String[] getLanguages() {
		return languages;
	}

	public void setLanguages(String[] languages) {
		this.languages = languages;
	}
	

2. Add below code inside action class to display languages in JSP

public class LanguageAction extends Action {
	private static final String SUCCESS = "success";

	/**
	 * 
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
     	    String selectedLanguageValues="";
				LanguageForm languageForm = (LanguageForm)form;
			for (String selectedLanguage : languageForm.getSelectedLanguages()) {
				selectedLanguageValues = selectedLanguageValues.concat(selectedLanguage+",");
			}
			if(selectedLanguageValues != null && !selectedLanguageValues.isEmpty()){
				selectedLanguageValues = selectedLanguageValues.substring(0,selectedLanguageValues.length()-1);
			}
			System.out.println("selectedLanguageValues["+selectedLanguageValues+"]");
			return SUCCESS;
			
	}
}

3. JSP code as mentioned below

<%@taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
         <%@taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>
         <%@taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>


     	  
            
              
              
             
         

     

4. Need to do respective configuratio in struts-config.xml like action class, form class etc...
Refer Struts Step By Step example in this blog for configuration related details.