前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Springboot动态生成网站地图sitemap教程

Springboot动态生成网站地图sitemap教程

作者头像
用户9131103
发布2023-07-17 18:48:07
6870
发布2023-07-17 18:48:07
举报
文章被收录于专栏:工作经验

发现自从换了一套博客程序后,之前搜索引擎抓取的链接都失效了,所以才有此想法采用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)

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020年05月07日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档