I have create liferay portlet in which, i simply get the weather data from Api and get the data through ajax as given below , now i want to pass these json data from ajax to portlet page and save it some variables on portlet file. I am new to liferay,Kindly anyone guide how to do this.
init.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %><%@
taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet" %>
<%@
taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %>
<%@
taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>
<liferay-theme:defineObjects />
<portlet:defineObjects />
<select id="list" >
<option>Pakistan</option>
<option>India</option>
<option>America</option>
<option>China</option>
<option>Canda</option>
</select >
<br><br>
<button onClick="setup()" id="btnSubmit">Submit</button>
<div id='demo'></div>
<script>
var country
function setup()
{
selectElement=document.querySelector('#list');
var country=selectElement.value;
$.ajax({
dataType: 'json',
success: function(data)
{
document.getElementById("demo").innerHTML =
'<br>country : '+data.name+
'<br>temprature : '+data.main.temp +
' °F<br>Percipitation : '+data.clouds.all +
' %<br>wind speed : '+data.wind.speed +
'<br>humidity : '+data.main.humidity
},
url: 'http://api.openweathermap.org/data/2.5/weather?q='+country+'&appid=e53a861b8514a25b48d943fec2b4fcd7&units=imperial'
});
}
</script>
here is portlet file
package assignment1.portlet;
import assignment1.constants.Assignment1PortletKeys;
import com.liferay.portal.kernel.portlet.bridges.mvc.MVCPortlet;
import java.io.IOException;
import javax.portlet.Portlet;
import javax.portlet.PortletException;
import javax.portlet.PortletSession;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import org.osgi.service.component.annotations.Component;
@Component(
immediate = true,
property = {
"com.liferay.portlet.display-category=category.sample",
"com.liferay.portlet.header-portlet-css=/css/main.css",
"com.liferay.portlet.instanceable=true",
"javax.portlet.display-name=Assignment1",
"javax.portlet.init-param.template-path=/",
"javax.portlet.init-param.view-template=/view.jsp",
"javax.portlet.name=" + Assignment1PortletKeys.ASSIGNMENT1,
"javax.portlet.resource-bundle=content.Language",
"javax.portlet.security-role-ref=power-user,user",
"com.liferay.portlet.private-session-attributes=false"
},
service = Portlet.class
)
public class Assignment1Portlet extends MVCPortlet
{
@Override
public void doView(RenderRequest renderRequest, RenderResponse renderResponse)
throws IOException, PortletException {
// TODO Auto-generated method stub
PortletSession session=renderRequest.getPortletSession();
session.setAttribute("LIFERAY_SHARED_sessionMessage", "Sent Data From First Portlet",PortletSession.APPLICATION_SCOPE);
super.doView(renderRequest, renderResponse);
}
}
As you're using JSPs to build your HTML output: To communicate back to a portlet, you'd use the
<portlet:actionURL/>tag with its various attributes and issue POST or GET requests to the resulting URL. You can also add parameters (of course), and they typically get decorated by<portlet:namespace/>. On the portlet-side you implement an action-handler (e.g. a method namedprocessAction, butMVCPortlet(which you use as superclass) offers nicer ways - I'd recommend to go through a Liferay Tutorial, as a full description for these options goes way beyond the scope of a single answer here.Yet another option is to just utilize portlet-independent REST services.
And a comment: You shouldn't store the data in a session, it's enough to store it as a request/response attribute to have it available in the JSP during render time. It's not being used later.