9
回答
采用JSON格式返回数据给资讯客户端 写了这条代码builder.append('['); 出现configure build path 错误,我在WEB/INF/lib中添加了jstl.jar和standar.jar
华为云实践训练营,热门技术免费实践!>>>   
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);
}
}


}

举报
stan_ljj
发帖于3年前 9回/596阅
顶部