Как получить доступ к внутреннему html пользовательского тега JSP - PullRequest
0 голосов
/ 19 июня 2020

У меня есть собственный тег JSP, как показано ниже:

<custom:render-element type="APP" />

Я хочу иметь доступ к любому html внутри этого JSP тега в его java классе:

<custom:render-element type="APP">
  <span> <h1> test </h1> </span>
</custom:render-element>

Java класс для тега выглядит следующим образом:

public class RenderElementTag extends AbstractScopedTag {

    private String type;

    public void setType(String elementType) {
        this.type = elementType;
    }

    public void doTag() throws JspException, IOException {
        JspWriter out = getJspContext().getOut();
        String[] appContainers = (String[]) getAttribute("APPS_LIST");
        HashMap<String, String[]> containerElementsMap = new HashMap<>();
        containerElementsMap.put("APPS_LIST", appContainers);

        out.print(renderElements(this.type, containerElementsMap));
        out.flush();
    }

    private String[] getAttribute(String attributeName) {
        JspContext jspContext = getJspContext();
        return (String[]) ((PageContext) jspContext).getRequest().getAttribute(attributeName);
    }

    private String renderElements(String type, HashMap<String, String[]> elementsMap) {
        try {
            StringBuilder builder = new StringBuilder();
            String[] containerIds = elementsMap.get(type);
            for (String containerId : containerIds) {
                if (Strings.isNullOrEmpty(containerId)) {

                    return null;
                }

                builder.append(String.format("%n<section data-type=\"%s\" id=\"%s\"></section>", type, containerId));
            }
            return builder.toString();
        } catch (NullPointerException error) {
            log.error(error.getMessage());
            return "";
        }
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...