2016-06-21 51 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>"); 
} 
} 
相關問題