基本上,你的请求映射和您的视图的名称是分离的,你只需要注意语法。
例如,对于
@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详细描述了如何映射请求,您应该仔细阅读它。
你试过了吗? –
是的,但没有奏效。我现在在问题中添加了它。 – krmanish007
简短的回答是:你可以,但这将取决于你的弹簧引导应用程序的设置。我在spring-mvc中配置了这样的东西。您可能需要在您的spring-boot应用程序中查看配置视图解析器。给你的问题添加更多细节肯定会有所帮助。 – iaforek