Home  >  Article  >  Java  >  How to add server.servlet.context-path in Springboot

How to add server.servlet.context-path in Springboot

WBOY
WBOYforward
2023-05-15 23:58:041697browse

    Springboot adds server.servlet.context-path

    The role of server.servlet.context-path configuration

    Definition: server. servlet.context-path= # Context path of the application. The context path of the application, which can also be called the project path, is part of the url address.

    • When server.servlet.context-path is not configured, the default is /, such as: localhost:8080/xxxxxx

    • When server.servlet When .context-path is configured, such as /demo, the access method at this time is localhost:8080/demo/xxxxxx

    Configuration differences after springboot 2.0 changes

    1. Before springboot 2.0, it was configured as server.context-path

    2. After springboot 2.0, it was configured as server.servlet.context-path

    A thought

    Original Operational project (already online), after adding the server.servlet.context-path configuration to the configuration file, do I need to add the action request in thymleaf?

    Answer: No need.

    Chestnut:

    The front-end page adopts form request

    <form th:action="@{/user/userLogin}" method="post" id="userLogin"></form>

    action interception acceptance method

    @Controller
    @RequestMapping("/user")
    public class LoginController {
     
    @PostMapping("/userLogin")
    public String userLogin(HttpServletRequest request, Model model) {

    Based on the original project, add A configuration

    server:  port: 8080  servlet:    context-path: /demo

    only needs to add localhost:8080/demo when entering the homepage again. There is no need to add /demo for subsequent hrefs and actions in thymleaf.

    Springboot configures server.servlet-path and server.context-path

    server.context-path

    Set the application context-path.

    The context-path is different depending on the springboot version:

    • If it is below springboot2.0, configure server.context-path=/demo

    • If it is springboot2.0 or above, configure server.servlet.context-path=/demo

    server.servlet-path

    • Set the monitoring path of dispatcher servlet, the default is: /

    Example :

    • If configured server.servlet-path = /demo, the access path is http://ip:port/demo/...

    • No configuration or server.servlet-path = /, the access path It's http://ip:port/...

    Small extension:

    1. Only server.servlet-path = is configured /demo, the access path is http://ip:port/demo/...

    2. If the idea used is only the Application context configured in Tomcat, the access path is http://ip: port/test/...

    3. If you use the idea, the Application context has been configured in Tomcat (picture below), server.servlet-path = /demo is also configured, and the access path is http: //ip:port/test/demo/...

    How to add server.servlet.context-path in Springboot

    The above is the detailed content of How to add server.servlet.context-path in Springboot. For more information, please follow other related articles on the PHP Chinese website!

    Statement:
    This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete