|
|
@@ -0,0 +1,105 @@
|
|
|
+/**
|
|
|
+ * Copyright (c) 2015-2016, Michael Yang 杨福海 (fuhai999@gmail.com).
|
|
|
+ *
|
|
|
+ * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.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.gnu.org/licenses/lgpl-3.0.txt
|
|
|
+ *
|
|
|
+ * 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.
|
|
|
+ */
|
|
|
+package io.jpress.ui.freemarker.tag;
|
|
|
+
|
|
|
+import io.jpress.core.render.freemarker.JTag;
|
|
|
+import io.jpress.utils.HttpUtils;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.util.Date;
|
|
|
+
|
|
|
+public class ApiDataTag extends JTag {
|
|
|
+
|
|
|
+ public static final String TAG_NAME = "jp.apiData";
|
|
|
+
|
|
|
+ HttpServletRequest request;
|
|
|
+
|
|
|
+ public ApiDataTag(HttpServletRequest request) {
|
|
|
+ this.request = request;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onRender() {
|
|
|
+
|
|
|
+ String id = getParam("id");
|
|
|
+
|
|
|
+ setVariable("id", id);
|
|
|
+
|
|
|
+ renderBody();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * api 地址及数据结果类
|
|
|
+ */
|
|
|
+ public static class ApiData {
|
|
|
+
|
|
|
+ private String id;
|
|
|
+ private String url;
|
|
|
+ private String data;
|
|
|
+ private Date updateTime;
|
|
|
+
|
|
|
+ public ApiData(String id, String url) {
|
|
|
+ this.id = id;
|
|
|
+ this.url = url;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getId() {
|
|
|
+ return id;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setId(String id) {
|
|
|
+ this.id = id;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getUrl() {
|
|
|
+ return url;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setUrl(String url) {
|
|
|
+ this.url = url;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getData() {
|
|
|
+ return data;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setData(String data) {
|
|
|
+ this.data = data;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Date getUpdateTime() {
|
|
|
+ return updateTime;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setUpdateTime(Date updateTime) {
|
|
|
+ this.updateTime = updateTime;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送请求获取数据
|
|
|
+ */
|
|
|
+ public void sendRequest() {
|
|
|
+ try {
|
|
|
+ String responseData = HttpUtils.get(this.url);
|
|
|
+ this.data = responseData;
|
|
|
+ this.updateTime = new Date();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|