留言板

How to create portlet with JSON response

David Knol,修改在7 年前。

How to create portlet with JSON response

Junior Member 帖子: 29 加入日期: 15-6-19 最近的帖子
Hi,
I am trying to create portlet, which after invoking returns JSON with data from database. What I'm tryintg to do is pretty similar to GetLayoutsAction class, which extends JSONAction. Can you guide me step by step what should I do? I have created new MVCPortlet so far. What should I do next? I'm fresh with Liferay.
thumbnail
David H Nebinger,修改在7 年前。

RE: How to create portlet with JSON response

Liferay Legend 帖子: 14919 加入日期: 06-9-2 最近的帖子
You can return json using the serveResource() method in the portlet, but I'd suggest going the service builder route so you can leverage the whole /api/jsonws entry point rather than trying to do within a portlet.
David Knol,修改在7 年前。

RE: How to create portlet with JSON response

Junior Member 帖子: 29 加入日期: 15-6-19 最近的帖子
Ok, thank you. Another thing. I've genereted a new MVCPortlet using Blade, so I have something like that:

package example.portlet;

@Component(
	immediate = true,
	property = {
		"com.liferay.portlet.display-category=category.sample",
		"com.liferay.portlet.instanceable=true",
		"javax.portlet.display-name=Example Portlet",
		"javax.portlet.init-param.template-path=/",
		"javax.portlet.init-param.view-template=/view.jsp",
		"javax.portlet.resource-bundle=content.Language",
		"javax.portlet.security-role-ref=power-user,user"
	},
	service = Portlet.class
)
public class ExamplePortlet extends MVCPortlet {

	@Override
	public void serveResource(ResourceRequest request, ResourceResponse response) throws IOException, PortletException {
		...
	}
	
}


What I'd like to do is to call this portlet from existing JS file using A.io.request and process received JSON. What will be URL of that resource? Is there something more I should do to make it works?