Question

I am trying to build a sample spring portlet, with LR 6.2 GA1. Below is the source for the same https://docs.google.com/file/d/0By1kU5o_jlrublhUNXIxQ24wODQ/edit

On the ajax the parameters are not being fetched.The parameters always remain blank.

@Controller(value = "ProjectSearch")
@RequestMapping("VIEW")
public class ProjectSearch {
    Log log_ = LogFactoryUtil.getLog(ProjectSearch.class);

@RenderMapping
public String handleRenderRequest(final RenderRequest request,
        final RenderResponse response, Model model) {
    System.out.println("ProjectSearch.handleRenderRequest()");
    return "search_form";
}

@ResourceMapping("getProjectNameSuggestion")
public void getNameSuggestion(ResourceRequest request,
        ResourceResponse response) throws IOException {
    Map<String, String[]> map = request.getParameterMap();
    for (Map.Entry<String, String[]> element : map.entrySet()) {
        log_.info(element.getKey());
    }
    String entityName = ParamUtil.getString(request, "query");
    log_.info("Entity name==>" + entityName);

}

}
@RenderMapping
    public String handleRenderRequest(final RenderRequest request,
            final RenderResponse response, Model model) {
        System.out.println("ProjectSearch.handleRenderRequest()");
        return "search_form";
    }

@ResourceMapping("getProjectNameSuggestion")
public void getNameSuggestion(ResourceRequest request,
        ResourceResponse response) throws IOException {
    Map<String, String[]> map = request.getParameterMap();
    for (Map.Entry<String, String[]> element : map.entrySet()) {
        log_.info(element.getKey());
    }
    String entityName = ParamUtil.getString(request, "query");
    log_.info("Entity name==>" + entityName);

}

}

Output-->05:23:24,148 INFO [http-bio-8080-exec-119][ProjectSearch:41] Entity name==>

Could any body tell me what is that I am doing wrong??

Was it helpful?

Solution

Solution:

Configure Requires Name Spaced Parameters to false in liferay-portlet.xml

Now need to do require Name spaced parameters to false then only form data is mapped in Action Request and Render Request. And also form data will be binding to model object or command object.

The following is configuration we need to do in liferay-portlet.xml file

<requires-namespaced-parameters>false</requires-namespaced-parameters>

Required Name Space Parameter Behavior in Liferay

Liferay 6.2 we have to append portlet Name space for every name of input element i.e. form input elements or request parameters names otherwise portlet action class ignore the parameters which does not have portlet name space to names.

Scenario

Jsp page

In the following form we are not appending portlet name space to form input element names.

<portlet:actionURL var="addEmployeeActionURL" name="addEmployee">
<portlet:param name="<%=ActionRequest.ACTION_NAME%>" value="addEmployee"/>
</portlet:actionURL>

<form action="<%=addEmployeeActionURL%>" name="emplyeeForm"  method="POST">
Employee Name<br/>
<input  type="text" name="employeeName" id="employeeName"/><br/>
Employee Address<br/>
<input type="text" name="employeeAddress" id="employeeName"/><br/>
<input type="submit" name="addEmployee" id="addEmployee" value="Add Employee"/>
</form>

Portlet Class Action Method

public class EmplyeePortletAction extends MVCPortlet {
public void addEmployee(ActionRequest actionRequest,
ActionResponse actionResponse) throws IOException, PortletException {

String employeeName=ParamUtil.getString(actionRequest,"employeeName");
String employeeAddress=ParamUtil.getString(actionRequest,"employeeAddress");

       }
}

In above case employeeName and employeeAddress form input data not accessible in portlet class action .The form elements name are not appended with portlet name space such scenarios portlet class ignore those request parameters or form inputs

Solution:1

Need to append tag to every input element name.

Jsp page

<portlet:actionURL var="addEmployeeActionURL" name="addEmployee">
<portlet:param name="<%=ActionRequest.ACTION_NAME%>" value="addEmployee"/>
<portlet:param name="requestParam" value=" requestParamValue"/>
</portlet:actionURL>

<form action="<%=addEmployeeActionURL%>" name="emplyeeForm"  method="POST">
Employee Name<br/>
<input  type="text" name="<portlet:namespace/>employeeName" id="<portlet:namespace/>employeeName"/><br/>
Employee Address<br/>
<input type="text" name="<portlet:namespace/>employeeAddress" id="<portlet:namespace/>employeeName"/><br/>
<input type="submit" name="addEmployee" id="addEmployee" value="Add Employee"/>
</form>

Portlet Class Action Method

public class EmplyeePortletAction extends MVCPortlet {
public void addEmployee(ActionRequest actionRequest,
ActionResponse actionResponse) throws IOException, PortletException {

String employeeName=ParamUtil.getString(actionRequest,"employeeName");
String employeeAddress=ParamUtil.getString(actionRequest,"employeeAddress");
String requestParamValue=ParamUtil.getString(actionRequest,"requestParam");

       }
}

Solution:2 We can make it false to following tag value in liferay-portlet.xml file

<requires-namespaced-parameters>false</requires-namespaced-parameters>

Solution:3

We can use alloy tag library form tags. When we use AUI tags it will append portlet name space to each input element name.

Jsp page

<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>

<aui:input type="text" name="employeeAddress" id="employeeName"/><br/>
<aui:input type="submit" name="addEmployee" id="addEmployee" value="Add Employee"/




<input type="text" name="<portlet:namespace/>employeeAddress" id="<portlet:namespace/>employeeName"/>

Is same As

<aui:input type="text" name="employeeAddress" id="employeeName"/>

http://www.liferaysavvy.com/2013/12/liferay-spring-portlet.html http://www.liferaysavvy.com/2014/04/liferay-mvc-portlet-development.html

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