2
3

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 5 years have passed since last update.

Struts2で自作の拡張タグを作る

Last updated at Posted at 2016-01-28

HTMLやJSPのようなタグを独自に作る事ができる。
環境はStruts2で試しています。

現在時刻を表示するサンプル

JSP

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEnco	ding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib prefix="t" uri="/WEB-INF/mytaglib.tld" %>

<html>
<body>
<t:test/>
</body>
</html>

TLD

<?xml version="1.0" ?>

<taglib>
    <tlib-version>1.1</tlib-version>
    <jsp-version>2.1</jsp-version>
    <short-name>HelloTag</short-name>
    <tag>
        <name>test</name>
        <tag-class>mytaglib.HelloTag</tag-class>
      <body-content>EMPTY</body-content>
    </tag>
</taglib>

java

package mytaglib;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class HelloTag extends SimpleTagSupport{

    public void doTag() throws JspException, IOException {
        String string = new SimpleDateFormat("yyyy/MM/dd").format(new Date());
        getJspContext().getOut().print(string);
    }
}
2
3
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
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?