【问题标题】:JSP custom tags with attributes带有属性的 JSP 自定义标签
【发布时间】:2016-10-22 21:56:21
【问题描述】:

我正在尝试使用自定义标签编写页脚。我是新手,我想让 MyFooterTag 类更具可读性。在 MyFooterTag 类中,我向 JspWriter 写入没有正文和属性的所有内容,但它看起来不太可读……有没有办法使用正文内容或属性来优化它?

<footer class="container-fluid text-center">
  <p><fmt:message key="online_store.copyright.footer"></fmt:message></p>
  <form class="form-inline"><fmt:message key="get_deals.footer"></fmt:message>
    <input type="email" class="form-control" size="50" placeholder="Email Address">
    <button type="button" class="btn btn-danger"><fmt:message key="sign_up.footer"></fmt:message></button>
  </form>
</footer>
private final static String bootstrapFooterClass = "container-fluid text-center";
private final static String bootstrapFormClass = "form-inline";
private final static String bootstrapInputClass = "form-control";
private final static String bootstrapButtonClass = "btn btn-danger";

private final static String footerCopyrightKey = "online_store.copyright.footer";
private final static String getDealsFooterKey = "get_deals.footer";
private final static String singUpFooterKey = "sing_up.footer";


@Override
public int doStartTag() throws JspException {
    String footerStart = "<footer class='" + bootstrapFooterClass + "'>";
    String copyright = "<p><fmt:message key='" + footerCopyrightKey + "'></fmt:message>";
    String form = "<form class='" + bootstrapFormClass + "'><fmt:message key='" + getDealsFooterKey + "'></fmt:message>";
    String input = "<input type='email' class='" + bootstrapInputClass + "' size='50' placeholder='Email Address'>";
    String button = "<button type='button' class='" + bootstrapButtonClass + "'><fmt:message key='" + singUpFooterKey + "'></fmt:message></button>";

    try{
        JspWriter out = pageContext.getOut();
        out.write(footerStart );
        out.newLine();
        out.write(copyright);
        out.newLine();
        out.write(form);
        out.newLine();
        out.write(input);
        out.newLine();
        out.write(button);
    }catch (IOException e){
        throw new JspException(e.toString());
    }
    return SKIP_BODY;
}

@Override
public int doEndTag() throws JspException {
    String endTags = "</form></footer>";
    try {
        pageContext.getOut().write(endTags);
    } catch (IOException e) {
        throw new JspTagException(e.toString());
    }
    return EVAL_PAGE;
}

【问题讨论】:

  • 嘿@Alexander 你有没有检查我的答案!

标签: html jsp custom-tags


【解决方案1】:

是的,您可以使用新版本的 JSTL 优化所有代码,仅使用 SimpleTagSupport 类,该类仅使用 doTag 方法,而不使用所有 dostartTag - doEndTag 或 doAfterBody,而不是每次您想去时都使用 out.newLine()在此示例中返回新行,我将向您展示如何在 println() 方法中执行此操作,该方法生成一个文本并同时返回这里是一个关于您将如何进行的示例:

public class myFooterTag extends SimpleTagSupport {

public void doTag() throws JspException, IOException{
    JspWriter out = getJspContext().getOut(); //create a jspWriter object
    out.println("<footer class='container-fluid text-center'>");
    out.println("<p><fmt:message key='online_store.copyright.footer'></fmt:message></p>");
    out.println("<form class='form-inline'><fmt:message key='get_deals.footer'></fmt:message>");
    out.println("<input type='email' class='form-control' size='50' placeholder='Email Address'>");
    out.println("<button type='button' class='btn btn-danger'><fmt:message key='sign_up.footer'></fmt:message></button>");
    out.println("</form>");
    out.println("</footer>");   
 }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-09
    • 2011-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-18
    • 2012-01-16
    相关资源
    最近更新 更多