url을 통해 보내는 json 내용을 뉴스 이미지 전환 컨트롤로 변환
8517 단어 json
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.JSONTokener;
//
@ResponseBody
@RequestMapping(params = "method=initpic")
public String initpic(HttpServletRequest request, HttpServletResponse response) {
final String NULL_IMAGE_PATH = "http://www.baidu.com/img/bd_logo1.png";
String result="";
StringBuffer html = new StringBuffer();
BufferedReader reader = null;
try{
String orgurl = request.getParameter("url");
URL connect = new URL(orgurl.toString());
URLConnection connection = connect.openConnection();
connection.connect();
reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF-8"));
String line;//
while ((line = reader.readLine()) != null) {
result += line;
}
if(!result.equals("")){
JsonConfig jsonConfig = new JsonConfig();
Map<String, Class> classMap = new HashMap<String, Class>();
classMap.put("result", String.class);
classMap.put("Page", Integer.class);
classMap.put("Pages", Integer.class);
classMap.put("Total", Integer.class);
classMap.put("Pagesize", Integer.class);
classMap.put("Items", HashMap.class);
jsonConfig.setClassMap(classMap);
jsonConfig.setRootClass(HashMap.class);
JSONTokener jt = new JSONTokener(result);
Object obj = jt.nextValue(jsonConfig);
JSONObject ja = JSONObject.fromObject(obj);
Map<String, List<Map>> map = (Map<String, List<Map>>) JSONObject.toBean(ja, jsonConfig);
List itemlist = map.get("Items");
html.append("<div style=\"width:500px;margin:0px auto;\">")
.append("<div class=\"roll-news-keleyi-com\">").append("<div class=\"roll-news-image\">");
for (int i = 0; i < itemlist.size(); i++) {
Map<String,Object> imgmap = (Map<String, Object>) itemlist.get(i);
html.append("<img src=\"");
if(imgmap.get("thumb")!=null && !"".equals(imgmap.get("thumb").toString())){
html.append(imgmap.get("thumb"));
}else{
html.append(NULL_IMAGE_PATH);
}
if(i!=0){
html.append("\" style=\"display:none\" >");
}else{
html.append("\">");
}
}
html.append("</div>");
html.append("<div class=\"roll-news-index\">");
html.append("<ul>");
for (int i = 0; i < itemlist.size(); i++) {
html.append("<li");
if(i == 0){
html.append(" class=\"roll-news-index-hover\"");
}
html.append(">").append(i+1).append("</li>");
}
html.append("</ul>");
html.append("</div>");
html.append("<div class=\"roll-news-title\">");
for (int i = 0; i < itemlist.size(); i++) {
Map<String,Object> imgmap = (Map<String, Object>) itemlist.get(i);
html.append("<a href=\"").append(imgmap.get("thumb"));
html.append("\" ");
if(i!=0){
html.append("style=\"display:none\"");
}
html.append(" >");
html.append(imgmap.get("title"));
html.append("</a>");
}
html.append("</div></div></div>");
System.out.println(html.toString());
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(reader!=null){//
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return html.toString();
}
페이지
<%@page import="java.util.ListResourceBundle"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<HEAD>
<TITLE> </TITLE>
<META content="text/html; charset=utf-8" http-equiv=Content-Type>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="res/js/json2.js" type="text/javascript" charset="UTF-8"></script>
<script src="res/js/jquery-1.8.0.min.js" type="text/javascript" charset="UTF-8"></script>
<style type="text/css">
* {
padding:0px;
margin:0px;
}
.roll-news-keleyi-com {
width:480px;
height:300px;
border:solid 1px #c1c1c1;
}
.roll-news-index-hover {
background-color:white;
}
.roll-news-image img {
width:480px;
height:300px;
}
.roll-news-index {
position:relative;
top:-50px;
margin-right:5px;
float:right;
}
.roll-news-index {
}
.roll-news-index li {
list-style:none;
float:left;
font-size:12px;
font-weight:600;
width:18px;
height:16px;
line-height:16px;
cursor:pointer;
margin:0 3px;
background-image:url(opacity_50.png);
text-align:center;
}
.roll-news-title {
position:relative;
top:-25px;
padding-left:10px;
height:22px;
line-height:20px;
background:url(opacity_50.png);
}
.roll-news-title a {
font-size:12px;
text-decoration:none;
color:white;
background-color:gray
}
.roll-news-title a:hover {
color:red;
}
</style>
</HEAD>
<body>
<div id="imgdiv">
</div>
</body>
<script type="text/javascript">
$(document).ready(function () {
$.ajax({
type : "post",
url : "login.go?method=initpic&url=http://www.xinhuamed.com.cn/getnews.aspx?action=list&page=1&pagesize=10",
contentType : "application/text; charset=utf-8",
success : function(data) {
document.getElementById("imgdiv").innerHTML=data;
},
error : function(msg) {
}
});
setTimeout(function(){
var index = 0;
var slideFlag = true;
var length = $(".roll-news-image img").length;
function showImg(i) {
$(".roll-news-image img")
.eq(i).stop(true, true).fadeIn(800)
.siblings("img").hide();
$(".roll-news-index li").removeClass("roll-news-index-hover");
$(".roll-news-index li").eq(i).addClass("roll-news-index-hover");
$(".roll-news-title a")
.eq(i).stop(true, true).fadeIn(800)
.siblings("a").hide();
}
showImg(index);
$(".roll-news-index li").click(function () {
index = $(".roll-news-index li").index(this);
showImg(index);
slideFlag = false;
});
function autoSlide() {
setInterval(function () {
if (slideFlag) {
showImg((index + 1) % length);
index = (index + 1) % length;
}
slideFlag = true;
}, 3000);
}
autoSlide();
},500);
});
</script>
</html>
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
콘텐츠 SaaS | JSON 스키마 양식 빌더Bloomreach Content를 위한 JSON Form Builder 맞춤형 통합을 개발합니다. 최근 Bloomreach Content SaaS는 내장 앱 프레임워크를 사용하여 혁신적인 콘텐츠 유형 필드를 구축할...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.