Skip to content

Commit

Permalink
增加了首页大广告位
Browse files Browse the repository at this point in the history
  • Loading branch information
sdksdk0 committed Oct 5, 2016
1 parent 2e17466 commit 3d98ea2
Show file tree
Hide file tree
Showing 20 changed files with 459 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cn.tf.taotao.common.utils;

import java.io.PrintWriter;
import java.io.StringWriter;

public class ExceptionUtil {

/**
* 获取异常的堆栈信息
*
* @param t
* @return
*/
public static String getStackTrace(Throwable t) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);

try {
t.printStackTrace(pw);
return sw.toString();
} finally {
pw.close();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package cn.tf.taotao.common.utils;

import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class HttpClientUtil {

public static String doGet(String url, Map<String, String> param) {

// 创建Httpclient对象
CloseableHttpClient httpclient = HttpClients.createDefault();

String resultString = "";
CloseableHttpResponse response = null;
try {
// 创建uri
URIBuilder builder = new URIBuilder(url);
if (param != null) {
for (String key : param.keySet()) {
builder.addParameter(key, param.get(key));
}
}
URI uri = builder.build();

// 创建http GET请求
HttpGet httpGet = new HttpGet(uri);

// 执行请求
response = httpclient.execute(httpGet);
// 判断返回状态是否为200
if (response.getStatusLine().getStatusCode() == 200) {
resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (response != null) {
response.close();
}
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return resultString;
}

public static String doGet(String url) {
return doGet(url, null);
}

public static String doPost(String url, Map<String, String> param) {
// 创建Httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String resultString = "";
try {
// 创建Http Post请求
HttpPost httpPost = new HttpPost(url);
// 创建参数列表
if (param != null) {
List<NameValuePair> paramList = new ArrayList<>();
for (String key : param.keySet()) {
paramList.add(new BasicNameValuePair(key, param.get(key)));
}
// 模拟表单
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList);
httpPost.setEntity(entity);
}
// 执行http请求
response = httpClient.execute(httpPost);
resultString = EntityUtils.toString(response.getEntity(), "utf-8");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

return resultString;
}

public static String doPost(String url) {
return doPost(url, null);
}

public static String doPostJson(String url, String json) {
// 创建Httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String resultString = "";
try {
// 创建Http Post请求
HttpPost httpPost = new HttpPost(url);
// 创建请求内容
StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
httpPost.setEntity(entity);
// 执行http请求
response = httpClient.execute(httpPost);
resultString = EntityUtils.toString(response.getEntity(), "utf-8");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

return resultString;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,16 @@ public String getContent() {
public void setContent(String content) {
this.content = content == null ? null : content.trim();
}

@Override
public String toString() {
return "TbContent [id=" + id + ", categoryId=" + categoryId
+ ", title=" + title + ", subTitle=" + subTitle
+ ", titleDesc=" + titleDesc + ", url=" + url + ", pic=" + pic
+ ", pic2=" + pic2 + ", created=" + created + ", updated="
+ updated + ", content=" + content + "]";
}



}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://qdm11501120.my3w.com:3306/qdm11501120_db?characterEncoding=utf-8
jdbc.username=qdm11501120
jdbc.password=admin123
jdbc.url=jdbc:mysql://localhost:3306/taotao?characterEncoding=utf-8
jdbc.username=zp
jdbc.password=a
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#Generated by Maven Integration for Eclipse
#Tue Oct 04 21:30:12 CST 2016
#Wed Oct 05 11:04:49 CST 2016
version=0.0.1-SNAPSHOT
groupId=cn.tf.taotao
m2e.projectName=taotao-manager-web
Expand Down
1 change: 1 addition & 0 deletions taotao-portal/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>
5 changes: 5 additions & 0 deletions taotao-portal/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
<artifactId>taotao-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>cn.tf.taotao</groupId>
<artifactId>taotao-manager-pojo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,38 @@
package cn.tf.taotao.portal.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.tf.taotao.common.utils.TaotaoResult;
import cn.tf.taotao.portal.service.ContentService;


@Controller
public class IndexController {

@Autowired
private ContentService contentService;


@RequestMapping("/index")
public String showIndex(){
public String showIndex(Model model){
String adResult = contentService.getContentList();
model.addAttribute("ad1", adResult);

return "index";
}

//模拟post请求
@RequestMapping(value="/httpclient/post",method=RequestMethod.POST)
@ResponseBody
public String tesePost(String username,String password){
return username+"_"+password;
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package cn.tf.taotao.portal.service;

public interface ContentService {
String getContentList();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package cn.tf.taotao.portal.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import cn.tf.taotao.common.utils.HttpClientUtil;
import cn.tf.taotao.common.utils.JsonUtils;
import cn.tf.taotao.common.utils.TaotaoResult;
import cn.tf.taotao.po.TbContent;
import cn.tf.taotao.portal.service.ContentService;

@Service
public class ContentServiceImpl implements ContentService{


@Value("${REST_BASE_URL}")
private String REST_BASE_URL;
@Value("${REST_INDEX_AD_URL}")
private String REST_INDEX_AD_URL;



@Override
public String getContentList() {
String result = HttpClientUtil.doGet(REST_BASE_URL + REST_INDEX_AD_URL);
//把json数据转换成对象
TaotaoResult taotaoResult = TaotaoResult.formatToList(result, TbContent.class);
List<TbContent> list = (List<TbContent>) taotaoResult.getData();
List<Map> resultList=new ArrayList<>();

try {
for (TbContent tbContent : list) {
Map map = new HashMap<>();
map.put("src", tbContent.getPic());
map.put("height", 240);
map.put("width", 670);
map.put("srcB", tbContent.getPic2());
map.put("widthB", 550);
map.put("height", 240);
map.put("href", tbContent.getUrl());
map.put("alt", tbContent.getSubTitle());
resultList.add(map);
}
return JsonUtils.objectToJson(resultList);
} catch (Exception e) {
e.printStackTrace();
return null;
}

}

}
Empty file.
2 changes: 2 additions & 0 deletions taotao-portal/src/main/resources/resource/resource.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
REST_BASE_URL=http://localhost:8084/rest
REST_INDEX_AD_URL=/content/list/89
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#Generated by Maven Integration for Eclipse
#Tue Oct 04 19:28:23 CST 2016
#Wed Oct 05 11:08:12 CST 2016
version=0.0.1-SNAPSHOT
groupId=cn.tf.taotao
m2e.projectName=taotao-portal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
<artifactId>taotao-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>cn.tf.taotao</groupId>
<artifactId>taotao-manager-pojo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
Expand Down
Loading

0 comments on commit 3d98ea2

Please sign in to comment.