Struts 2拡張ラベルのページングラベルPageTag

11649 ワード

改ページラベルを拡張
(1)
拡張:org.apache.struts2.views.jsp.ComponentTagSupport
拡張クラスExtTag


public class ExtTag extends ComponentTagSupport{
	/**
	 *  
	 */
	protected String theme;
	/**
	 *  id
	 */
	protected String id;
	
	public String getTheme() {
		return theme;
	}
	public void setTheme(String theme) {
		this.theme = theme;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	 
	@Override
	public Component getBean(ValueStack arg0, HttpServletRequest arg1,
			HttpServletResponse arg2) {
		return null;
	}
}

ページングラベル


public class PageTag extends ExtTag{
	
	/**
	 *   
	 */
	private String pagesize;
	/**
	 *  
	 */
	private String total;
	/**
	 *  
	 */
	private String currentpage;
	/**
	 * url
	 */
	private String url;
	/**
	 *url  
	 */
	private String urlType;

	/**
	 *  
	 */
	private String unit;
	 
	@Override
	public Component getBean(ValueStack arg0, HttpServletRequest arg1,
			HttpServletResponse arg2) {
		super.populateParams();  
          
		return  new Page(arg0);
	}
	/**
	 *  
	 */
    protected void populateParams() {  
        super.populateParams();  
        Page page = (Page)component;  
        page.setCurrentpage(currentpage);
        page.setId(id);
        page.setPagesize(pagesize);
        page.setTheme(theme);
        page.setTotal(total);
        page.setUrl(url);
        page.setUrlType(urlType);
        page.setUnit(unit);
    } 
	public String getPagesize() {
		return pagesize;
	}
	public void setPagesize(String pagesize) {
		this.pagesize = pagesize;
	}
	public String getTotal() {
		return total;
	}
	public void setTotal(String total) {
		this.total = total;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public String getUrlType() {
		return urlType;
	}
	public void setUrlType(String urlType) {
		this.urlType = urlType;
	}

	public String getCurrentpage() {
		return currentpage;
	}
	public void setCurrentpage(String currentpage) {
		this.currentpage = currentpage;
	}
	public String getUnit() {
		return unit;
	}
	public void setUnit(String unit) {
		this.unit = unit;
	}
}


(2)
拡張コントロール:org.apache.struts2.components.Component

public class ExtComponent extends Component{
	
	public ExtComponent(ValueStack arg0) {
		super(arg0);
	}

	/**
	 *  
	 */
	protected String theme;
	/**
	 *  id
	 */
	protected String id;
	public String getTheme() {
		return theme;
	}
	public void setTheme(String theme) {
		this.theme = theme;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
}

ページングコントロール

public class Page extends ExtComponent{
	/**
	 *   
	 */
	private String pagesize;
	/**
	 *  
	 */
	private String total;
	/**
	 *  
	 */
	private String currentpage;
	/**
	 * url
	 */
	private String url;
	/**
	 *url  
	 */
	private String urlType;
	/**
	 * @param stack
	 */
	/**
	 *  
	 */
	private String unit;	
	
