View Javadoc
1   package gboat2.base.view.components;
2   
3   import java.io.Writer;
4   
5   import org.apache.struts2.components.Component;
6   import org.apache.struts2.components.UIBean;
7   import org.apache.struts2.views.annotations.StrutsTag;
8   import org.apache.struts2.views.annotations.StrutsTagAttribute;
9   
10  import com.opensymphony.xwork2.util.ValueStack;
11  
12  /**
13   * 通过 Ajax 请求加载远程服务器上数据的组件
14   * 
15   * @author <a href="mailto:[email protected]">何明旺</a>
16   * @since 3.0
17   * @date 2014年5月4日
18   */
19  @StrutsTag(
20          name = "remote",
21          tldTagClass = "gboat2.base.view.jsp.RemoteTag",
22          description = "通过 Ajax 请求加载远程服务器上数据的组件")
23  public class Remote extends Component {
24  
25      protected String targets;
26      
27      protected String href;
28      
29      protected String formIds;
30      
31      protected String indicator;
32      
33      protected String loadingText;
34      
35      protected String errorText;
36      
37      protected String errorElementId;
38  
39      protected String dataType;
40      
41      protected String requestType;
42      
43      protected String effect;
44      
45      protected String effectDuration;
46      
47      protected String effectOptions;
48      
49      protected String effectMode;
50      
51      protected String timeout;
52      
53      protected String listenTopics;
54      
55      protected String onEffectCompleteTopics;
56  
57      public Remote(ValueStack stack) {
58          super(stack);
59      }
60  
61      @Override
62      public boolean end(Writer writer, String body) {
63          Component component = findAncestor(UIBean.class);
64  
65          if (href != null) {
66              String hrefValue = findString(href);
67              component.addParameter("href", hrefValue);
68              if (hrefValue.indexOf("?") > 0) {
69                  component.addParameter("hrefUrl", hrefValue.substring(0, hrefValue.indexOf("?")));
70                  component.addParameter("hrefParameter", hrefValue.substring(hrefValue.indexOf("?") + 1));
71              } else {
72                  component.addParameter("hrefUrl", hrefValue);
73              }
74          }
75          
76          if (targets != null)
77              component.addParameter("targets", findString(targets));
78          
79          if (formIds != null)
80              component.addParameter("formIds", findString(formIds));
81          
82          if (indicator != null)
83              component.addParameter("indicator", findString(indicator));
84          
85          if (loadingText != null)
86              component.addParameter("loadingText", findString(loadingText));
87          
88          if (errorText != null)
89              component.addParameter("errorText", findString(errorText));
90          
91          if (errorElementId != null)
92              component.addParameter("errorElementId", findString(errorElementId));
93          
94          if (dataType != null)
95              component.addParameter("dataType", findString(dataType));
96          
97          if (requestType != null)
98              component.addParameter("requestType", findString(requestType));
99          
100         if (effect != null)
101             component.addParameter("effect", findString(effect));
102         
103         if (effectDuration != null)
104             component.addParameter("effectDuration", findValue(effectDuration, Integer.class));
105         
106         if (effectOptions != null)
107             component.addParameter("effectOptions", findString(effectOptions));
108         
109         if (effectMode != null)
110             component.addParameter("effectMode", findString(effectMode));
111         
112         if (timeout != null)
113             component.addParameter("timeout", findValue(timeout, Integer.class));
114         
115         if (listenTopics != null)
116             component.addParameter("listenTopics", findString(listenTopics));
117         
118         if (onEffectCompleteTopics != null)
119             component.addParameter("onEffectCompleteTopics", findString(onEffectCompleteTopics));
120         
121         return super.end(writer, "");
122     }
123 
124     @StrutsTagAttribute(name = "targets", description = "显示 ajax 请求到的数据的 DOM 节点的 id,多值之间使用逗号分隔")
125     public void setTargets(String targets) {
126         this.targets = targets;
127     }
128 
129     @StrutsTagAttribute(name = "href", description = "点击元素时,ajax 请求的远程 URL")
130     public void setHref(String href) {
131         this.href = href;
132     }
133 
134     @StrutsTagAttribute(name = "formIds", description = "在发起 ajax 请求时,会序列化 id 为该值的表单中所有表单域作为请求参数,多个表单 id 之间用逗号分隔")
135     public void setFormIds(String formIds) {
136         this.formIds = formIds;
137     }
138 
139     @StrutsTagAttribute(name = "indicator", description = "如果是从远程服务器加载内容到某个目标容器中,则在加载过程中,id 为该值的元素将会被显示在目录容器中,在加载完成后,又自动隐藏该元素。")
140     public void setIndicator(String indicator) {
141         this.indicator = indicator;
142     }
143 
144     @StrutsTagAttribute(name = "loadingText", description = "在 ajax 请求过程中显示的文本内容,如:正在加载...")
145     public void setLoadingText(String loadingText) {
146         this.loadingText = loadingText;
147     }
148 
149     @StrutsTagAttribute(name = "errorText", description = "加载数据发生错误时,显示的错误信息。如果属性 'errorElement' 设置了值,而且存在对应的元素,则将错误信息显示在 'errorElement',否则将其显示在 targets 对应的元素中。")
150     public void setErrorText(String errorText) {
151         this.errorText = errorText;
152     }
153 
154     @StrutsTagAttribute(name = "errorElementId", description = "显示错误消息的 DOM 节点的 id")
155     public void setErrorElementId(String errorElementId) {
156         this.errorElementId = errorElementId;
157     }
158 
159     @StrutsTagAttribute(description = "ajax 请求的数据类型,可选值: html, xml, script, text, json, jsonp")
160     public void setDataType(String dataType) {
161         this.dataType = dataType;
162     }
163 
164     @StrutsTagAttribute(description = "ajax 请求的类型,可选值:POST, GET, PUT", defaultValue = "POST")
165     public void setRequestType(String requestType) {
166         this.requestType = requestType;
167     }
168 
169     @StrutsTagAttribute(description = "对 'targets' 属性指定的元素设置效果。如:bounce, highlight, pulsate, shake, size 或 transfer. 查看更详细的资料请参见 http://docs.jquery.com/UI/Effects", defaultValue = "none")
170     public void setEffect(String effect) {
171         this.effect = effect;
172     }
173 
174     @StrutsTagAttribute(description = "效果的持续时间,单位为毫秒,只有设置了属性 'effect' 的值时才生效", type="Integer", defaultValue = "2000")
175     public void setEffectDuration(String effectDuration) {
176         this.effectDuration = effectDuration;
177     }
178 
179     @StrutsTagAttribute(description = "效果参数,如:{'color : #aaaaaa'} 或 {'times' : 3},只有设置了属性 'effect' 的值时才生效。查看更详细的资料请参见 http://docs.jquery.com/UI/Effects")
180     public void setEffectOptions(String effectOptions) {
181         this.effectOptions = effectOptions;
182     }
183 
184     @StrutsTagAttribute(description = "效果模式,可选值:show, hide, toggle, none", defaultValue = "none")
185     public void setEffectMode(String effectMode) {
186         this.effectMode = effectMode;
187     }
188 
189     @StrutsTagAttribute(description = "ajax 请求的超时时间,单位为毫秒", type = "Integer", defaultValue = "3000")
190     public void setTimeout(String timeout) {
191         this.timeout = timeout;
192     }
193 
194     @StrutsTagAttribute(description = "订阅的话题列表,多值之间使用逗号进行分隔。当监听到指定的话题被发布时,则发起一个新的 ajax 请求。")
195     public void setListenTopics(String listenTopics) {
196         this.listenTopics = listenTopics;
197     }
198 
199     @StrutsTagAttribute(description = "当某一效果执行完成后发布的话题,多值之间使用逗号进行分隔。")
200     public void setOnEffectCompleteTopics(String onEffectCompleteTopics) {
201         this.onEffectCompleteTopics = onEffectCompleteTopics;
202     }
203 
204 }