Struts2 - 如何为六个几乎相同的JSP编写模板?

时间:2014-01-14 15:52:48

标签: java-ee struts2 enums

我有六个几乎相同的JSP,它们之间的区别在于它们中的每一个都会迭代一个被分割成第一个属性的枚举。

枚举:

public enum Segmento {

    INTERNACAO_DOMICILIAR(Vertical.1, "/relatorios/saude/tiss/internacao/domiciliar", "Internação Domiciliar", Projeto.TISS, Arrays.asList(LinksRelatorios.CAIXA_VISITA, LinksRelatorios.CAIXA_VISITA_EMPRESA, LinksRelatorios.PRODUTIVIDADE_INTERNACAO_DOMICILIAR, LinksRelatorios.PENDENCIAS, LinksRelatorios.SOLICITACAO_INICIAL, LinksRelatorios.SOLICITACAO_PRORROGACAO, LinksRelatorios.TEMPO_RESPOSTA)),
    INTERNACAO_CONCIERGE(Vertical.2, "/relatorios/saude/tiss/internacao/concierge", "Internação Concierge", Projeto.TISS, Arrays.asList(LinksRelatorios.INTERNACOES_CONCIERGE)),
    OPERADOR_WEB(Vertical.3, "/relatorios/saude/tiss/operadorweb", "Operador Web", Projeto.TISS, Arrays.asList(LinksRelatorios.ATENDIMENTO_ANALITICO, LinksRelatorios.ATENDIMENTO_SINTETICO, LinksRelatorios.TEMPO_ANALISE_ANALITICO, LinksRelatorios.TEMPO_ANALISE_SINTETICO, LinksRelatorios.SLA));

    private Vertical vertical;
    private String namespace;
    private String descricao;
    private Projeto projeto;
    private List<LinksRelatorios> links;

    private Segmento(final Vertical verticalParam, final String namespaceParam, final String descricaoParam,
            final Projeto projetoParam, final List<LinksRelatorios> linksParam) {
        this.vertical = verticalParam;
        this.namespace = namespaceParam;
        this.descricao = descricaoParam;
        this.projeto = projetoParam;
        this.links = linksParam;
    }

    public Vertical getVertical() {
        return this.vertical;
    }

    public String getNamespace() {
        return this.namespace;
    }

    public String getDescricao() {
        return this.descricao;
    }

    public Projeto getProjeto() {
        return this.projeto;
    }

    public List<LinksRelatorios> getLinks() {
        return this.links;
    }
}

六个JSP中的一个:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" %>
<%@ taglib prefix="s" uri="/struts-tags"%>
<head>
    <script type="text/javascript" src="<s:url value="/includes/js/botoes/links.js" />"></script>
