• [读书笔记] 三、搭建基于Spring boot的JavaWeb项目


    一、POM

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.springboot</groupId>
        <artifactId>springboot_test1_1</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <name>springboot_test1_1</name>
        <url>http://maven.apache.org</url>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.2.RELEASE</version>
            <relativePath />
        </parent>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.apache.tomcat.embed</groupId>
                <artifactId>tomcat-embed-jasper</artifactId>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    </project>
    pom.xml

    二、Java配置类

    package core;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
    import org.springframework.context.annotation.Bean;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.servlet.view.InternalResourceViewResolver;
    
    @RestController
    // @SpringBootApplication 是Spring boot 项目的核心注解
    // 它是一个组合注解,主要组合了@Configuration,@EnableAutoConfiguration,@ComponentScan
    // 若不使用@SpringBootApplication,直接使用@Configuration,@EnableAutoConfiguration,@ComponentScan也可
    // tips1:
    // @EnableAutoConfiguration : 让Spring boot根据类路径中的jar包依赖为当前项目进行自动配置
    // tips2:
    // Spring
    // boot会自动扫描@SpringBootApplication注解所在类的同级与下级包里的Bean,故一般让@SpringBootApplication注解所在类放到顶级目录
    @SpringBootApplication
    public class App {
        // main 方法作为启动入口
        public static void main(String[] args) {
            SpringApplication.run(App.class, args);
        }
        // 通过Java配置视图解析器
        @Bean
        @ConditionalOnMissingBean(InternalResourceViewResolver.class)
        public InternalResourceViewResolver defaultViewResolver() {
            InternalResourceViewResolver resolver = new InternalResourceViewResolver();
            resolver.setPrefix("/pages/");
            resolver.setSuffix(".jsp");
            return resolver;
        }
        
    }
    App.java

    三、Controller

    package core;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    
    @Controller
    public class TestController {
        @RequestMapping(value = "/index",method = RequestMethod.GET)
        public String toIndex(Model model){
            model.addAttribute("user", "tester");
            return "index";
        }
    }
    TestController.java

    四、JSP

    <html>
    <body>
    <h2>Hello ${user} this is index.jsp</h2>
    </body>
    </html>

    五、目录结构

  • 相关阅读:
    C# 创建Excel并写入内容
    c#中使用excel
    C#中EXCEL表格的内容进度条实现
    WinForm c#操作Excel
    如何使用 Visual C# .NET 处理 Excel 事件
    C#与Excel的交互示例
    c#操作excel方式三:使用Microsoft.Office.Interop.Excel.dll读取Excel文件
    C#在excel中添加超链接
    ASP.NET学习笔记(3)
    ASP.NET学习笔记(4)
  • 原文地址:https://www.cnblogs.com/javapath/p/6830622.html
Copyright © 2020-2023  润新知