如何使用cutom标记预处理数据并将其存储在jsp hiddent input中

如何使用cutom标记预处理数据并将其存储在jsp hiddent input中,jsp,jsp-tags,custom-tags,scriptlet,Jsp,Jsp Tags,Custom Tags,Scriptlet,嗨,我写了一个下面的scriplet,它将从属性文件中获取数据。现在,我想将其作为一个自定义标记,因为jsp中的java代码是不建议的 ApplicationContext appCtx = WebApplicationContextUtils.getWebApplicationContext(config.getServletContext()); ReloadableResourceBundleMessageSource mySpringBean = (ReloadableReso

嗨,我写了一个下面的scriplet,它将从属性文件中获取数据。现在,我想将其作为一个自定义标记,因为jsp中的java代码是不建议的

ApplicationContext appCtx =      WebApplicationContextUtils.getWebApplicationContext(config.getServletContext());
ReloadableResourceBundleMessageSource mySpringBean = (ReloadableResourceBundleMessageSource)    appCtx.getBean("messageSource");
String gridColumnValues=mySpringBean.getMessage("propertyfile", null,  locale)
现在我将这个字符串设置为我的隐藏输入值


如何将其转换为自定义标记首先,您需要创建一个类来完成自定义标记的工作。它应该继承自
javax.servlet.jsp.tagext.SimpleTagSupport
。如果所需的输出是一个隐藏的html输入标记,则可能需要
id
name
的属性。自定义标记的用户应该提供这些字段,以便它们成为具有getter和setter的自定义标记类的字段。用户还将指定从Springbean获取哪个属性,因此该属性也是类中的一个字段

package com.example;

import java.io.IOException;
import java.util.Locale;

import javax.servlet.ServletContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class MyCustomTag extends SimpleTagSupport {

    private String name;
    private String id;
    private String property;

    @Override
    public void doTag() throws JspException, IOException {
        // Overriding doTag() from SimpleTagSupport, you do your Java work.
        try {
            PageContext pc = (PageContext) getJspContext();
            ServletContext sc = pc.getServletContext();
            ApplicationContext appCtx = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);

            ReloadableResourceBundleMessageSource mySpringBean = (ReloadableResourceBundleMessageSource)appCtx.getBean("messageSource");

            // use this.property to allow your tag's user to get any message from the bean.
            String gridColumnValues = mySpringBean.getMessage(this.property, null, Locale.US);

            // write your desired output, in this case a complete hidden html form field
            JspWriter out = pc.getOut();

            out.print("<input type=\"hidden\" ");
            if (this.name != null)
                out.print("name=\"" + this.name + "\" ");
            if (this.id != null)
                out.print("id=\"" + this.id + "\" ");

            out.println("value=\"" + gridColumnValues + "\"/>");

        } catch (Exception e) {
            throw new JspException(e);
        }

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getProperty() {
        return property;
    }

    public void setProperty(String property) {
        this.property = property;
    }

}
要在jsp中使用它,请使用taglib指令声明自定义标记库。前缀将是您希望标记使用的xml命名空间。在这个例子中,我称之为“我的”。tld文件中的标记名为“custom”,因此将使用


当您运行它并查看html源代码时,您将看到一个隐藏的输入标记,其中包含Springbean消息的值

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
                        "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>

    <tlib-version>1.0</tlib-version>
    <jsp-version>2.0</jsp-version>
    <short-name>MyCustomTag</short-name>
    <uri>http://example.com/mytags</uri>

    <tag>
        <name>custom</name>  <!-- The name is how your users will invoke the tag in a jsp.  You can call it anything you want. -->
        <tag-class>com.example.MyCustomTag</tag-class>
        <body-content>empty</body-content>
        <attribute>
            <name>name</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>id</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>property</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>        
    </tag>

</taglib>
<%@ taglib prefix="my" uri="http://example.com/mytags" %>

<my:custom id="myhiddenfield" name="hiddenname" property="propertyfile"/>