Search code examples
jspuser-interfacecheckboxliferayalloy-ui

Alloy UI checkbox return datatype


If am writing this below mentioned code in my jsp file along with alloy UI, what data type I will be receiving in my Java class as a response.

And what code I have to write to retrieve my selections ?

    <%
            for (Manufacturer manufacturer : manufacturers) {
        %>
        <aui:input type="checkbox" name="location"
            value="<%=manufacturer.getManufacturerId()%>"
            label="<%=manufacturer.getName()%>"/>
        <%
            }
        %>

Solution

  • Call String[] locationValues = ParamUtil.getParameterValues(request, "locationCheckbox"); can get checked values.

    Here is the test result (assume you checked Manufacturer A and Manufacturer C):

    view.jsp:

    <portlet:actionURL var="actionURL" name="myAction" />
    
    <aui:form action="<%=actionURL %>">
        <aui:input type="checkbox" id="a123" label="Manufacturer A" name="location" value="a123"></aui:input>
        <aui:input type="checkbox" id="b456" label="Manufacturer B" name="location" value="b456"></aui:input>
        <aui:input type="checkbox" id="c789" label="Manufacturer C" name="location" value="c789"></aui:input>
        <aui:button type="submit" value="Submit"></aui:button>
    </aui:form>
    

    Testing Java code:

    public void myAction(ActionRequest request, ActionResponse response){
        Enumeration<String> params = request.getParameterNames();
        while (params.hasMoreElements() )
        {
            String para_name = (String)params.nextElement();
            if(para_name.startsWith("location")){
                System.out.println(para_name);
            }
        }
    
        System.out.println("location-getString:" + ParamUtil.getString(request, "location"));
        System.out.println("location-getParameterValues:" + Arrays.toString(ParamUtil.getParameterValues(request, "location")));
    
        System.out.println("locationCheckbox-getString:" + ParamUtil.getString(request, "locationCheckbox"));
        System.out.println("locationCheckbox-getParameterValues:" + Arrays.toString(ParamUtil.getParameterValues(request, "locationCheckbox")));
    }
    

    Console result:

    location
    locationCheckbox
    location-getString:a123
    location-getParameterValues:[a123, b456, c789]
    locationCheckbox-getString:a123
    locationCheckbox-getParameterValues:[a123, c789]
    

    Notes: I test the above codes with Liferay 6.2. It creates two input fields to client side for each <aui:input type="checkbox" ... />:

    <input type=hidden name="<portlet:namespace/>location" ... />
    <input type=checkbox name="<portlet:namespace/>locationCheckbox" ... />
    

    Further Action: If you would like set pre-checked values, add the following sample code to doView function:

    List<String> preCheckedLocation = new ArrayList<String>();
    preCheckedLocation.add("a123");
    preCheckedLocation.add("c789");
    renderRequest.setAttribute("preCheckedLocation",preCheckedLocation);
    

    add checked=true|false to <aui:input> field in view.jsp:

    <%@ page import="java.util.List"%>
    
    ...
    
    <%
        List<String> preCheckedLocation = (List<String>) request.getAttribute("preCheckedLocation");
    %>
    
    ...
    
    <aui:form action="<%=actionURL %>">
        <aui:input type="checkbox" id="a123" label="Manufacturer A" name="location" value="a123" checked='<%= preCheckedLocation.contains("a123") %>' ></aui:input>
        <aui:input type="checkbox" id="b456" label="Manufacturer B" name="location" value="b456" checked='<%= preCheckedLocation.contains("b456") %>' ></aui:input>
        <aui:input type="checkbox" id="c789" label="Manufacturer C" name="location" value="c789" checked='<%= preCheckedLocation.contains("c789") %>' ></aui:input>
        <aui:button type="submit" value="Submit"></aui:button>
    </aui:form>