- 创建mvc类型的portlet
- 下载jquery3.6.0.js并放入src/main/resources/META-INF/resources/js目录中
- 在init.jsp中添加jquery引用
<% String contextPath = request.getContextPath(); %> <script type="text/javascript" src="<%=contextPath%>/js/jquery3.6.0.js" /> - view.jsp页面中添加页面元素
<button type="button" id="testBtn">测试Ajax</button><br> <div id="div1"></div> - view.jsp中添加后台请求resourceURL标签,可写在div下面,注意这里的var与下面ajax请求的地址对应,id与后台resourceID对应
<portlet:resourceURL var="ajaxUrl" id="addOper" /> - 编写js业务处理
<script type="text/javascript"> $(function (){ $("#testBtn").click(function(){ doAjax() ; }); }) ; function doAjax (){ $.ajax({ url: '${ajaxUrl}', // 注意这里与步骤5中resourceURL的var对应 method: "POST", data:{ <portlet:namespace/>username: "yicj", <portlet:namespace/>password: "123" }, success:function(result){ $("#div1").html(result); } }); } </script> - 编写后台处理逻辑
public class HelloMvcPorletPortlet extends MVCPortlet { // 这里需要按自己需要引入log4j依赖即可 private Logger logger = LoggerFactory.getLogger(HelloMvcPorletPortlet.class); @Override public void serveResource(ResourceRequest resourceRequest, ResourceResponse resourceResponse) throws IOException, PortletException { // 可以根据这个id作为不同请求的判断(增\删\改\查) String resourceID = resourceRequest.getResourceID(); String username = ParamUtil.getString(resourceRequest, "username"); String password = ParamUtil.getString(resourceRequest, "password") ; logger.info("resourceID : {}", resourceID); logger.info("username : {}", username); logger.info("password : {}", password); resourceResponse.setContentType("text/html"); PrintWriter out = resourceResponse.getWriter(); out.write("Resource served successfully!"); out.flush(); out.close(); } } - 测试结果
2021-07-06 07:42:44.271 INFO [http-nio-8080-exec-8][HelloMvcPorletPortlet:45] resourceID : addOper 2021-07-06 07:42:44.271 INFO [http-nio-8080-exec-8][HelloMvcPorletPortlet:46] username : yicj 2021-07-06 07:42:44.272 INFO [http-nio-8080-exec-8][HelloMvcPorletPortlet:47] password : 123
Liferay使用Ajax调用后台学习笔记
最新推荐文章于 2025-12-16 16:02:25 发布
该博客介绍了如何在portlet中创建一个MVC类型的组件,并利用jQuery进行Ajax交互。首先,下载jQuery库并将其置于指定目录下,然后在init.jsp中引入。接着,在view.jsp页面设置按钮和响应区域,并通过portlet:resourceURL标签生成后台请求URL。在JavaScript部分,定义了点击按钮时的Ajax请求,将数据发送到后台。后台接收到请求后,根据resourceID处理数据并返回响应。测试结果显示,Ajax请求成功,日志显示了接收到的参数信息。
1133

被折叠的 条评论
为什么被折叠?



