zoukankan      html  css  js  c++  java
  • struts2 实现自定义标签

    /**
     * lostingz
     * Created on 2015年11月18日
     */
    package com.test.web.tags;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.apache.struts2.components.Component;
    import org.apache.struts2.views.jsp.ComponentTagSupport;
    
    import com.opensymphony.xwork2.util.ValueStack;
    
    public class TestTag extends ComponentTagSupport {
    
        private static final long serialVersionUID = 1L;
        private String code;
    
        /**
         * 获得一个基本类的对象。
         *
         * @param stack
         * @param req
         * @param res
         * @return
         */
        @Override
        public Component getBean(ValueStack stack, HttpServletRequest req, HttpServletResponse res) {
            return new TestComponent(stack);
        }
         /**
         * 设置标签获取到的code对象。
         * @param code
         */
        public void setCode(String code) {
            this.code = code;
        }
    
        protected void populateParams() {
            super.populateParams();
            TestComponent testComponent = (TestComponent) component;
            //将code赋值给compnent
            testComponent.setCode(code);
        }
    
    }
    /**
     * lostingz
     * Created on 2015年11月18日
     */
    package com.test.web.tags;
    
    import java.io.IOException;
    import java.io.Writer;
    
    import org.apache.struts2.components.Component;
    import com.opensymphony.xwork2.util.ValueStack;
    
    public class TestComponent extends Component {
    
        private String code;
    
        public TestComponent(ValueStack stack) {
            super(stack);
        }
    
        public String getCode() {
            return code;
        }
    
        public void setCode(String code) {
            this.code = code;
        }
    
        public boolean start(Writer writer) {
            boolean flag = true;
            try {
                String result = getInfo(code);
           //输出 writer.write(result); }
    catch (IOException e) { e.printStackTrace(); } return flag; } public boolean end(Writer writer, String body) { return super.end(writer, body); } private String getInfo(String code){
         //处理code得到显示的信息
    return doSomething(code); } }

    配置

    <taglib>   
        <tlib-version>2.2.3</tlib-version>   
        <jsp-version>1.2</jsp-version>   
        <short-name>test</short-name>   
        <uri>/testTag</uri><display-name>"test"</display-name>   
            <tag>   
                <name>getCode</name>   
                <tag-class>com.test.web.tags.TestTag</tag-class>    
                <body-content>empty</body-content>       
                <attribute>   
                    <name>code</name>   
                    <required>false</required>   
                    <rtexprvalue>true</rtexprvalue>   
                </attribute>
            </tag>   
    </taglib> 
  • 相关阅读:
    67. Add Binary
    66. Plus One
    64. Minimum Path Sum
    63. Unique Paths II
    How to skip all the wizard pages and go directly to the installation process?
    Inno Setup打包之先卸载再安装
    How to change the header background color of a QTableView
    Openstack object list 一次最多有一万个 object
    Openstack 的 Log 在 /var/log/syslog 里 【Ubuntu】
    Git 分支
  • 原文地址:https://www.cnblogs.com/birkhoff/p/4974647.html
Copyright © 2011-2022 走看看