我正在尝试使用自定义标记来编写我的页脚。我是新手,我想让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>
&#13;
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 :(得分:0)
是的,您可以使用新版本的JSTL优化所有代码,只使用SimpleTagSupport类,它只使用doTag方法而不使用所有dostartTag - doEndTag或doAfterBody,而不是每次都使用out.newLine()
回到这个例子中的一个新行,我告诉你如何在println()
方法中创建文本并在同一时间返回这里是一个关于如何继续的例子: / p>
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>");
}
}