我是spring boot的新手,我已经写了下面的WelcomeController。
@Controller
public class WelcomeController {
@RequestMapping("/home")
public String home() {
return "homePage";
}
}然后将spring.mvc.view.suffix=.html添加到application.properties中。我想只有localhost:8080/home.html url才能呈现homePage.html。但是当我访问localhost:8080/home时,我仍然得到了homePage.html,而不是我认为的404错误。
我怎样才能使只以'.html‘结尾的url合法?为什么spring.mvc.view.suffix=.html配置不起作用?
发布于 2017-06-26 10:17:02
我认为你对财产的用途感到困惑了。基本上,它所做的就是说明视图文件在服务器中的扩展名是什么。在这里阅读一些关于它的信息,https://docs.spring.io/spring-boot/docs/current/reference/html/howto-spring-mvc.html关于掩体74.8
我相信您只是在尝试验证url端点,并确保它以".html“结尾
@Controller
public class WelcomeController {
@RequestMapping("/home")
public String home(HttpServletRequest request) throws Exception {
String uri = request.getRequestURI();
if (!isValidEndpoint(uri)) {
throw new InvalidUrlException();
}
return "homePage";
}
private boolean isValidEndpoint(String url) {
return url.endsWith(".html");
}
}
@ResponseStatus(code=HttpStatus.NOT_FOUND, reason="The url needs to end with '.html'")
class InvalidUrlException extends Exception {
private static final long serialVersionUID = 1L;
public InvalidUrlException() {
super();
}
}https://stackoverflow.com/questions/44749219
复制相似问题