首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >spring.mvc.view.suffix不工作

spring.mvc.view.suffix不工作
EN

Stack Overflow用户
提问于 2017-06-26 02:14:52
回答 1查看 860关注 0票数 0

我是spring boot的新手,我已经写了下面的WelcomeController。

代码语言:javascript
复制
@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配置不起作用?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-06-26 10:17:02

我认为你对财产的用途感到困惑了。基本上,它所做的就是说明视图文件在服务器中的扩展名是什么。在这里阅读一些关于它的信息,https://docs.spring.io/spring-boot/docs/current/reference/html/howto-spring-mvc.html关于掩体74.8

我相信您只是在尝试验证url端点,并确保它以".html“结尾

代码语言:javascript
复制
@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();
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44749219

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档