View Javadoc
1   package gboat2.base.view.components;
2   
3   import java.io.Writer;
4   
5   import javax.servlet.http.HttpServletRequest;
6   import javax.servlet.http.HttpServletResponse;
7   
8   import org.apache.commons.lang3.RandomStringUtils;
9   import org.apache.commons.lang3.StringUtils;
10  import org.apache.struts2.components.ClosingUIBean;
11  import org.apache.struts2.views.annotations.StrutsTag;
12  import org.apache.struts2.views.annotations.StrutsTagAttribute;
13  import org.springframework.util.Assert;
14  
15  import com.opensymphony.xwork2.util.ValueStack;
16  
17  /**
18   * 创建 jquery-ui tab 控件
19   * <p>
20   * 示例代码:
21   * </p>
22   * <p/>
23   * <pre>
24   * &lt;g2:tab href="#t2" closable="true"&gt;显示Div中的内容&lt;/g2:tab&gt;
25   * &lt;div id="t2"&gt;这个 DIV 的内容将显示在 Tab 中&lt;/div&gt;
26   * 
27   * &lt;g2:tab id="tab32" href="none/example.action?forward=ajax/ajax1" formIds="form_3"&gt;Ajax加载&lt;/g2:tab&gt;
28   * &lt;g2:form id="form_3"&gt;
29   *     &lt;g2:textfield name="test" value="hello" label="测试"/&gt;
30   * &lt;/g2:form&gt;
31   * </pre>
32   * <p/>
33   * 
34   * @date 2014年5月24日
35   * @author <a href="mailto:[email protected]">何明旺</a>
36   * @since 3.0.0-SNAPSHOT
37   * @see TabbedPanel
38   */
39  @StrutsTag(
40          name = "tab",
41          tldTagClass = "gboat2.base.view.jsp.TabTag",
42          description = "创建 jquery-ui tab 控件",
43          allowDynamicAttributes = true)
44  public class Tab extends ClosingUIBean {
45  
46      public static final String TEMPLATE = "tab";
47      public static final String TEMPLATE_CLOSE = "tab-close";
48  
49      protected String closable;
50      protected String formIds;
51      protected String href;
52      
53      public Tab(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
54          super(stack, request, response);
55      }
56  
57      @Override
58      public String getDefaultOpenTemplate() {
59          return TEMPLATE;
60      }
61  
62      @Override
63      public String getDefaultTemplate() {
64          return TEMPLATE_CLOSE;
65      }
66      
67      @Override
68      public void evaluateExtraParams() {
69          super.evaluateExtraParams();
70  
71          TabbedPanel tabbedPanel = (TabbedPanel) findAncestor(TabbedPanel.class);
72          Assert.notNull(tabbedPanel, "<g2:tab> 标签必须位于 <g2:tabbedpanel> 标签中!");
73          addParameter("tabbedPanelId", tabbedPanel.getId());
74          
75          if (closable != null)
76              addParameter("closable", findValue(closable, Boolean.class));
77          
78          if (formIds != null)
79              addParameter("formIds", findString(formIds));
80  
81          if (href != null)
82              addParameter("href", findString(href));
83          
84          if (StringUtils.isBlank(id)) {
85              this.id = "tab_" + RandomStringUtils.randomNumeric(3);
86              addParameter("id", this.id);
87          }
88      }
89  
90      @Override
91      public boolean end(Writer writer, String body) {
92          return super.end(writer, StringUtils.defaultIfBlank(body, label));
93      }
94  
95      @StrutsTagAttribute(description = "是否可以关闭", type = "Boolean", defaultValue = "false")
96      public void setClosable(String closable) {
97          this.closable = closable;
98      }
99  
100     @StrutsTagAttribute(name = "formIds", description = "在发起 ajax 请求时,会序列化 id 为该值的表单中所有表单域作为请求参数,多个表单 id 之间用逗号分隔")
101     public void setFormIds(String formIds) {
102         this.formIds = formIds;
103     }
104 
105     @StrutsTagAttribute(description = "Tab 页显示内容对应的锚链接或 ajax 请求地址,如: #div1 或 /ajax.do", required = true)
106     public void setHref(String href) {
107         this.href = href;
108     }
109 }