我有用Java/Spring MVC编写的测试代码.我想运行它,并期望我的GET请求如下所示:

localhost:9090/hello 

但我明白了

localhost:9090/spring-mvc-app1/hello

我把这个项目命名为spring-mvc-app1,但我不明白为什么这个名字会出现在逻辑上不应该出现的地方.开发环境--Eclipse.

The code itself:

package john.project.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class ControllerOrder {
    @GetMapping("/hello")
    public String helloPage() {
        return "order/hello";
    }
    
    @GetMapping("/goodbye")
    public String goodbyePage() {
        return "order/goodbye";
    }
}

Config: SpringConfig.java:

package john.project.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.thymeleaf.spring6.SpringTemplateEngine;
import org.thymeleaf.spring6.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring6.view.ThymeleafViewResolver;


@Configuration
@ComponentScan("john.project")
@EnableWebMvc
public class SpringConfig implements WebMvcConfigurer {

    private final ApplicationContext applicationContext;

    @Autowired
    public SpringConfig(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    @Bean
    public SpringResourceTemplateResolver templateResolver() {
        SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
        templateResolver.setApplicationContext(applicationContext);
        templateResolver.setPrefix("/WEB-INF/views/");
        templateResolver.setSuffix(".html");
        return templateResolver;
    }

    @Bean
    public SpringTemplateEngine templateEngine() {
        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.setTemplateResolver(templateResolver());
        templateEngine.setEnableSpringELCompiler(true);
        return templateEngine;
    }

    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        ThymeleafViewResolver resolver = new ThymeleafViewResolver();
        resolver.setTemplateEngine(templateEngine());
        registry.viewResolver(resolver);
    }
}

SpringMvcDispatcherServletInitializer.java:

package john.project.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class SpringMvcDispatcherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return null;
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{SpringConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }
}

推荐答案

从逻辑上讲,Web服务器上可能有几个应用程序,并且在部署时,每个应用程序缺省都将一个项目名称作为Web应用程序上下文.您可以在Eclipse的项目属性中对其进行配置.

当您将Web应用程序部署到Tomcat时,它将在处理web.xml之后try 创建在部署期间指定的应用程序上下文.如果Web应用程序中有错误,它可能不会启动,也不会创建上下文.您需要解决这些错误并重新部署您的应用程序.您可能有错误的配置,使用了过时的API,错误的库版本和不一致的依赖关系,以及其他服务器问题.很难告诉您发生了什么,以及为什么您的应用程序没有启动.但是,提供完整的堆栈跟踪、项目配置可能有助于进一步解决问题.下面是您的项目代码中存在的另一个问题的解决方案,这些问题可能不存在,或者已经解决,但没有显示在您发布的代码中.这有助于在向Web应用注册该上下文之后在上下文/spring-mvc-app1处的浏览器中启动Webapply.


如果你想更改Web应用程序上下文路径,那么你可以阅读这篇文章: How to change Web Application Context in Eclipse

如果您在Eclipse中运行或调试Web项目,则项目名称将是默认的上下文根.例如,名为"springmvc"的项目,默认上下文根为

http://localhost:8080/springmvc

请注意, 上下文根告诉您已部署的Web应用程序的URL.

http://localhost:8080/{context_root}

如何在Eclipse IDE中更新Web项目上下文根.

  1. Eclipse-Web项目设置
  • 右击项目, Select 属性,Web项目设置,在此处更新上下文根.
  • 从服务器上删除您的Web应用程序,然后将其添加回服务器.应更新上下文根.
  • 如果步骤2失败,请删除服务器,创建新服务器,然后重新添加Web应用程序.

新上下文根:

http://localhost:8080/abc
  1. Eclipse-Web模块
  • 双击Eclipse服务器插件.
  • 单击模块选项卡.
  • 更新路径.
  • 好了.重新启动服务器.

新上下文根:

http://localhost:8080/abc

Java相关问答推荐

Selenium Java:无法访问IFRAME内部的元素

如果给定层次 struct 级别,如何从其预序穿越构造n元树

Mat. n_Delete()和Mat. n_release的区别

inteliJ中是否有一个功能可以自动在块注释中的/*后面添加一个空格?''

无法传递消费者<;>;实例

对某一Hyroby控制器禁用@cacheable

是否在允许数组元素为空时阻止 idea 为空性警告?

测试期间未执行开放重写方法

Jenv-相同的Java版本,但带有前缀

如何在Microronaut中将 map 读取为 map

在Frege中,我如何将一个字符串安全地转换为一个可能的Int?

FETCH类型设置为LAZY,但它仍会发送第二个请求

Java 21中泛型的不兼容更改

具有多个模式的DateTimeForMatter的LocalDate.parse失败

本机方法(JNI)总是编译的吗?

如何在Java中的重写方法参数中强制(Enum)接口实现?

使用@ExceptionHandler的GlobalExceptionHandler还是来自服务器的REST应答的ResponseEntity?

URI构造函数错误?

关于正则表达式的一个特定问题,该问题与固定宽度负向后看有关

Spring Boot应用程序中的自定义constraintvalidator不会被调用