springboot application articles (1) simple integration of web components and configuration using jsp

1. Build a springboot project

   Inherit spring-boot-parent  

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-parent</artifactId>
        <version>1.5.19.RELEASE</version>
    </parent>

 Add web components

         <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

Write application.properties or application.yml file under resources

Configure and write application.yml

server:
  port: 8081  # 配置端口
  context-path: /xiaodu  #配置根路径

Write java code

  Start class

/**
 * springboot 启动类
 * 这个类最好放在最外层包下,其他的java类放到他的包下
 *  启动会扫描本包和子包下的类
 */
@SpringBootApplication
public class Springboot_Web_StartApp_8081 {

    public static void main(String[] args) {
        SpringApplication.run(Springboot_Web_StartApp_8081.class,args);
    }
    
}

Write the controller


/**
 * @RestController 是@Controller ,@ResponseBody一起使用的作用
 */
@RestController
public class MyHelloController {

    /**
     * getMaping   == @RequestMapping(method = {RequestMethod.GET})
     * @return
     */
    @GetMapping("/hello")
    public String hello() {
        return "hello world";
    }

}

It can be accessed by directly running the main method to start; 

Configuration support jsp

     Write application.yml 

spring:
  mvc:
    view:
#    配置支持jsp
      prefix: /WEB-INF/jsp/
      suffix: .jsp

 

Create a webapp directory to store jsp pages

 

And set it as a web resource directory,

Click on your own project configuration, select + sign, add web

 

Add support jar for jsp

  <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>

        <!-- servlet 依赖包 -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>
        <!-- JSTL (JSP standard Tag Library) JSP 标准标签库 -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>

Finally configure the working directory of the main of the startup item

Select edit configruation..

Write the controller for access

  @GetMapping("/hello")
    public String hello() {
        System.out.println("aaaaaaaaaaaaaaaaaaaaaa");
        return "hello";
    }

eclipse configuration method connection: https://mp.csdn.net/postedit/89376652

Guess you like

Origin blog.csdn.net/xiaodujava/article/details/89363706