2017-03-03 168 views
1

我在春季开机使用百里香,并有几个意见。我不想在默认情况下将所有视图保存在src/main/resources/templates相同的文件夹中。views在thymeleaf春季启动模板子文件夹

是否可以移动src/main/resources/templates/folder1中的某些视图,并且我将通过“folder1/视图名称”来访问该页面?

当我试过http://localhost:8080/folder1/layout1它没有在src/main/resources/templates/folder1 /中找到我的html,但是当我在模板主文件夹src/main/resources/templates /中移动html时,http://localhost:8080/layout1工作正常。

我的控制器类的样子:

@RequestMapping(value = "{pagename}", method = RequestMethod.GET) 
public String mf42_layout1(@PathVariable String pagename) { 
    return pagename; 
} 

所以,我想,如果我通过布局1,它看起来INT的模板,如果我说“A /布局1”,它会看起来/布局文件夹

感谢, 马尼什

+0

你试过了吗? –

+0

是的,但没有奏效。我现在在问题中添加了它。 – krmanish007

+0

简短的回答是:你可以,但这将取决于你的弹簧引导应用程序的设置。我在spring-mvc中配置了这样的东西。您可能需要在您的spring-boot应用程序中查看配置视图解析器。给你的问题添加更多细节肯定会有所帮助。 – iaforek

回答

1

基本上,你的请求映射和您的视图的名称是分离的,你只需要注意语法。

例如,对于

@RequestMapping(value = "/foobar", method = RequestMethod.GET) 
public String mf42_layout1() { 
    return "layout1"; 
} 

请求http://localhost:8080/layout1将使位于src/main/resources/templates/pagename.html的模板。

它也可以,如果你把你的模板上的子文件夹,只要你提供了正确的路径图:

@RequestMapping(value = "/foobar", method = RequestMethod.GET) 
public String mf42_layout1() { 
    return "a/layout1"; 
} 

的请求http://localhost:8080/foobar将使位于src/main/resources/templates/a/layout1.html模板。

你也可以参数化与@PathVariable的URL端点:

@RequestMapping(value = "/foobar/{layout}", method = RequestMethod.GET) 
    public String mf42_layout1(@PathVariable(value = "layout") String layout) { // I prefer binding to the variable name explicitely 
     return "a/" + layout; 
    } 

我们http://localhost:8080/foobar/layout1请求将呈现src/main/resources/templates/a/layout1.html模板和http://localhost:8080/foobar/layout2请求将呈现什么在src/main/resources/templates/a/layout2.html

但要注意的正斜杠作为网址中的分隔符,因此您的控制器:

@RequestMapping(value = "{pagename}", method = RequestMethod.GET) 
public String mf42_layout1(@PathVariable String pagename) { 
    return pagename; 
} 

我的猜测是当你点击http://localhost:8080/a/layout1 pagename收到“a”并且“layout1”没有被捕获。所以控制器可能会尝试渲染内容src/main/resources/templates/a.html

Spring MVC reference详细描述了如何映射请求,您应该仔细阅读它。