使用 Spring Boot 快速构建 Java Web 应用
    		       		warning:
    		            这篇文章距离上次修改已过428天,其中的内容可能已经有所变动。
    		        
        		                
                
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 
@SpringBootApplication // 标注这是一个Spring Boot应用
public class HelloWorldApplication {
 
    // 主程序入口
    public static void main(String[] args) {
        SpringApplication.run(HelloWorldApplication.class, args);
    }
 
    // 添加一个Bean,配置一个控制器(Controller)
    @Bean
    public WebMvcConfigurer helloController() {
        return new WebMvcConfigurer() {
            @Override
            public void addViewControllers(ViewControllerRegistry registry) {
                registry.addViewController("/hello").setViewName("hello"); // 映射URL到视图
            }
        };
    }
}这段代码创建了一个简单的Spring Boot应用,定义了一个控制器,将"/hello"这个URL映射到名为"hello"的视图。这是一个入门级的Spring Boot应用,展示了如何快速启动一个Web项目。
评论已关闭