Java Code Examples for javax.servlet.jsp.tagext.JspFragment#invoke()

The following examples show how to use javax.servlet.jsp.tagext.JspFragment#invoke() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ToUpperCaseTag.java    From java-tutorial with Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
@Override
public void doTag() throws JspException, IOException {

	// 将 标签体内容读入该 writer
	StringWriter writer = new StringWriter();

	// 标签体 为 JspFragment 的形式
	JspFragment jspFragment = this.getJspBody();

	// 通过 invoke 输出到指定的 writer 中。
	// 如果参数为 null,将输出到默认的 writer 中,即 getJspContext().getOut() 输出到HTML中
	jspFragment.invoke(writer);

	String content = writer.getBuffer().toString();
	this.getJspContext().getOut().print(content.toUpperCase());
}
 
Example 2
Source File: NuidTag.java    From HongsCORE with MIT License 6 votes vote down vote up
@Override
public void doTag() throws JspException {
  JspWriter out = getJspContext().getOut();

  String uid;
  if (this.sid != null) {
    uid = Core.newIdentity(this.sid);
  } else {
    uid = Core.newIdentity();
  }

  try {
    out.print(uid);

    JspFragment f = getJspBody();
    if (f != null) f.invoke(out);
  } catch (java.io.IOException ex) {
    throw new JspException("Error in NuidTag", ex);
  }
}
 
Example 3
Source File: PageNavigationTag.java    From sinavi-jfw with Apache License 2.0 6 votes vote down vote up
private String invokeFragmentIfNeeded(
        JspFragment fragment,
        String defaultValue,
        int currentIndex) throws JspException {
    PageContext p = pageContext;
    StringWriter w = new StringWriter();
    if (fragment != null) {
        if (currentIndex > 0) p.setAttribute(var, currentIndex);
        try {
            fragment.invoke(w);
        } catch (IOException e) {throw new JspException(e);}
        if (currentIndex > 0) p.removeAttribute(var);
    } else {
        w.append(Strings.substitute(
                defaultValue,
                Maps.hash(var, String.valueOf(currentIndex))));
    }
    return w.toString();
}
 
Example 4
Source File: BlockTagUtils.java    From jsp-template-inheritance with Apache License 2.0 5 votes vote down vote up
public static String getBodyResult(JspFragment jspBody) throws IOException, JspException {
    if (jspBody == null) {
        return "";
    }

    StringWriter writer = new StringWriter();
    jspBody.invoke(writer);
    return writer.toString();
}
 
Example 5
Source File: CallTag.java    From packagedrone with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public void doTag () throws JspException, IOException
{
    final JspFragment body = (JspFragment)getJspContext ().getAttribute ( DefineTag.ATTR_PREFIX + this.name );

    if ( body == null )
    {
        throw new JspException ( String.format ( "Unable to find macro '%s'", this.name ) );
    }

    final JspContext ctx = body.getJspContext ();

    // set attributes to body context

    final Map<String, Object> oldEntries = new HashMap<> ( this.data.size () );

    for ( final Map.Entry<String, Object> entry : this.data.entrySet () )
    {
        oldEntries.put ( entry.getKey (), ctx.getAttribute ( entry.getKey (), PageContext.PAGE_SCOPE ) );
        ctx.setAttribute ( entry.getKey (), entry.getValue (), PageContext.PAGE_SCOPE );
    }

    // invoke

    body.invoke ( getJspContext ().getOut () );

    // set old values, so we don't clutter up the context for the next caller

    for ( final String key : this.data.keySet () )
    {
        final Object val = oldEntries.get ( key );
        if ( val == null )
        {
            ctx.removeAttribute ( key, PageContext.PAGE_SCOPE );
        }
        else
        {
            ctx.setAttribute ( key, val, PageContext.PAGE_SCOPE );
        }
    }
}