java - Struts2 - 我如何使用 <s :a> without previously knowing the action that I'll use?

标签 java jsp struts2

我必须在 JSP 中显示多个链接,并且每个链接的 HTML 结构都是相同的,因此我使用了 struts2 taglib 迭代器来构建它。问题是我不知道如何建立链接本身:

我的 JSP

<%@ taglib prefix="s" uri="/struts-tags"%>
<head></head>
<div class="menuBotoes">
    <s:iterator value="links" var="link">
        <s:a namespace="link.nameSpace" action="link.action">
            <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="textoLink" /></td>
                </tr>
                <tr class="trBaixoBotaoMenuSelect">
                    <td align="center" class="imagemBotaoMenuSelect" colspan="2"><s:property value="projeto" /></td>
                </tr>
            </table>
        </s:a>
    </s:iterator>
</div>

在 JSP 中迭代的对象是这样的:

public enum LinksRelatorios {

    1("Caixa Visita", "/relatorios/", "iniciarRelatorioCaixaVisita", "TISS"),
    2("Caixa Visita Empresa", "/relatorios/", "iniciarRelatorioCaixaVisitaEmpresa", "TISS"),
    3("Produtividade Internação Domiciliar", "/relatorios/", "iniciarRelatorioInternacaoDomiciliar", "TISS"),
    4("Pendências", "/relatorios/", "iniciarRelatorioPendencias", "TISS"),
    5("Solicitação Inicial", "/relatorios/", "iniciarRelatorioSolicitacaoInicial", "TISS"),
    6("Solicitação Prorrogação", "/relatorios/", "iniciarRelatorioSolicitacaoProrrogacao", "TISS"),
    7("Tempo Resposta", "/relatorios/", "iniciarRelatorioTempoResposta", "TISS");

    private String textoLink;
    private String nameSpace;
    private String action;
    private String projeto;

    private LinksRelatorios(final String textoLinkParam, final String nameSpaceParam, final String actionParam,
            final String projetoParam) {
        this.textoLink = textoLinkParam;
        this.nameSpace = nameSpaceParam;
        this.action = actionParam;
        this.projeto = projetoParam;
    }

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

    public String getNameSpace() {
        return this.nameSpace;
    }

    public String getAction() {
        return this.action;
    }

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

我的操作

@Controller
@Scope("request")
public class InicioAction extends BaseAction {

    private static final long serialVersionUID = -1161409943678292285L;

    private static final LinksRelatorios[] links = LinksRelatorios.values();

    public String inicio() {
        this.addActionMessage(this.getText("msg.sucesso.saudacao.mensagem", new String[] { (String) BaseAction
                .getSession().getAttribute(Constantes.PERFIL) }));
        return Action.SUCCESS;
    }

    public String iniciarRelatoriosPorProjeto() {
        return Action.SUCCESS;
    }

    public String iniciarRelatoriosFiltro() {
        return Action.SUCCESS;
    }

    public static LinksRelatorios[] getLinks() {
        return InicioAction.links;
    }
}

我试过使用 <s:a namespace="link.nameSpace" action="link.action"> , < s:a namespace="%{link.nameSpace}" action="%{link.action}"> , <s:a namespace="#link.nameSpace" action="#link.action"> , 但似乎不起作用。

在有人问之前,枚举工作正常,在我的 JSP 中我有 <s:property value="projeto" /><s:property value="textoLink" />这些是来自枚举的属性。

我已阅读 http://struts.apache.org/release/2.3.x/docs/using-struts-2-tags.html 中的在线官方文档, http://struts.apache.org/release/2.3.x/docs/a.htmlhttp://struts.apache.org/release/2.3.x/docs/url.html但与 PrimeFaces 或 RichFaces 等已经构建了自己的展示柜的框架相比,示例部分实际上非常差。

最佳答案

使用下面的代码

<s:a namespace="%{#link.nameSpace}" action="%{#link.action}">

使用 %{} 使 struts2 计算 {} 内表达式的内容,然后将结果分配给属性。

引用http://struts.apache.org/release/2.0.x/docs/tag-syntax.html了解更多详情。

关于java - Struts2 - 我如何使用 <s :a> without previously knowing the action that I'll use?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20884989/

相关文章:

java - 后退按钮后,portlet 不会进入 processaction

java - Struts 2 JSON JQuery Grid 不点击 Action

在 IE10 中重新加载另一个框架时,Java Applet 失败/消失

java - 当 jsp 到 servlet 调用时,上下文路径未在 url 中设置

java - Spring Boot JPA Starter 的 Spring Boot 启动问题

java - 每次我在 Eclipse 上重新启动服务器时,Eclipse Tomcat 服务器配置都会重置

javascript - Struts 2 标签的扩展扰乱了页面的呈现

java - 从另一个应用程序接收参数

java - ProgressMonitor 和 SwingWorker 不工作

java - 如何从 RepositoryItemWriter 调用 SimpleJpaWriter 方法?