java 使用 Spring MVC 提供 sitemap.xml 和 robots.txt

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12289232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 08:19:01  来源:igfitidea点击:

Serving sitemap.xml and robots.txt with Spring MVC

javaspringspring-mvcsitemaprobots.txt

提问by michal.kreuzman

What is the best way to server sitemap.xmland robots.txtwith Spring MVC? I want server these files through Controllerin cleanest way.

服务器sitemap.xmlrobots.txt使用的最佳方式是什么Spring MVC?我希望Controller以最干净的方式处理这些文件。

回答by limc

I'm relying on JAXB to generate the sitemap.xml for me.

我依靠 JAXB 为我生成 sitemap.xml。

My controller looks something like the below, and I have some database tables to keep track of the links that I want to appear in the sitemap:-

我的控制器看起来像下面这样,我有一些数据库表来跟踪我想要出现在站点地图中的链接:-

SitemapController.java

站点地图控制器.java

@Controller
public class SitemapController {

    @RequestMapping(value = "/sitemap.xml", method = RequestMethod.GET)
    @ResponseBody
    public XmlUrlSet main() {
        XmlUrlSet xmlUrlSet = new XmlUrlSet();
        create(xmlUrlSet, "", XmlUrl.Priority.HIGH);
        create(xmlUrlSet, "/link-1", XmlUrl.Priority.HIGH);
        create(xmlUrlSet, "/link-2", XmlUrl.Priority.MEDIUM);

        // for loop to generate all the links by querying against database
        ...

        return xmlUrlSet;
    }

    private void create(XmlUrlSet xmlUrlSet, String link, XmlUrl.Priority priority) {
        xmlUrlSet.addUrl(new XmlUrl("http://www.mysite.com" + link, priority));
    }

}

XmlUrl.java

XmlUrl.java

@XmlAccessorType(value = XmlAccessType.NONE)
@XmlRootElement(name = "url")
public class XmlUrl {
    public enum Priority {
        HIGH("1.0"), MEDIUM("0.5");

        private String value;

        Priority(String value) {
            this.value = value;
        }

        public String getValue() {
            return value;
        }
    }

    @XmlElement
    private String loc;

    @XmlElement
    private String lastmod = new DateTime().toString(DateTimeFormat.forPattern("yyyy-MM-dd"));

    @XmlElement
    private String changefreq = "daily";

    @XmlElement
    private String priority;

    public XmlUrl() {
    }

    public XmlUrl(String loc, Priority priority) {
        this.loc = loc;
        this.priority = priority.getValue();
    }

    public String getLoc() {
        return loc;
    }

    public String getPriority() {
        return priority;
    }

    public String getChangefreq() {
        return changefreq;
    }

    public String getLastmod() {
        return lastmod;
    }
}

XmlUrlSet.java

XmlUrlSet.java

@XmlAccessorType(value = XmlAccessType.NONE)
@XmlRootElement(name = "urlset", namespace = "http://www.sitemaps.org/schemas/sitemap/0.9")
public class XmlUrlSet {

    @XmlElements({@XmlElement(name = "url", type = XmlUrl.class)})
    private Collection<XmlUrl> xmlUrls = new ArrayList<XmlUrl>();

    public void addUrl(XmlUrl xmlUrl) {
        xmlUrls.add(xmlUrl);
    }

    public Collection<XmlUrl> getXmlUrls() {
        return xmlUrls;
    }
}

For the robots.txt, it looks something like the below, and obviously, you will need to configure it based on your likings:-

对于robots.txt,它看起来像下面这样,显然,您需要根据自己的喜好配置它:-

RobotsController.java

机器人控制器.java

@Controller
public class RobotsController {

    @RequestMapping(value = "/robots.txt", method = RequestMethod.GET)
    public String getRobots(HttpServletRequest request) {
        return (Arrays.asList("mysite.com", "www.mysite.com").contains(request.getServerName())) ?
                "robotsAllowed" : "robotsDisallowed";
    }
}

回答by v0ld3m0rt

Add this line to your dispatcher servlet xml file:

将此行添加到您的调度程序 servlet xml 文件中:

<mvc:resources mapping="/robots.txt" location="/WEB-INF/robots.txt" order="0"/> 

Put the robots.txt at WEB-INF/robots.txt. The file will be accessible by yoursite.com/robots.txt

将 robots.txt 放在 WEB-INF/robots.txt 中。该文件可通过 yoursite.com/robots.txt 访问