zoukankan      html  css  js  c++  java
  • [读书笔记] 三、搭建基于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#对ListView控件的几个操作技巧
    C#用代码创建控件,以及它的鼠标事件
    C#使用ListView控件对数据进行频繁更新时,出现闪烁问题的解决办法
    C#判断某个键值是否存在于字典中
    FreeMASTER 2.0的安装与使用
    C和C++中获取二维数组的行列数
    Python中类的定义和使用
    Python创建字典和添加键值
    C#用鼠标滚轮控制控件大小,实现滚轮缩放效果
    C#中对Excel文件执行写数据操作
  • 原文地址:https://www.cnblogs.com/javapath/p/6830622.html
Copyright © 2011-2022 走看看