2016-06-21 65 views
0

我正在尝试使用自定义标记来编写我的页脚。我对此很陌生,我想让MyFooterTag类更具可读性。在MyFooterTag类中,我向JspWriter写入没有主体和属性的所有东西,但它看起来不太可读...有没有一种方法可以使用主体内容或属性来优化它?具有属性的JSP自定义标记

<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; 
} 
+0

嘿@亚历山大你检查我的答案或不! – PacMan

回答

0

是的,你可以优化所有的代码,只要使用的SimpleTagSupport类JSTL的新版本,它仅使用的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>"); 
} 
}