Forums de discussion

How to get values of checkboxes iin action class?

thumbnail
Shahbaj Maner, modifié il y a 10 années.

How to get values of checkboxes iin action class?

New Member Publications: 11 Date d'inscription: 29/11/13 Publications récentes
Hello,
I am using checkboxes for hobbies..But i am getting null in action class. please help me how to get that values in action class?
This my .jsp page code

			<input type="checkbox" name="hobbies" value="1"> Art
			<input type="checkbox" name="hobbies" value="2"> Music
			<input type="checkbox" name="hobbies" value="3"> Sports
            <input type="checkbox" name="hobbies" value="4"> Trekking


This my action class where I want that values

        public void test (ActionRequest actionRequest,
        	ActionResponse actionResponse) throws IOException, PortletException {
                String[] hobbiesList =ParamUtil.getParameterValues(actionRequest,"hobbies");
                String[] hobibiesList1 = actionRequest.getParameterValues("hobbies");
        }



Both of these are returning no values...Please help me...
Thanks in advance
thumbnail
Mika Koivisto, modifié il y a 10 années.

RE: How to get values of checkboxes iin action class?

Liferay Legend Publications: 1519 Date d'inscription: 07/08/06 Publications récentes
Per portlet spec you need to namespace your parameter name. Liferay 6.2 now enforces that although there's a setting to turn the old behavior on but I can't remember what it was off the top of my head and it's better to namespace them always.
thumbnail
Mika Koivisto, modifié il y a 10 années.

RE: How to get values of checkboxes iin action class? (Réponse)

Liferay Legend Publications: 1519 Date d'inscription: 07/08/06 Publications récentes
It's all documented in the developer guide's section 3 - Passing information from action phase to render phase. The details are under using portlet namespacing.

For the lazy: This is what you put on your portlet's liferay-portlet.xml to allow non namespaced parameters:

<requires-namespaced-parameters>false</requires-namespaced-parameters>
Javier Vera, modifié il y a 9 années.

RE: How to get values of checkboxes iin action class?

New Member Publications: 18 Date d'inscription: 01/08/14 Publications récentes
hey man why would you teach the lazy way?
i'm having the same issue right now with a biggie portlet....
and i do need these namespace parameters.
if you could share a quick thing on how to achieve the real goal...

the guy up there had tried :

String[] hobbiesList =ParamUtil.getParameterValues(actionRequest,"hobbies");
String[] hobibiesList1 = actionRequest.getParameterValues("hobbies");

and then he got an empty array as result.
i myself am facing this too.
So, i'd like, if you could, you to guide us with the scenario.. or maybe we need to handle this actionRequest in a different way?
anyway, any advice would always be greatly taken.
peace!
thumbnail
Tanweer Ahmed ., modifié il y a 1 année.

RE: How to get values of checkboxes iin action class?

Expert Publications: 322 Date d'inscription: 11/03/10 Publications récentes
Hi Javier,

If you really need the namespaced parameters, use the following for all the html elements
i. with aui html fields e.g. <aui:input name="test" /> OR
ii with normal html fields e.g. <input type="text" name="<portlet:namespace/>test">

Also make sure your Portlet Class is registered in portlet.xml within the <portlet-class>(I did this mistake earlier).
<portlet-class>com.xyz.test.TestPortlet</portlet-class>

-Tanweer
Javier Vera, modifié il y a 9 années.

RE: How to get values of checkboxes iin action class?

New Member Publications: 18 Date d'inscription: 01/08/14 Publications récentes
hi ho, Tanweer.
Thanks for the quick reply.

So far i've tryed these without success.


<aui:input type="checkbox" class="case" name="caseapp[]" value="<%= String.valueOf(sContent.getPrimaryKey()) %>" />


and


<aui:input type="checkbox" class="case" name="caseapp" value="<%= String.valueOf(sContent.getPrimaryKey()) %>" />



note these two have the "name" property set and both are "aui" components.
However, on the action class i get to the method ( i put the portlet-class correctly ), i find empty parameters.

i try to catch these with the following:

String[] arrString = ParamUtil.getParameterValues(actionRequest, "caseapp");
String arrTry = ParamUtil.getString(actionRequest, "caseapp");
String[] arrString2 = ParamUtil.getParameterValues(actionRequest, "caseapp");

yet the result is the same, i know i am doing something wrong i just figure out what.
any idea emoticon besides taking namespace of parameters????
thumbnail
Tanweer Ahmed ., modifié il y a 1 année.

RE: How to get values of checkboxes iin action class?

Expert Publications: 322 Date d'inscription: 11/03/10 Publications récentes
Hi Javier,

As a test I did below and it works as expected.I hope your form uses post method.
<form method="post" id="fm" name="fm" action="<portlet:actionURL />">
    <aui:input type="checkbox" name="caseapp" value="123" />
    <aui:button type="submit" value="Save" />
</form>

final String caseapp = ParamUtil.getString(actionRequest, "caseapp");
        System.out.println(caseapp);

-Tanweer
Javier Vera, modifié il y a 9 années.

RE: How to get values of checkboxes iin action class?

New Member Publications: 18 Date d'inscription: 01/08/14 Publications récentes
Yup, just to clarify this is what i used.



<aui:form name="frm" method="POST" action="<%= oUrl.toString() %>">  
</aui:form>
<!-- daaa daaa --> <div id="panel"> <section> <div> <input type="checkbox" class="Checkbox" name="caseapp" value="<%= String.valueOf(sIndicador.getPrimaryKey()) %>" title="" <%="(isAlreadyRegistered(iCut).size()"> 0)?"Checked":"" %&gt; /&gt; </div> &lt;%} %&gt; </section> </div> [b]<aui:input id="resulttxt" type="hidden" name="resulttxt" />[/b] <aui:button onclick="collectChk();" id="clickmepri" value="Grabar" type="submit"></aui:button>
and to the bottom i added

 
$('#<portlet:namespace />resulttxt').val($('.Checkbox:checked').map(function() {
	    return this.value;
	}).get().join(', '));
}


then i just catch the "resulttxt" in the action method.

thank you for the time emoticon