</head>
<div class="menuBotoes">
    <s:iterator value="segmentosVerticalSaude" var="segmento">
        <table cellpadding="0" cellspacing="0" class="tableBotaoMenu">
            <tr class="trCimaBotaoMenuSelect">
                <td align="center" class="imagemBotaoMenuSelect"><img src="<s:url value="/includes/imagens/global/botoes/grafico.png" />" /></td>
                <td align="left" class="descricaoBotaoMenuSelect"><s:property value="descricao" escapeHtml="false" /></td>
            </tr>
            <tr class="trBaixoBotaoMenuSelect">
                <td align="center" class="imagemBotaoMenuSelect"><s:property value="projeto.sigla" escapeHtml="false" /></td>
                <td align="left" class="descricaoBotaoMenuSelect">
                    <select>
                        <option value="">-- Selecione um relat&oacute;rio --</option>
                        <s:iterator value="links" var="link">
                            <option value="<s:url namespace="%{#segmento.namespace}" action="%{#link.action}" />"><s:property value="textoLink" escapeHtml="false" /></option>
                        </s:iterator>
                    </select>
                </td>
            </tr>
        </table>
    </s:iterator>
</div>

这些JSP之间的区别在于第七行。我遍历上面描述的枚举,但是每个JSP迭代在我的动作中创建的列表,该列表按照其第一个参数拆分枚举,换句话说,第一个JSP只会带来枚举条目它的第一个属性等于1,第二个JSP只会带来枚举条目,它的第一个属性等于1,并继续。

有什么建议吗?

编辑:拆分枚举的操作:

public static List<Segmento> getSegmentosVerticalSaude() {
    return BaseAction.getSegmentosPorVertical(Vertical.SAUDE);
}

public static List<Segmento> getSegmentosVerticalAuto() {
    return BaseAction.getSegmentosPorVertical(Vertical.AUTOMOVEIS);
}

public static List<Segmento> getSegmentosVerticalCapitalizacao() {
    return BaseAction.getSegmentosPorVertical(Vertical.CAPITALIZACAO);
}

public static List<Segmento> getSegmentosVerticalDental() {
    return BaseAction.getSegmentosPorVertical(Vertical.DENTAL);
}

public static List<Segmento> getSegmentosVerticalPrevidencia() {
    return BaseAction.getSegmentosPorVertical(Vertical.PREVIDENCIA);
}

public static List<Segmento> getSegmentosVerticalResidencial() {
    return BaseAction.getSegmentosPorVertical(Vertical.RESIDENCIAL);
}

private static List<Segmento> getSegmentosPorVertical(final Vertical vertical) {
    final List<Segmento> segmentosSaude = new ArrayList<Segmento>();
    for (final Segmento segmento : Segmento.values()) {
        if (vertical == segmento.getVertical()) {
            segmentosSaude.add(segmento);
        }
    }

    return segmentosSaude;
}

将在六个JSP中的每一个中调用这些方法中的每一个。我想在运行时知道应该调用哪一个。

1 个答案:

答案 0 :(得分:0)

根据@DaveNewton的建议,我将所有getSegmentosPorVertical Anything 包装到一个方法中,该方法验证URL中传递的参数并找出应该使用的枚举条目。

这些方法现在以这种方式编码:

public static List<Segmento> getSegmentos() {
    return BaseAction.getSegmentosPorVertical(Vertical.findVerticalByIdentificador(BaseAction
            .getParameter("vertical")));
}

private static List<Segmento> getSegmentosPorVertical(final Vertical vertical) {
    final List<Segmento> segmentos = new ArrayList<Segmento>();
    for (final Segmento segmento : Segmento.values()) {
        if (vertical == segmento.getVertical()) {
            segmentos.add(segmento);
        }
    }

    return segmentos;
}

原始JSP是这样的:(这里唯一改变的是第一个struts taglib迭代器中的方法调用)

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" %>
<%@ taglib prefix="s" uri="/struts-tags"%>
<head>
    <script type="text/javascript" src="<s:url value="/includes/js/botoes/links.js" />"></script>
</head>
<div class="menuBotoes">
    <div align="right">
        <s:a namespace="/filtros/verticais" action="iniciarFiltroVerticais">
            <img src="<s:url value="/includes/imagens/global/botao-voltar.png" />" width="59" height="26" alt="Voltar para a tela de sele&ccedil;&atilde;o de relat&oacute;rios por verticais." />
        </s:a>
    </div>
    <s:iterator value="segmentos" var="segmento">
        <table cellpadding="0" cellspacing="0" class="tableBotaoMenu">
            <tr class="trCimaBotaoMenuSelect">
                <td align="center" class="imagemBotaoMenuSelect"><img src="<s:url value="/includes/imagens/global/botoes/grafico.png" />" /></td>
                <td align="left" class="descricaoBotaoMenuSelect"><s:property value="descricao" escapeHtml="false" /></td>
            </tr>
            <tr class="trBaixoBotaoMenuSelect">
                <td align="center" class="imagemBotaoMenuSelect"><s:property value="projeto.sigla" escapeHtml="false" /></td>
                <td align="left" class="descricaoBotaoMenuSelect">
                    <select>
                        <option value="">-- Selecione um relat&oacute;rio --</option>
                        <s:iterator value="links" var="link">
                            <option value="<s:url namespace="%{#segmento.namespace}" action="%{#link.action}" />"><s:property value="textoLink" escapeHtml="false" /></option>
                        </s:iterator>
                    </select>
                </td>
            </tr>
        </table>
    </s:iterator>
    <div align="right" style="clear: both;">
        <s:a namespace="/filtros/verticais" action="iniciarFiltroVerticais">
            <img src="<s:url value="/includes/imagens/global/botao-voltar.png" />" width="59" height="26" alt="Voltar para a tela de sele&ccedil;&atilde;o de relat&oacute;rios por verticais." />
        </s:a>
    </div>
</div>

创建参数的JSP:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" %>
<%@ taglib prefix="s" uri="/struts-tags"%>
<head></head>
<div class="menuBotoes">
    <s:iterator value="verticais" var="vertical">
        <s:a namespace="/filtros" action="iniciarFiltroSegmentos?vertical=%{#vertical.identificador}" cssClass="%{#vertical.classeCSSLink}">
            <span class="<s:property value="classeCSSSpan" />"><s:property value="textoLink" escapeHtml="false" /></span>
        </s:a>
    </s:iterator>
</div>

最后但并非最不重要的是,包含参数值的枚举:

public enum Vertical {

    SAUDE(1, "botoesMenu", "verticalSaude", "Sa&uacute;de"),
    AUTOMOVEIS(2, "botoesMenu disabled", "verticalAutomoveis", "Autom&oacute;veis"),
    CAPITALIZACAO(3, "botoesMenu disabled", "verticalCapitalizacao", "Capitaliza&ccedil;&atilde;o"),
    DENTAL(4, "botoesMenu disabled", "verticalDental", "Dental"),
    RESIDENCIAL(5, "botoesMenu disabled", "verticalResidencial", "Residencial"),
    PREVIDENCIA(6, "botoesMenu disabled", "verticalPrevidencia", "Previd&ecirc;ncia");

    private int identificador;
    private String classeCSSLink;
    private String classeCSSSpan;
    private String textoLink;

    private Vertical(final int identificadorParam, final String classeCSSLinkParam, final String classeCSSSpanParam,
            final String textoLinkParam) {
        this.identificador = identificadorParam;
        this.classeCSSLink = classeCSSLinkParam;
        this.classeCSSSpan = classeCSSSpanParam;
        this.textoLink = textoLinkParam;
    }

    public int getIdentificador() {
        return this.identificador;
    }

    public String getClasseCSSLink() {
        return this.classeCSSLink;
    }

    public String getClasseCSSSpan() {
        return this.classeCSSSpan;
    }

    public String getTextoLink() {
        return this.textoLink;
    }

    public static Vertical findVerticalByIdentificador(final int identificadorParam) {
        for (final Vertical vertical : Vertical.values()) {
            if (vertical.getIdentificador() == identificadorParam) {
                return vertical;
            }
        }

        return null;
    }

    public static Vertical findVerticalByIdentificador(final String identificadorParam) {
        if (StringUtils.isBlank(identificadorParam)) {
            return null;
        }

        return Vertical.findVerticalByIdentificador(Integer.parseInt(identificadorParam));
    }
}