[读书笔记] 三、搭建基于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>

五、目录结构

 

posted on 2017-05-09 15:02  懂技术爱生活  阅读(1570)  评论(0编辑  收藏  举报

导航