/**生成google所读取的sitemap,方便google索引本站*/
@At("/sitemap") //google实际访问的URI是 /sitemap.xml , 不过@At里面的值是绝对不能后缀的,因为它只会去匹配去除后缀的URI
@Ok("void")
public void sitemap(HttpServletRequest req, HttpServletResponse resp) throws IOException {
DBCursor cur = questionColl.find(new BasicDBObject(), new BasicDBObject("updateAt", 1)).sort(new BasicDBObject("createAt", -1));
Tag urlset = Tag.tag("urlset");
urlset.attr("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9")
.attr("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance")
.attr("xsi:schemaLocation", "http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd");
urlset.add("url").add("loc").setText("http://" + req.getHeader("Host"));
while(cur.hasNext()) {
Question question = new Question();
question.setId(cur.next().get("_id").toString());
urlset.add("url").add("loc").setText(Helpers.makeQuestionURL(question));
}
Writer writer = resp.getWriter();
writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
writer.write(urlset.toString());
}