servlets - 如何将值从 ajax 传递到 portlet 页面
问题描述
我已经创建了 liferay portlet,我只是从 Api 获取天气数据并通过 ajax 获取数据,如下所示,现在我想将这些 json 数据从 ajax 传递到 portlet 页面并将它的一些变量保存在 portlet 文件中。我是liferay的新手,请任何人指导如何做到这一点。
初始化.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 中可用。以后不用了。
推荐阅读
- python - Python程序连续运行几个小时后无错误冻结
- html - Material-ui:如何避免动态内容因为滚动条而“跳跃”?
- javascript - 需要一种使用数组设置字符串变量的方法
- c# - 关于项目数组的一些说明
- javascript - 迷宫算法:哪一行代码规定了当一个球碰到死角时,它会回溯?
- python - 安装anaconda后django出现问题
- javascript - webpack 5:无效的配置对象。Webpack 已使用与 API 架构不匹配的配置对象进行初始化
- php - 如何在PHP中满足条件后打破while循环?
- html - 将现有值添加到图像 URL
- python - 如何根据所有行值为正、一行值为负、两行值为负...和所有行值为负来排列 Pandas 数据框,