[英]Spring MVC - run application in absolute path
I have a demo application of Spring MVC - I just follow udemy course for it.我有一个 Spring MVC 的演示应用程序 - 我只是按照 udemy 课程学习。
I have created the first controller and view.我已经创建了第一个控制器和视图。 All is working fine, however I have one doubt about.一切正常,但我有一个疑问。 The pom file of application contains:应用程序的 pom 文件包含:
<groupId>eu.smartgroup</groupId>
<artifactId>spring-demo-mvc</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
So when I run application on tomcat server it starts, but it is available with url: http://localhost:8080/spring_demo_mvc_war因此,当我在 tomcat 服务器上运行应用程序时,它会启动,但它可以通过 url 使用:http://localhost:8080/spring_demo_mvc_war
Is there possibility to configure application so it will be available in the root path: http://localhost:8080/ (without project name after slash)?是否有可能配置应用程序,使其在根路径中可用:http://localhost:8080/(斜杠后没有项目名称)?
Edit: Here is full application.yml编辑:这是完整的 application.yml
server:
servlet:
contextPath: /
HelloController.java你好控制器.java
package eu.test.springdemo.mvc;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
@Controller
@RequestMapping("/")
public class HelloController {
@GetMapping("/")
public String showPage() {
return "main-menu";
}
}
DemoAppConfig.java DemoAppConfig.java
package eu.test.springdemo.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages="eu.test.springdemo")
public class DemoAppConfig implements WebMvcConfigurer {
// define a bean for ViewResolver
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/view/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
}
Add this line to your application.properties
:将此行添加到您的application.properties
:
server.servlet.contextPath=/
I've tried it on my project and it works.我已经在我的项目中尝试过它并且有效。
Write back is it working on your.回信它是否对您有效。
Check if you have a controller class that is annotated as such:检查您是否有这样注释的控制器类:
@Controller
@RequestMapping("spring_demo_mvc_war")
or或者
@RestController
@RequestMapping("spring_demo_mvc_war")
If that is the case, then make the string empty or remove the @RequestMapping
(you can also change it to whatever else you like).如果是这种情况,则将字符串设为空或删除@RequestMapping
(您也可以将其更改为您喜欢的任何其他内容)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.