Java 在tapestry 5中显示.txt或.xml文件

Java 在tapestry 5中显示.txt或.xml文件,java,tapestry,Java,Tapestry,我最近开始使用tapestry。我在tapestry应用程序的src/main/resources中有一个名为robots.txt的文本文件 我希望在键入URL时显示该文件:localhost:8080/robots.txt 如何做到这一点?您可以在应用程序的根目录中使用索引页 package foo.bar.mybasepackage.pages; public class Index { public Object onActivate(EventContext context)

我最近开始使用tapestry。我在tapestry应用程序的src/main/resources中有一个名为robots.txt的文本文件

我希望在键入URL时显示该文件:
localhost:8080/robots.txt


如何做到这一点?

您可以在应用程序的根目录中使用索引页

package foo.bar.mybasepackage.pages;

public class Index {
    public Object onActivate(EventContext context) {
        if (context.getCount() == 1
                && "robots.txt".equals(context.get(String.class, 0)) {

           return getRobotsTxt();
        }
        if (context.getCount() != 0) {
           return new HttpError(404, "Not found");
        }
        return this;
    }

    private StreamResponse getRobotsTxt() {
        return new TextStreamResponse(...);
    }
}

谢谢。如我所见,我需要将文本作为字符串作为参数放入TextStreamResponse构造函数。我怎样才能从文件中读取文本?哦,对不起。。。我应该把你的问题读得更清楚。我还以为你在计算robots.txt呢。假设您遵循maven的约定,那么您应该将文件放在
src/main/webapp/robots.txt中,然后忘记整个索引页的事情。如果您想将文件保存在
src/main/resources
中,您可以通过
getClass().getClassLoader().getResourceAsStream(“robots.txt”)
访问它,我将其移动到src/main/webapp。我应该把sitemap.xml文件放在同一个地方,对吗?再次感谢:)