Spring portlet ajax调用,找不到参数

时间:2014-05-05 05:28:08

标签: ajax liferay-6 spring-portlet-mvc

我正在尝试使用LR 6.2 GA1构建一个示例弹簧portlet。 以下是相同https://docs.google.com/file/d/0By1kU5o_jlrublhUNXIxQ24wODQ/edit

的来源

在ajax上没有提取参数。参数始终为空白。

@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);

}

}

输出 - &GT; 05:23:24,148 INFO [http-bio-8080-exec-119][ProjectSearch:41] Entity name==>

任何人都可以告诉我,我做错了什么?

1 个答案:

答案 0 :(得分:1)

解决方案:

在liferay-portlet.xml中配置需要名称空格参数为false

现在需要将Name间隔参数设置为false,然后仅在Action Request和Render Request中映射表单数据。并且表单数据也将绑定到模型对象或命令对象。

以下是我们需要在liferay-portlet.xml文件中进行的配置

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

Liferay中必需的名称空间参数行为

Liferay 6.2我们必须为输入元素的每个名称附加portlet名称空间,即表单输入元素或请求参数名称,否则portlet操作类忽略没有名称的portlet名称空间的参数。

方案

Jsp页面

在下面的表单中,我们没有附加portlet名称空间来形成输入元素名称。

<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");

       }
}

在上面的情况下,employeeName和employeeAddress表单输入数据在portlet类操作中无法访问。表单元素名称不附加portlet名称空间这样的场景portlet类忽略那些请求参数或表单输入

解决方案:1

需要将标记附加到每个输入元素名称。

Jsp页面

<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类操作方法

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");

       }
}

解决方案:2 我们可以将liferay-portlet.xml文件中的标记值设为false

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

解决方案:3

我们可以使用合金标签库表格标签。当我们使用AUI标签时,它会将portlet名称空间附加到每个输入元素名称。

Jsp页面

<%@ 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"/>

相同
<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