Procházet zdrojové kódy

增加api数据接口标签数据绑定

suntg před 7 roky
rodič
revize
ad18544c70

+ 105 - 0
jpress-web-core/src/main/java/io/jpress/ui/freemarker/tag/ApiDataTag.java

@@ -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();
+			}
+		}
+	}
+
+}

+ 5 - 0
jpress-web-front/src/main/java/io/jpress/front/controller/IndexController.java

@@ -23,6 +23,7 @@ import io.jpress.core.addon.HookInvoker;
 import io.jpress.core.cache.ActionCache;
 import io.jpress.model.query.OptionQuery;
 import io.jpress.router.RouterMapping;
+import io.jpress.ui.freemarker.tag.ApiDataTag;
 import io.jpress.ui.freemarker.tag.IndexPageTag;
 import io.jpress.utils.StringUtils;
 
@@ -50,6 +51,7 @@ public class IndexController extends BaseFrontController {
 
 		if (StringUtils.isBlank(para)) {
 			setAttr(IndexPageTag.TAG_NAME, new IndexPageTag(getRequest(), null, 1, null));
+			setAttr(ApiDataTag.TAG_NAME, new ApiDataTag(getRequest()));
 			render("index.html");
 			return;
 		}
@@ -58,9 +60,11 @@ public class IndexController extends BaseFrontController {
 		if (paras.length == 1) {
 			if (StringUtils.isNumeric(para.trim())) {
 				setAttr(IndexPageTag.TAG_NAME, new IndexPageTag(getRequest(), null, StringUtils.toInt(para.trim(), 1), null));
+				setAttr(ApiDataTag.TAG_NAME, new ApiDataTag(getRequest()));
 				render("index.html");
 			} else {
 				setAttr(IndexPageTag.TAG_NAME, new IndexPageTag(getRequest(), para.trim(), 1, null));
+				setAttr(ApiDataTag.TAG_NAME, new ApiDataTag(getRequest()));
 				render("page_" + para + ".html");
 			}
 		} else if (paras.length == 2) {
@@ -72,6 +76,7 @@ public class IndexController extends BaseFrontController {
 			}
 
 			setAttr(IndexPageTag.TAG_NAME, new IndexPageTag(getRequest(), pageName, StringUtils.toInt(pageNumber, 1), null));
+			setAttr(ApiDataTag.TAG_NAME, new ApiDataTag(getRequest()));
 			render("page_" + pageName + ".html");
 		} else {
 			renderError(404);