在AEM 6.4中实现动态下拉菜单的方法如下:
创建一个组件,用于显示下拉菜单。组件的路径可以是/apps/myproject/components/dropdownmenu。
在组件的客户端库中添加必要的CSS和JavaScript文件。在/apps/myproject/clientlibs/myclientlib/js文件夹中创建一个名为dropdown.js的JavaScript文件,并将其添加到组件的客户端库中。
在组件的HTML文件中添加一个选择元素,用于显示下拉菜单选项。在组件的HTML文件(/apps/myproject/components/dropdownmenu/dropdownmenu.html)中添加以下代码:
(function($, $document) {
"use strict";
$document.on("dialog-ready", function() {
var dropdown = $("#dropdown");
// 发送Ajax请求获取动态数据
$.ajax({
url: "/bin/getoptions", // 替换为实际的数据源URL
method: "GET",
dataType: "json",
success: function(data) {
// 将数据填充到下拉菜单中
data.forEach(function(option) {
dropdown.append($("
package com.myproject.servlets;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import org.apache.commons.lang3.StringUtils;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@SlingServlet(paths = "/bin/getoptions", methods = "GET", metatype = true)
@Properties({ @Property(name = "service.description", value = "Get Options Servlet"),
@Property(name = "service.vendor", value = "My Project") })
public class GetOptionsServlet extends SlingSafeMethodsServlet implements Servlet {
private static final long serialVersionUID = 1L;
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
throws ServletException, IOException {
try {
List
请注意