我无法在 MVC 资源命令中从 AJAX 获取 json 数据,因为我在请求参数中获取空值。
我搜索了几个 Stackoverflow 答案来解决这个问题,但我不清楚我哪里出了问题。
下面将提供代码片段和屏幕截图。
控制器 portlet
@Component(
immediate = true,
property = {
"com.liferay.portlet.display-category=Demo",
"com.liferay.portlet.header-portlet-css=/css/main.css",
"com.liferay.portlet.footer-portlet-javascript=/js/main.js",
"com.liferay.portlet.instanceable=true",
"javax.portlet.display-name=Empleado",
"javax.portlet.init-param.template-path=/",
"javax.portlet.init-param.view-template=/view.jsp",
"javax.portlet.name=" + EmpleadoPortletKeys.EMPLEADO,
"javax.portlet.resource-bundle=content.Language",
"javax.portlet.security-role-ref=power-user,user",
"javax.portlet.version=3.0"
},
service = Portlet.class
)
public class EmpleadoPortlet extends MVCPortlet {
public void render(RenderRequest renderRequest, RenderResponse renderResponse) throws IOException, PortletException {
List<Empleado> empleados = EmpleadoLocalServiceUtil.getEmpleados(-1, -1);
renderRequest.setAttribute(EmpleadoPortletKeys.EMPLEADO_ID_TABLE_HEADER, EmpleadoPortletKeys.EMPLEADO_ID_TABLE_HEADER);
renderRequest.setAttribute(EmpleadoPortletKeys.DNI_TABLE_HEADER, EmpleadoPortletKeys.DNI_TABLE_HEADER);
renderRequest.setAttribute(EmpleadoPortletKeys.NOMBRE_TABLE_HEADER, EmpleadoPortletKeys.NOMBRE_TABLE_HEADER);
renderRequest.setAttribute(EmpleadoPortletKeys.APELLIDOS_TABLE_HEADER, EmpleadoPortletKeys.APELLIDOS_TABLE_HEADER);
renderRequest.setAttribute(EmpleadoPortletKeys.CARGO_TABLE_HEADER, EmpleadoPortletKeys.CARGO_TABLE_HEADER);
renderRequest.setAttribute(EmpleadoPortletKeys.ACCIONES_TABLE_HEADER, EmpleadoPortletKeys.ACCIONES_TABLE_HEADER);
renderRequest.setAttribute(EmpleadoPortletKeys.EMPLEADOS, empleados);
super.render(renderRequest, renderResponse);
}
}
view.jsp
<%@ include file="/init.jsp" %>
<portlet:resourceURL id="empleado-resource" var="empleadoResourceURL"/>
<div id="empleado-portlet">
<div class="container">
<table class="table table-hover">
<thead>
<tr>
<th scope="col">${fn:toUpperCase(dni)}</th>
<th scope="col">${nombre}</th>
<th scope="col">${apellidos}</th>
<th scope="col">${cargo}</th>
<th scope="col" class="text-center">${acciones}</th>
</tr>
</thead>
<tbody>
<c:forEach var="employee" items="${employees}">
<tr>
<td>${employee.dni}</td>
<td>${employee.nombre}</td>
<td>${employee.apellidos}</td>
<td>${employee.cargo}</td>
<td>
<div class="btn-actions d-flex justify-content-center">
<div class="mr-1">
<button type="button" id="${employee.empleadoId}" class="btn btn-primary btn-update">
<i class="icon-pencil"></i>
</button>
</div>
<div class="ml-1">
<button type="button" id="${employee.empleadoId}" class="btn btn-danger btn-remove">
<i class="icon-remove"></i>
</button>
</div>
</div>
</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</div>
<script>
let empleadoResourceURL = '${empleadoResourceURL}';
</script>
main.js
function ajaxCall(data) {
$.ajax({
beforeSend : function() {
/*showSpinner();*/
},
url: empleadoResourceURL,
type: "POST",
data: JSON.stringify(data),
contentType: "json",
success: function(data) {
console.log("Respuesta de la solicitud: " + data);
switch(data){
case 'updateEmployee':
console.log('Empleado actualizado: OK');
break;
}
},
error: function(xhr, status) {
console.log("La solicitud ha fallado: " + status);
},
complete: function(xhr, status) {
/*hideSpinner();*/
}
});
}
$(document).ready(function(){
$('#empleado-portlet .btn-actions').on('click','.btn-update',function() {
var data = {};
data.action = 'updateEmployee';
data.selectedEmployee = $(this).attr('id');
console.log(data);
console.log('JSON.stringfy: ' + JSON.stringify(data));
ajaxCall(data);
});
});
MVC 资源命令
@Component(
property = {
"javax.portlet.name=" + EmpleadoPortletKeys.EMPLEADO,
"mvc.command.name=empleado-resource",
"javax.portlet.version=3.0"
},
service = MVCResourceCommand.class
)
public class EmpleadoResource extends BaseMVCResourceCommand{
@Override
public void doServeResource(ResourceRequest resourceRequest, ResourceResponse resourceResponse) throws Exception {
System.out.println("I HAVE PASSED");
ResourceParameters resourceParameters = resourceRequest.getResourceParameters();
PrintWriter printWriter = resourceResponse.getWriter();
//Alternative 1
String action_1 = resourceParameters.getValue(EmpleadoPortletKeys.ACTION);
System.out.println("ACTION_1: " + action_1);
//Alternative 2
String action_2 = ParamUtil.getString(resourceRequest, EmpleadoPortletKeys.ACTION);
System.out.println("ACTION_2: " + action_2);
printWriter.print(EmpleadoPortletKeys.ACTION_REMOVE_EMPLEADO);
}
}
问题
部署 portlet 后,在页面的表格中选择更新按钮。在下图中,您可以通过浏览器控制台看到 AJAX 函数发出请求和响应。
另一方面,在 eclipse 控制台中,您可以看到 MVC Resource Command“doServeResource”方法被执行,因为它在屏幕上打印“我已经通过”。然而,并没有获得“action”键的值。
第一个选项打印“null”,第二个选项打印空白。
- 为什么我无法在 MVC 资源命令中从 AJAX 获取 json 数据?
-我做错了什么?
问候!
我的最佳猜测:Portlet 参数通常需要命名空间,以便很明显地看出它们针对的是哪个 Portlet。 (例如,在 JSP 中,在参数名称前添加
<portlet:namespace/>
)
如果您不想这样做,您可以将
com.liferay.portlet.requires-namespaced-parameters=false
属性添加到您的 portlet,或者您可以访问原始 HttpServletRequest 以获得原始参数值(不推荐)。