是否有可能在自定义标签内包装弹簧形式标签? (见更新1)

时间:2014-08-18 23:31:07

标签: java jsp jstl taglib

在我目前的春季项目中,我创建了跟随自定义标签来定义表单:

public class MainFormTag extends TagSupport {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private String action;

    private String classe;

    public void doTag() throws IOException {
        JspWriter out = pageContext.getOut();
        out.println("<form method='post' action='"+action+"'></form>");
    }

    @Override
    public int doStartTag() throws JspException {
        return EVAL_BODY_INCLUDE;
    }

    @Override
    public int doEndTag() {
        return EVAL_PAGE;
    }
}

此标记以这种方式使用:

<c:url value="/${entity}/cadastra" var="cadastra"/>
<custom:MainForm action="${cadastra}" classe="${command['class'].simpleName}">
    <button type="submit" class="btn btn-lg btn-primary">cadastrar</button>
</custom:MainForm>

但是当我部署应用程序并打开此表单的页面时,页面中不显示任何内容。我在这里做错了什么?

我的customTag.tld是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.0" xmlns="http://java.sun.com/xml/ns/j2ee" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd">
  <tlib-version>1.0</tlib-version>
    <short-name>custom</short-name>
    <uri>/WEB-INF/customTag</uri>
    <tag>
        <name>MainForm</name>
        <tag-class>com.spring.loja.config.taglib.form.MainFormTag</tag-class>
        <attribute>
            <name>action</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>classe</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
    <tag>
        <name>Input</name>
        <tag-class>com.spring.loja.config.taglib.form_control.InputTag</tag-class>
        <attribute>
            <name>ordem</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
    <tag>
        <name>Select</name>
        <tag-class>com.spring.loja.config.taglib.form_control.SelectTag</tag-class>
        <attribute>
            <name>ordem</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
    <tag>
        <name>EmbedSelect</name>
        <tag-class>com.spring.loja.config.taglib.form_control.EmbedSelectTag</tag-class>
        <attribute>
            <name>ordem</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
    <tag>
        <name>Textarea</name>
        <tag-class>com.spring.loja.config.taglib.form_control.TextareaTag</tag-class>
        <attribute>
            <name>ordem</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
    <tag>
        <name>Checkbox</name>
        <tag-class>com.spring.loja.config.taglib.form_control.CheckboxTag</tag-class>
        <attribute>
            <name>ordem</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>label</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
    <tag>
        <name>Radiobutton</name>
        <tag-class>com.spring.loja.config.taglib.form_control.RadiobuttonTag</tag-class>
        <attribute>
            <name>ordem</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>label</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>value</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
</taglib>

更新1

我解决了上面描述的问题,用doStartTag和doEndTag对替换方法doTag。但现在我有另一个问题:是否可以在自定义标记类中使用spring form标记?通过这种方式,应用程序必须转换spring form标签中的自定义标签,并将最后一个标签转换为最终的form标签,并将其传送到浏览器。

这些方法,如果这种方法可行的话,那就是:

public int doStartTag() throws JspException {
    try {
        JspWriter out = pageContext.getOut();
        out.println("<form:form method='post' action='"+action+"'>");
    } catch (IOException e) {
        throw new JspException("Error: " + e.getMessage());
    }
    return EVAL_BODY_INCLUDE;
}

public int doEndTag() throws JspException {
    try {
        JspWriter out = pageContext.getOut();
        out.println("</form:form>");
    } catch (IOException e) {
        throw new JspException("Error: " + e.getMessage());
    }
    return EVAL_PAGE;
}

0 个答案:

没有答案