Home > Java > javaTutorial > body text

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

WBOY
Release: 2023-05-15 23:58:04
forward
1772 people have browsed it

    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>
    Copy after login

    action interception acceptance method

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

    Based on the original project, add A configuration

    server:  port: 8080  servlet:    context-path: /demo
    Copy after login

    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!

    source:yisu.com
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template