[JC]改行コードをbrタグに置換するタグライブラリを作成しました


はい。タイトル通りのタグライブラリを作成してみました。当初普通のJSPのタグライブラリを作成したのですが、下記のように値を渡す予定だったのですが、これじゃ渡せないじゃんということに作成し終わった後に気づいて作成しなおしました。というか、デバッグの始まりでしたw

<jc :txt2br value="%{messageDto.body}"></jc>

今回作成したのは下記の4点です。

  • Text2BrTag.java:タグライブラリクラス
  • Text2Br.java:タグライブラリコンポーネント
  • HtmlUtils.java:HTML出力用ユーティリティ
  • jcommunity.tld:カスタムタグ定義ファイル

また今回も醜いかとは思いますが、何かの役に立てばと思い、ソースを貼り付けておきます。

広告

■Text2BrTag.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package org.fukata.jc_common.view.taglib;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.apache.struts2.components.Component;
import org.apache.struts2.views.jsp.ComponentTagSupport;
import org.fukata.jc_common.view.component.Text2Br;
 
import com.opensymphony.xwork2.util.ValueStack;
 
/**
 * テキストの改行コードを<br />に置換する
 * 
 * @author tatsuya
 *
 */
public class Text2BrTag extends ComponentTagSupport {
 
	private static final long serialVersionUID = 1L;
 
	// 置換する文字列
	private String value;
 
	public String getValue() {
		return value;
	}
 
	public void setValue(String value) {
		this.value = value;
	}
 
	@Override
	public Component getBean(ValueStack stack, HttpServletRequest req,
			HttpServletResponse res) {
		return new Text2Br(stack);
	}
 
	@Override
	protected void populateParams() {
		super.populateParams();
 
		Text2Br text2Br = (Text2Br) component;
		text2Br.setValue(this.value);
	}
 
 
}

■Text2Br.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package org.fukata.jc_common.view.component;
 
import java.io.IOException;
import java.io.Writer;
 
import org.apache.struts2.components.Component;
import org.fukata.jc_common.utils.HtmlUtils;
 
import com.opensymphony.xwork2.util.ValueStack;
 
public class Text2Br extends Component {
 
	private String value;
 
	public Text2Br(ValueStack stack) {
		super(stack);
	}
 
	@Override
	public boolean start(Writer writer) {
		boolean result = super.start(writer);
 
		if (this.value!=null) {
			this.value = findString(this.value);
			// 改行コードをbrタグに置換
			this.value = HtmlUtils.getTxt2Br(this.value);
		}
 
		return result;
	}
 
	@Override
	protected boolean end(Writer writer, String body, boolean popComponentStack) {
		boolean result = super.end(writer, body, popComponentStack);
 
		try {
			// 画面に書き出す
			writer.write(this.value);
		} catch (IOException e) {
			e.printStackTrace();
		}
 
		return result;
	}
 
	public String getValue() {
		return value;
	}
 
	public void setValue(String value) {
		this.value = value;
	}
 
 
}

■HtmlUtils.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package org.fukata.jc_common.utils;
 
public class HtmlUtils {
 
	private static final String BKREAK_REGEX = "(\r\n|\r|\n)"; 
	private static final String HTML_BREAK = "<br />"; 
 
	private HtmlUtils() {
	}
 
	public static String getTxt2Br(String txt) {
		return txt.replaceAll(BKREAK_REGEX, HTML_BREAK);
	}
}

■jcommunity.tld

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
< ?xml version="1.0" ?>
< !DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" "https://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
 
<taglib xmlns="https://java.sun.com/xml/ns/j2ee"
        xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="https://java.sun.com/xml/ns/j2eeweb-jsptaglibrary_2_0.xsd"
        version="2.0">
  <tlibversion>1.0</tlibversion>
  <jspversion>2.0</jspversion>
  <shortname>jcommunity</shortname>
  <info>JCommunity tab library</info>
 
  <tag>
    <name>txt2br</name>
    <tagclass>org.fukata.jc_common.view.taglib.Text2BrTag</tagclass>
    <bodycontent>empty</bodycontent>
    <info>text to html br tag</info>
    <attribute>
    	<name>value</name>
    	<required>true</required>
    	<rtexprvalue>true</rtexprvalue>
    	<description>< ![CDATA[Value expression for Parameter to set]]></description>
    </attribute>
  </tag>
</taglib>

関連記事