javax.servlet.jsp.tagext.JspFragment Java Examples

The following examples show how to use javax.servlet.jsp.tagext.JspFragment. 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: 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 #2
Source File: PankuzuTag.java    From sinavi-jfw with Apache License 2.0 6 votes vote down vote up
/**
 * パンクズリストを表示します。
 * このメソッドはJSP-BODYを評価せずに、
 * 既存のHTMLテンプレート、あるいはクラスコンフィグオーバライドされたHTMLテンプレートを利用します。
 * @throws JspException JSP処理中にエラーが発生した場合
 * @throws IOException 入出力エラーの場合
 */
protected void render() throws JspException, IOException {
    render(new JspFragment() {
        @Override public JspContext getJspContext() {
            return PankuzuTag.this.getJspContext();
        }
        @Override public void invoke(Writer out) throws JspException, IOException {
            JspContext context = getJspContext();
            ViewId vid = (ViewId) context.getAttribute(var);
            String encodedUrl = encodeURL(vid.getUrl());
            String item = Strings.substitute(DEFAULT_TEMPLATE, Maps
                        .hash("url", encodedUrl)
                        .map("label", Strings.escapeHTML(vid.getLabel()))
                        .map("query", trimQueryMarkerIfGetRequestParameterExists(encodedUrl, vid.getQuery())));
            context.getOut().print(item);
        }
    });
}
 
Example #3
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 #4
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 #5
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 #6
Source File: PankuzuTag.java    From sinavi-jfw with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void doTag() throws JspException, IOException {
    super.doTag();
    JspFragment template = getJspBody();
    if (template != null) {
        render(template);
    } else {
        render();
    }
}
 
Example #7
Source File: DefineTag.java    From packagedrone with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public void setJspBody ( final JspFragment jspBody )
{
    super.setJspBody ( jspBody );
    getJspContext ().setAttribute ( ATTR_PREFIX + this.name, jspBody, PageContext.PAGE_SCOPE );
}
 
Example #8
Source File: PageNavigationTagTest.java    From sinavi-jfw with Apache License 2.0 4 votes vote down vote up
@Test
public void jspFragmentが設定される() throws Exception {

    PartialList<String> display = new PartialList<String>();
    for (int i = 0; i < 110; i++) {
        display.add(Integer.toString(i));
    }
    display.setElementCount(110);
    display.setPartCount(11);
    display.setElementCountPerPart(10);
    display.setPartIndex(7);
    PageNavigationTag tag = new PageNavigationTag();
    tag.setPageContext(context);
    tag.setPartial(display);
    tag.setAction("/list");
    JspFragment fragment = new JspFragment() {

        @Override
        public JspContext getJspContext() {
            return context;
        }

        @Override
        public void invoke(Writer out) throws JspException, IOException {
            out.write("----");
        }
    };
    tag.setOmission(fragment);
    tag.doStartTag();
    tag.doEndTag();
    String actual = resp.getContentAsString();
    actual = actual.replaceAll("<span class=\"jfw_paginate_navi_head\">", "");
    actual = actual.replaceAll("<span class=\"jfw_paginate_navi_previous\">", "");
    actual = actual.replaceAll("<span class=\"jfw_paginate_navi_page\">", "");
    actual = actual.replaceAll("<span class=\"jfw_paginate_navi_page_selected\">", "");
    actual = actual.replaceAll("<span class=\"jfw_paginate_navi_next\">", "");
    actual = actual.replaceAll("<span class=\"jfw_paginate_navi_tail\">", "");
    actual = actual.replaceAll("<span class=\"jfw_paginate_navi_omission\">", "");
    actual = actual.replaceAll("</span>", "\n");
    String[] split = actual.split("\n");
    assertThat(split.length, is(15));
    assertThat(Arrays.asList(split), hasItem("<a href=\"/list?pageNumber=1\">&lt;&lt;</a>"));
    assertThat(Arrays.asList(split), hasItem("<a href=\"/list?pageNumber=6\">&lt;</a>"));
    assertThat(Arrays.asList(split), hasItem("<a href=\"/list?pageNumber=2\">2</a>"));
    assertThat(Arrays.asList(split), hasItem("<a href=\"/list?pageNumber=11\">11</a>"));
    assertThat(Arrays.asList(split), hasItem("7"));
    assertThat(Arrays.asList(split), hasItem("----"));
    assertThat(Arrays.asList(split), hasItem("<a href=\"/list?pageNumber=8\">&gt;</a>"));
    assertThat(Arrays.asList(split), hasItem("<a href=\"/list?pageNumber=11\">&gt;&gt;</a>"));
}
 
