新建项目
spring initialezr,
勾选 web,等,基本的,不勾选sql,
在运行配置信息里,选择%MODULE_WORKING_DIR%
添加dependency
<dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency>
<build> <resources> <resource> <directory>src/main/resources</directory> <filtering>false</filtering> </resource> <resource> <directory>src/main/webapp</directory> <!-- 注意打包后必须要放在此目录下才能被访问到 --> <targetPath>META-INF/resources</targetPath> <includes> <include>**/**</include> </includes> </resource> </resources> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <!--版本号,必须加,否则打包没有jsp页面--> <version>1.4.2.RELEASE</version> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build>
在main上,右键文件夹 webapp,然后建立jsp文件,index.jsp
新建application.yml 文件
spring:
mvc:
view:
prefix: /
suffix: .jsp
新建controller文件夹,然后建立IndexController.java 文件
package com.example.demo.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class IndexController { // @ResponseBody @RequestMapping("/") public String get(){ return "index"; } }
即可访问http://localhost:8080/ 即可访问了。
有时会碰到 没有maven的情况,这时,需要按 Ctrl+shift+ t ,然后再在view里,找到maven,就可以maven全周期了。