采用JSON格式返回数据给资讯客户端 写了这条代码builder.append('['); 出现configure build path 错误,我在WEB/INF/lib中添加了jstl.jar和standar.jar

stan_ljj 发布于 2014/08/04 10:41
阅读 604
收藏 1
package stan.hdu.servlet;


import java.io.IOException;
import java.util.List;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import stan.hdu.domain.News;
import stan.hdu.service.VideoNewsService;
import stan.hdu.service.impl.VideoNewsServiceBean;




public class ListServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private VideoNewsService service = new VideoNewsServiceBean();


protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}


protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<News> videos = service.getLastNews();
String format = request.getParameter("format");
if("json".equals(format)){
// [{id:56,title:"xxxxx",timelength:90},{id:16,title:"xbbx",timelength:20}]
StringBuilder builder = new StringBuilder();
builder.append('[');
for(News news : videos){
builder.append('{');
builder.append("id:").append(news.getId()).append(',');
builder.append("title:\"").append(news.getTitle()).append("\",");
builder.append("timelength:").append(news.getTimelength());
builder.append("},");
}
builder.deleteCharAt(builder.length() - 1);
builder.append(']');
request.setAttribute("json", builder.toString());
request.getRequestDispatcher("/WEB-INF/page/jsonvideonews.jsp").forward(request, response);
}else{
request.setAttribute("videos", videos);
request.getRequestDispatcher("/WEB-INF/page/videonews.jsp").forward(request, response);
}
}


}

加载中
0
落叶飞花
落叶飞花
把数据放到list里面,一句话就转json了,搞一大堆,蛋疼不?
0
黑狗
黑狗
json,gson很多开源库给你用 自行百度
0
loki_lan
loki_lan
自己拼装的JSON格式不加双引号,用Jquery的话,会报错!
0
Ryan-瑞恩
Ryan-瑞恩
好可爱的孩子啊。。。。。。。。。。。。用FastJson去转吧。
__loong
__loong
+1
0
0
陈阳阳阳
陈阳阳阳
哥 你这是在手动拼接json吗!
0
走位风骚闪着腰
走位风骚闪着腰
我曾经也傻傻的干过。。
返回顶部
顶部