Example #9
Source File: PageNavigationTag.java    From sinavi-jfw with Apache License 2.0 4 votes vote down vote up
private String invokeFragmentIfNeeded(
        JspFragment fragment,
        String defaultValue) throws JspException {
    return invokeFragmentIfNeeded(fragment, defaultValue, Integer.MIN_VALUE);
}
 
Example #10
Source File: ShuffleSimpleTag.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
public void setFragment1( JspFragment fragment1 ) {
    this.fragment1 = fragment1;
}
 
Example #11
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 );
        }
    }
}
 
Example #12
Source File: ShuffleSimpleTag.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
public void setFragment3( JspFragment fragment3 ) {
    this.fragment3 = fragment3;
}
 
Example #13
Source File: ShuffleSimpleTag.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
public void setFragment2( JspFragment fragment2 ) {
    this.fragment2 = fragment2;
}
 
Example #14
Source File: ShuffleSimpleTag.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
public void setFragment1( JspFragment fragment1 ) {
    this.fragment1 = fragment1;
}
 
Example #15
Source File: ShuffleSimpleTag.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
public void setFragment3( JspFragment fragment3 ) {
    this.fragment3 = fragment3;
}
 
Example #16
Source File: ShuffleSimpleTag.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
public void setFragment2( JspFragment fragment2 ) {
    this.fragment2 = fragment2;
}
 
Example #17
Source File: ShuffleSimpleTag.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
public void setFragment1( JspFragment fragment1 ) {
    this.fragment1 = fragment1;
}
 
Example #18
Source File: ShuffleSimpleTag.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
public void setFragment2( JspFragment fragment2 ) {
    this.fragment2 = fragment2;
}
 
Example #19
Source File: ShuffleSimpleTag.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
public void setFragment3( JspFragment fragment3 ) {
    this.fragment3 = fragment3;
}
 
Example #20
Source File: ShuffleSimpleTag.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
public void setFragment1( JspFragment fragment1 ) {
    this.fragment1 = fragment1;
}
 
Example #21
Source File: ShuffleSimpleTag.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
public void setFragment2( JspFragment fragment2 ) {
    this.fragment2 = fragment2;
}
 
Example #22
Source File: ShuffleSimpleTag.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
public void setFragment3( JspFragment fragment3 ) {
    this.fragment3 = fragment3;
}
 
Example #23
Source File: ShuffleSimpleTag.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
public void setFragment3( JspFragment fragment3 ) {
    this.fragment3 = fragment3;
}
 
Example #24
Source File: ShuffleSimpleTag.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
public void setFragment2( JspFragment fragment2 ) {
    this.fragment2 = fragment2;
}
 
Example #25
Source File: MultiAttributeTag.java    From java-tutorial with Creative Commons Attribution Share Alike 4.0 International 4 votes vote down vote up
public void setBody1(JspFragment body1) {
	this.body1 = body1;
}
 
Example #26
Source File: MultiAttributeTag.java    From java-tutorial with Creative Commons Attribution Share Alike 4.0 International 4 votes vote down vote up
public void setBody2(JspFragment body2) {
	this.body2 = body2;
}
 
Example #27
Source File: ShuffleSimpleTag.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
public void setFragment1( JspFragment fragment1 ) {
    this.fragment1 = fragment1;
}
 
Example #28
Source File: PageNavigationTag.java    From sinavi-jfw with Apache License 2.0 2 votes vote down vote up
/**
 * 前ページリンクのテンプレートを変更するJSPフラグメントを返却します。
 * @return 前ページリンクのテンプレートを変更するJSPフラグメントです。
 */
public JspFragment getPrevious() {
    return previous;
}
 
Example #29
Source File: PageNavigationTag.java    From sinavi-jfw with Apache License 2.0 2 votes vote down vote up
/**
 * 末尾ページリンクのテンプレートを変更するJSPフラグメントを設定します。
 * @param tail 末尾ページリンクのテンプレートを変更するJSPフラグメントです。
 */
public void setTail(JspFragment tail) {
    this.tail = tail;
}
 
Example #30
Source File: PageNavigationTag.java    From sinavi-jfw with Apache License 2.0 2 votes vote down vote up
/**
 * 末尾ページリンクのテンプレートを変更するJSPフラグメントを返却します。
 * @return 末尾ページリンクのテンプレートを変更するJSPフラグメント
 */
public JspFragment getTail() {
    return tail;
}