Struts2 自定义标签与struts2 无缝整合支技ognl 等

struts2 专栏收录该内容
11 篇文章 0 订阅

author liuqing

刘庆

标签原理这里就不做详细介绍了

1. 首先我们来读一读struts2 的源代码

 

 public abstract class UIBean extends Component {

    private static final Logger LOG = LoggerFactory.getLogger(UIBean.class);

    protected HttpServletRequest request;
    protected HttpServletResponse response;

    public UIBean(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
        super(stack);
        this.request = request;
        this.response = response;
        this.templateSuffix = ContextUtil.getTemplateSuffix(stack.getContext());
    }

    // The templateSuffic to use, overrides the default one if not null.
    protected String templateSuffix;

    // The template to use, overrides the default one.
    protected String template;

    // templateDir and theme attributes
    //定义freemark.ftl文件夹
    protected String templateDir;
    //对应根目录
    protected String theme;

    // shortcut, sets label, name, and value
    protected String key;

    protected String id;
    protected String cssClass;
    protected String cssStyle;
    protected String cssErrorClass;
    protected String cssErrorStyle;
    protected String disabled;
    protected String label;
    protected String labelPosition;
    protected String labelSeparator;
    protected String requiredposition;
    protected String name;
    protected String required;
    protected String tabindex;
    protected String value;
    protected String title;

    // HTML scripting events attributes
    protected String onclick;
    protected String ondblclick;
    protected String onmousedown;
    protected String onmouseup;
    protected String onmouseover;
    protected String onmousemove;
    protected String onmouseout;
    protected String onfocus;
    protected String onblur;
    protected String onkeypress;
    protected String onkeydown;
    protected String onkeyup;
    protected String onselect;
    protected String onchange;

    // common html attributes
    protected String accesskey;

    // javascript tooltip attribute
    protected String tooltip;
    protected String tooltipConfig;
    protected String javascriptTooltip;
    protected String tooltipDelay;
    protected String tooltipCssClass;
    protected String tooltipIconPath;

    // dynamic attributes
    protected Map<String,Object> dynamicAttributes = new HashMap<String,Object>();

    protected String defaultTemplateDir;
    protected String defaultUITheme;
    protected TemplateEngineManager templateEngineManager;

    //注入对应的目录文件夹内容
    @Inject(StrutsConstants.STRUTS_UI_TEMPLATEDIR)
    public void setDefaultTemplateDir(String dir) {
        this.defaultTemplateDir = dir;
    }

 

    定义基Bean

   package org.cxkj.struts2.components;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.components.UIBean;
import org.apache.struts2.views.annotations.StrutsTagAttribute;

import com.opensymphony.xwork2.util.ValueStack;

/**
 * 
 * @author liuqing
 * @version 2.0
 * 2011-3-8
 *
 */
public abstract class HtmlBean extends UIBean {
	
	protected String width;
	
	protected String height;
	
	protected String align;

	public HtmlBean(ValueStack stack, HttpServletRequest request,
			HttpServletResponse response) {
		super(stack, request, response);
	}
	
	

	/* (non-Javadoc)
	 * @see org.apache.struts2.components.UIBean#evaluateParams()
	 */
	@Override
	public void evaluateParams() {
		super.evaluateParams();
		if (width != null) {
            addParameter("width", findString(width));
        }
		if (height != null) {
            addParameter("height", findString(height));
        }
		if (align != null) {
            addParameter("align", findString(align));
        }
	}



	/**
	 * @param width the width to set
	 */
	@StrutsTagAttribute(description="")
	public void setWidth(String width) {
		this.width = width;
	}

	/**
	 * @param height the height to set
	 */
	@StrutsTagAttribute(description="")
	public void setHeight(String height) {
		this.height = height;
	}

	/**
	 * @param align the align to set
	 */
	@StrutsTagAttribute(description="")
	public void setAlign(String align) {
		this.align = align;
	}
	
}

 

   添加时间控件

 

   package org.cxkj.struts2.views.jsp.ui;


import org.apache.struts2.views.jsp.ui.AbstractUITag;
import org.cxkj.struts2.components.HtmlBean;


/**
 * 
 * @author liuqing
 * @version 2.0
 * 2011-3-8
 *
 */
public abstract class HtmlDateboxTag extends AbstractUITag {
	
    protected String width;
	
	protected String height;
	
	protected String align;

	/* (non-Javadoc)
	 * @see org.apache.struts2.views.jsp.ui.AbstractUITag#populateParams()
	 */
	@Override
	protected void populateParams() {
		super.populateParams();
		HtmlBean html = (HtmlBean)this.component;
		html.setWidth(width);
		html.setHeight(height);
		html.setAlign(align);
		html.setDynamicAttributes(dynamicAttributes);
	}

	/**
	 * @param width the width to set
	 */
	public void setWidth(String width) {
		this.width = width;
	}

	/**
	 * @param height the height to set
	 */
	public void setHeight(String height) {
		this.height = height;
	}

	/**
	 * @param align the align to set
	 */
	public void setAlign(String align) {
		this.align = align;
	}

}

  定义实现标签

 package org.cxkj.struts2.views.jsp.ui;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.components.Component;
import org.cxkj.struts2.components.DateboxBean;

import com.opensymphony.xwork2.util.ValueStack;

/**
 * 
 * @author liuqing
 * @version 2.0
 * 2011-3-8
 *
 */
public class DateboxTag extends HtmlDateboxTag {

	@Override
	public Component getBean(ValueStack valueStack, HttpServletRequest request,
			HttpServletResponse response) {
		return new DateboxBean(valueStack,request,response);
	}

}

 

 

 

定义freemarker 模板文件

 <#--

/*
 * $Id: text.ftl 720258 2008-11-24 19:05:16Z musachy $
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
-->
<div style="margin:2px;text-align:<#rt/>
<#if parameters.align??>
${parameters.align?html};"> <#rt/>
</#if>
<input type="text"<#rt/>
 name="${parameters.name?default("")?html}"<#rt/>
 class="easyui-datebox" <#rt/>
<#if parameters.get("size")??>
 size="${parameters.get("size")?html}"<#rt/>
</#if>
<#if parameters.maxlength??>
 maxlength="${parameters.maxlength?html}"<#rt/>
</#if>
<#if parameters.nameValue??>
 value="<@s.property value="parameters.nameValue"/>"<#rt/>
</#if>
<#if parameters.disabled?default(false)>
 disabled="disabled"<#rt/>
</#if>
<#if parameters.readonly?default(false)>
 readonly="readonly"<#rt/>
</#if>
<#if parameters.tabindex??>
 tabindex="${parameters.tabindex?html}"<#rt/>
</#if>
<#if parameters.id??>
 id="${parameters.id?html}"<#rt/>
</#if><#rt/>
 style="<#rt/>
<#if parameters.width??>
width:${parameters.width?html}px;<#rt/>
<#else>
width:160px;<#rt/>
</#if><#rt/>
<#if parameters.height??>
height:${parameters.height?html}px;<#rt/>
<#else>
height:26px;<#rt/>
</#if>" <#rt/>
<#include "/${parameters.templateDir}/simple/css.ftl" />
<#if parameters.title??>
 title="${parameters.title?html}"<#rt/>
</#if>
<#include "/${parameters.templateDir}/simple/scripting-events.ftl" />
<#include "/${parameters.templateDir}/simple/common-attributes.ftl" />
<#include "/${parameters.templateDir}/simple/dynamic-attributes.ftl" />
/>
</div>
 

 添加tld 文件到WEB-INF目录下

OK<?xml version="1.0" encoding="UTF-8" standalone="no"?>

<taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd">
  <description><![CDATA["To make it easier to access dynamic data;
                    the Apache Struts framework includes a library of custom tags.
                    The tags interact with the framework's validation and internationalization features;
                    to ensure that input is correct and output is localized.
                    The Struts Tags can be used with JSP FreeMarker or Velocity."]]></description>
  <display-name>"Struts Tags"</display-name>
  <tlib-version>2.2.3</tlib-version>
  <short-name>sx</short-name>
  <uri>/struts-tags-cxkj</uri>
  <tag>
    <description><![CDATA[Execute an action from within a view]]></description>
    <name>datebox</name>
    <tag-class>org.cxkj.struts2.views.jsp.ui.DateboxTag</tag-class>
    <body-content>JSP</body-content>
    <attribute>
      <description><![CDATA[Whether the result of this action (probably a view) should be executed/rendered]]></description>
      <name>executeResult</name>
      <required>false</required>
      <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
      <description><![CDATA[Whether the writer should be flush upon end of action component tag, default to true]]></description>
      <name>flush</name>
      <required>false</required>
      <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
      <description><![CDATA[Deprecated. Use 'var' instead]]></description>
      <name>width</name>
      <required>false</required>
      <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
      <description><![CDATA[Deprecated. Use 'var' instead]]></description>
      <name>height</name>
      <required>false</required>
      <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
      <description><![CDATA[Deprecated. Use 'var' instead]]></description>
      <name>id</name>
      <required>false</required>
      <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
      <description><![CDATA[Whether the request parameters are to be included when the action is invoked]]></description>
      <name>ignoreContextParams</name>
      <required>false</required>
      <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
      <description><![CDATA[Name of the action to be executed (without the extension suffix eg. .action)]]></description>
      <name>name</name>
      <required>true</required>
      <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
      <description><![CDATA[Namespace for action to call]]></description>
      <name>namespace</name>
      <required>false</required>
      <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
      <description><![CDATA[Namespace for action to call]]></description>
      <name>value</name>
      <required>false</required>
      <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
      <description><![CDATA[Namespace for action to call]]></description>
      <name>align</name>
      <required>false</required>
      <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
      <description><![CDATA[Whether an exception should be rethrown, if the target action throws an exception]]></description>
      <name>rethrowException</name>
      <required>false</required>
      <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
      <description><![CDATA[Name used to reference the value pushed into the Value Stack]]></description>
      <name>var</name>
      <required>false</required>
      <rtexprvalue>false</rtexprvalue>
    </attribute>
    <dynamic-attributes>false</dynamic-attributes>
  </tag>
  
 </taglib>

 

<%@taglib prefix="sx" uri="/struts-tags-cxkj" %>
<sx:datebox name="umlj" width="400" height="80" align="right" value="%{'1008-10-23'}">
</sx:datebox>
 

 

 

 

 

 

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值