JSP 具有属性的自定义标签
JSP custom tags with attributes
我正在尝试使用自定义标签编写页脚。我是新手,我想让 MyFooterTag class 更具可读性。在 MyFooterTag class 我向 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;
}
是的,您可以仅使用 SimpleTagSupport Class 使用新版本的 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>");
}
}
我正在尝试使用自定义标签编写页脚。我是新手,我想让 MyFooterTag class 更具可读性。在 MyFooterTag class 我向 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;
}
是的,您可以仅使用 SimpleTagSupport Class 使用新版本的 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>");
}
}