	public Page(ValueStack arg0) {
		super(arg0);
	}
	/**
	 *  
	 */
	public boolean start(Writer writer) {
		try {
			StringBuffer html = new StringBuffer();
			int _pagesize = 15;
			if (!CheckUtil.isNullOrEmpty(pagesize)) {
			   _pagesize = StringUtil.parsetoInt(pagesize, 15);
			}
			int _total = StringUtil.parsetoInt(total, 0);
			int _currentpage = StringUtil.parsetoInt(currentpage, 1);
			if (_currentpage < 1) {
				_currentpage = 1;
			}
			int _totalPage = (int)Math.ceil(_total * 1.0f/_pagesize);
		    if (CheckUtil.isNullOrEmpty(unit)) {
		    	unit = " ";
		    }
		    
			html.append("<div class=\"fer_pager");
			if (!CheckUtil.isNullOrEmpty(theme)) {
				html.append(" " + theme);
			}
			html.append("\" ");
		    if (!CheckUtil.isNullOrEmpty(id)) {
		    	html.append(" id=\"" + id + "\"");
		    }

		    html.append(">");
		    html.append("<ul class=\"kptturnpage pagerpanel\">");
			html.append("<li class=\"ntotal\"> <i class=\"currentpage\">" + _currentpage + "</i> / <i>" + _totalPage + "</i>    <i>" + _pagesize + "</i>" + unit + "/ <i class=\"totalNum\">" + _total +"</i>" + unit + "</li>");
			if (_totalPage > 0) {
				if (_currentpage <= 1){
					html.append("<li class=\"nfirst\"><a title=\" \"></a></li>");
					html.append("<li class=\"nprevior\"><a title=\" \"></a></li>");
			    }
				else {
					html.append("<li class=\"first\"><a " + go(" ", 1, url)+ "></a></li>");					
					int _previosPage = _currentpage - 1 > 0? (_currentpage - 1):1;
					html.append("<li class=\"previor\"><a " + go(" ", _previosPage, url)+ "></a></li>");				
				}
				if (_currentpage >= _totalPage) {
					html.append("<li class=\"nnext\"><a title=\" \" href=\"javascript:void(0);\"></a></li>");
					html.append("<li class=\"nlast\"><a title=\" \" href=\"javascript:void(0);\"></a></li>");
				}
				else {
					int _nextPage = _currentpage + 1 <= _totalPage? (_currentpage + 1): _totalPage;
					html.append("<li class=\"next\"><a " + go(" ", _nextPage, url)+ "></a></li>");
					html.append("<li class=\"last\"><a " + go(" ", _totalPage, url)+ "></a></li>");
				}
			}
			html.append("</ul></div>");
			
			writer.write(html.toString());   
		} catch (Exception e) {
			e.printStackTrace();
		}
		return true;
	}
    /**
     *  
     */
    private String go(String title, int page,String url){
    	StringBuffer sb = new StringBuffer();
    	sb.append(" title=\"" + title + "\" ");
    	if (CheckUtil.isNullOrEmpty(url)) {
    		sb.append(" href=\"javascript:void(0);\" ");
    		sb.append(" onclick=\"PAS.page.go(" + page+")\" ");
    	}
    	else {
    		sb.append(" href=\"");
	    	sb.append(url);
	    	if (url.indexOf('?') > -1) {
	    		sb.append("&");
	    	}
	    	else {
	    		sb.append("?"); 
	    	}
	    	sb.append("page=" + page); 
	    	sb.append("\"");
    	}
    	return sb.toString();
    }
	public String getPagesize() {
		return pagesize;
	}
	public void setPagesize(String pagesize) {
		this.pagesize = pagesize;
	}
	public String getTotal() {
		return total;
	}
	public void setTotal(String total) {
		this.total = total;
	}
	public String getCurrentpage() {
		return currentpage;
	}
	public void setCurrentpage(String currentpage) {
		this.currentpage = currentpage;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public String getUrlType() {
		return urlType;
	}
	public void setUrlType(String urlType) {
		this.urlType = urlType;
	}

	public String getUnit() {
		return unit;
	}
	public void setUnit(String unit) {
		this.unit = unit;
	}
}

(3)
ラベルtld説明

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
<tlib-version>2.2.3</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>ext</short-name>
<uri>/ext-tages</uri>
<display-name>"Ext Tags"</display-name>
   <!--   -->
   <tag>
       <name>page</name>
       <tag-class>***.PageTag</tag-class>
       <body-content>empty</body-content>
       <attribute>
           <name>pagesize</name>
           <required>false</required>
           <rtexprvalue>true</rtexprvalue>
       </attribute>
       <attribute>
           <name>total</name>
           <required>true</required>
           <rtexprvalue>true</rtexprvalue>
       </attribute>
       <attribute>
           <name>currentpage</name>
           <required>true</required>
           <rtexprvalue>true</rtexprvalue>
       </attribute>
       <attribute>
           <name>url</name>
           <required>false</required>
           <rtexprvalue>true</rtexprvalue>
       </attribute>
       <attribute>
           <name>urlType</name>
           <required>false</required>
           <rtexprvalue>true</rtexprvalue>
       </attribute>
       <attribute>
           <name>theme</name>
           <required>false</required>
           <rtexprvalue>true</rtexprvalue>
       </attribute>  
       <attribute>
           <name>id</name>
           <required>false</required>
           <rtexprvalue>true</rtexprvalue>
       </attribute>                      
   </tag>
</taglib> 

(4)
ページコール

<%@taglib uri="/WEB-INF/tags/ext-tags.tld" prefix="ext" %>
<ext:page total="${total}" currentpage="${page}" pagesize="${pagesize}"/>