1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【spring, jsp】カスタムタグでDBからクエリ結果を取得する

Last updated at Posted at 2020-12-04

カスタムタグでDBからクエリ結果を取得する

・タグ内でサービスをDI
・通常ならコントローラでサービスを呼び出して結果を設定するが、一部の分割された jsp でのみ使うといった場合に有効
・コントローラの肥大化を抑制できる
・パラメータを受け取ってSQLを発行し結果を変数に返す

環境

・Windows10 64bit
・SpringFramework 4
・Java 8

カスタムタグの書式

jsp での記載方法
入力を増やしたり、出力を増やしたりももちろん可能

<targetTable:getByInputParam inputParam="${inputParam}" resultVar="resultTargetTable"/>

・inputParam: 入力パラメータ
・resultVar: 結果が格納される変数名

カスタムタグ定義 (taglib)

<?xml version="1.0" encoding="UTF-8" ?>
<taglib 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 http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
    version="2.0">
    <description>targetTable query functions library</description>
    <display-name>targetTable query functions</display-name>
    <tlib-version>1.0</tlib-version>
    <short-name>query_targetTable</short-name>
    <uri>http://your.own.domain/tags/query_targetTable</uri>

    <tag>
        <name>getByInputParam</name>
        <tag-class>your.own.domain.tag.query_targetTable.GetByInputParamTag</tag-class>
        <body-content>empty</body-content>
        <attribute>
            <name>inputParam</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>resultVar</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
    </tag>
</taglib>

カスタムタグのクラス

public class GetByInputParamTag extends RequestContextAwareTag {

	private static final long serialVersionUID = -1L;
	private static final String DEFAULT_RESULTVAR_NAME = "TargetTable_GetByInputParam";

	private String inputParam;
	private String resultVar;

	@Autowired
	private TargetTableService targetTableService;
	@Override
	protected int doStartTagInternal() throws Exception {
		try {
			Integer inputParamInt = Integer.valueOf(inputParam);
			// DI
			if (targetTableService == null) {
				WebApplicationContext webApplicationContext = getRequestContext().getWebApplicationContext();
				AutowireCapableBeanFactory autowireCapableBeanFactory = webApplicationContext.getAutowireCapableBeanFactory();
				autowireCapableBeanFactory.autowireBean(this);
			}
			// クエリ実行
			TargetTable tt = targetTableService.getByinputParam(inputParamInt);
			if (StringUtils.isEmpty(resultVar)) {
				resultVar = DEFAULT_RESULTVAR_NAME;
			}
			pageContext.setAttribute(resultVar, tt);
		}
		catch (Exception e) {
			StringWriter sw = new StringWriter();
			e.printStackTrace(new PrintWriter(sw));
			pageContext.getServletContext().log(sw.toString());
		}
		return SKIP_BODY;
	}

	public String getInputParam() {
		return inputParam;
	}

	public void setInputParam(String inputParam) {
		this.inputParam = inputParam;
	}

	public TargetTableService getTargetTableService() {
		return targetTableService;
	}

	public void setTargetTableService(TargetTableService targetTableService) {
		this.targetTableService = targetTableService;
	}

	public String getResultVar() {
		return resultVar;
	}

	public void setResultVar(String resultVar) {
		this.resultVar = resultVar;
	}
}

jsp での カスタムタグ利用設定

<%@ taglib prefix="targetTable" uri="http://your.own.domain/tags/query_targetTable" %>

以上、お疲れさまでした!

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?