htmljspcustom-tags

JSP custom tags with attributes


I am trying to write my footer using custom tags. I am new to this, and i want to make MyFooterTag class more readable. At MyFooterTag class i write to JspWriter everything without body and attributes but it looks not pretty readable... Is there a way to optimize it using body content or attributes?

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

Solution

  • yes you can optimise all that code with the new version of JSTL using just the SimpleTagSupport Class which use only the doTag Method without using all the dostartTag - doEndTag or doAfterBody and instead of using the out.newLine() every time you want to go back to a new line in this example i'm showing you how to do it in the println() method which made out a text and go back in the same time here is an example on how you will proceed :

    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>");   
     }
    }