微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何将值从Ajax传递到Portlet页面

如何解决如何将值从Ajax传递到Portlet页面

我创建了liferay portlet,在其中,我只是从Api获取天气数据,并通过ajax获取数据,如下所示,现在我想将这些json数据从ajax传递到portlet页面并将其保存在portlet文件中。我是liferay的新手,请任何人指导如何做。

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>

这是Portlet文件

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

解决方法

在使用JSP构建HTML输出时: 要与portlet通讯,您可以使用带有各种属性的<portlet:actionURL/>标记,并对结果URL发出POST或GET请求。您当然也可以添加参数,它们通常由<portlet:namespace/>装饰。在Portlet端,您实现了一个动作处理程序(例如,名为processAction的方法,但是MVCPortlet(用作超类)提供了更好的方法-我建议您阅读Liferay教程,因为这些选项的完整说明已经超出了此处单个答案的范围。

另一种选择是仅利用独立于Portlet的REST服务。

还有一条评论:您不应该将数据存储在会话中,只需将其存储为请求/响应属性即可,以使其在呈现期间可在JSP中使用。以后不使用。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。