发现自从换了一套博客程序后,之前搜索引擎抓取的链接都失效了,所以才有此想法采用java生成动态的网站地图。 # 步骤一 创建实体 > SiteMapVo
```java package com.my.blog.website.modal.Vo;
import com.my.blog.website.utils.SiteMapUtils; import org.thymeleaf.util.DateUtils;
import java.util.Date; import java.util.Locale;
/** * @author Jesse-liu * @description: 网站地图entity * @date 2020/5/7 9:09 */ public class SiteMapVo { /** * url https://www.xxx.com */ private String loc; /** * 最后更新时间 yyyy-MM-dd */ private Date lastmod; /** * 更新速度 always hourly daily weekly monthly yearly never */ private String changefreq; /** * 权重 1.0 0.9 0.8 */ private String priority;
public String getLoc() { return loc; }
public void setLoc(String loc) { this.loc = loc; }
public Date getLastmod() { return lastmod; }
public void setLastmod(Date lastmod) { this.lastmod = lastmod; }
public String getChangefreq() { return changefreq; }
public void setChangefreq(String changefreq) { this.changefreq = changefreq; }
public String getPriority() { return priority; }
public void setPriority(String priority) { this.priority = priority; }
public SiteMapVo() {
}
public SiteMapVo(String loc) { this.loc = loc; this.lastmod = new Date(); this.changefreq = SiteMapUtils.CHANGEFREQ_ALWAYS; this.priority = "1.0"; }
public SiteMapVo(String loc, Date lastmod, String changefreq, String priority) { this.loc = loc; this.lastmod = lastmod; this.changefreq = changefreq; this.priority = priority; }
@Override /** 重写 toString 适应xml格式 */ public String toString() { StringBuffer sb = new StringBuffer(); sb.append("
"); sb.append("" + loc + ""); sb.append("" + DateUtils.format(lastmod, "yyyy-MM-dd", Locale.SIMPLIFIED_CHINESE) + ""); sb.append("" + changefreq + ""); sb.append("" + priority + ""); sb.append("
"); return sb.toString(); } }
```
# 步骤二 创建工具类 > SiteMapUtils
```java package com.my.blog.website.utils;
import com.my.blog.website.constant.WebConst; import com.my.blog.website.modal.Vo.ContentVo; import com.my.blog.website.modal.Vo.SiteMapVo; import com.my.blog.website.service.IContentService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component;
import java.util.List;
/** * @author Jesse-liu * @description: java生成sitemap网站地图工具类 * @date 2020/5/7 10:25 */ @Component public class SiteMapUtils {
public final static String BEGIN_DOC = "
"; public final static String END_DOC = "
"; public final static String CHANGEFREQ_ALWAYS = "always"; public final static String CHANGEFREQ_HOURLY = "hourly"; public final static String CHANGEFREQ_DAILY = "daily"; public final static String CHANGEFREQ_WEEKLY = "weekly"; public final static String CHANGEFREQ_MONTHLY = "monthly"; public final static String CHANGEFREQ_YEARLY = "yearly"; public final static String CHANGEFREQ_NEVER = "never";
@Autowired private IContentService contentsService;
public String getBlogSiteMap() { StringBuffer sb = new StringBuffer(); sb.append(BEGIN_DOC); sb.append(new SiteMapVo(WebConst.initConfig.get("site_url"))); List contentList = contentsService.findContentList(); contentList.forEach(entity -> { sb.append(new SiteMapVo(Commons.permalink(entity), DateKit.dateFormat(Commons.fmtdate(entity.getModified(), "yyyy-MM-dd"), "yyyy-MM-dd"), CHANGEFREQ_MONTHLY, "0.9")); }); sb.append(END_DOC); return sb.toString(); }
public String getBzSiteMap() { StringBuffer sb = new StringBuffer(); sb.append(BEGIN_DOC); sb.append(new SiteMapVo(WebConst.initConfig.get("site_url"))); sb.append(END_DOC); return sb.toString(); } }
``` # 步骤三 > Web层添加请求控制器
```java /** * @return : java.lang.String * @author Jesse-liu * @date 2020/5/7 * @description: 动态生成网站地图sitemap **/ @GetMapping(value = {"sitemap.xml", "sitemap"}) public void getSiteMap(HttpServletResponse response) throws IOException { response.setCharacterEncoding("UTF-8"); response.setContentType(MediaType.APPLICATION_XML_VALUE); response.getWriter().append(siteMapUtils.getBlogSiteMap()); } ```
完美收工!!!
# 效果展示页面 [https://ooolo.net/sitemap.xml](https://ooolo.net/sitemap.xml)