如今開始springboot-web開發教程。html
引入依賴,pom.xmljava
<?xml version="1.0" encoding="UTF-8"?> <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>cn.ac.iie</groupId> <artifactId>spring-course</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.1.4.RELEASE</version> <scope>import</scope> <type>pom</type> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
spring-boot-starter-web已經包含了spring-boot-starter依賴,所以只需引入這個依賴就能夠了。web
新建UserController.javaspring
package com.edu.spring.springboot; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class UserController { @RequestMapping(value = "/user/home") @ResponseBody public String home() { return "user home"; } }
新建App.javaapache
package com.edu.spring.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
運行App.java,則服務器正常運行,默認端口號是8080,經過瀏覽器訪問http://localhost:8080/user/home正常。api
這樣最簡單的web開發就完成了。瀏覽器
若是要修改端口,能夠再application.properties中修改:tomcat
server.port=8081
這樣端口號就修改爲功了。springboot
默認的請求方式是:GET,POST,PUT方式都支持。咱們能夠限制他的請求方式:服務器
@RequestMapping(value = "/user/home", method = RequestMethod.GET) @ResponseBody public String home() { return "user home"; }
使用GetMapping
@GetMapping("/user/show") @ResponseBody public String show() { return "user home"; } @PostMapping("/user/create") @ResponseBody public String create() { return "user home"; }
GetMapping PostMapping等是spring4.3的新特性
修改UserController.java
@PostMapping("/user/create") @ResponseBody public String create(@RequestParam("username") String username, @RequestParam("password") String password) { return "user create, username: " + username + ", password: " + password; }
@RequestParam註解默認是參數必須提供,若是能夠不提供能夠使用required=false
能夠提供一個默認值defaultValue=""
@GetMapping("/user/{id}") @ResponseBody public String show(@PathVariable("id") String id) { return "user home id: " + id; }
注入Servlet的api
@GetMapping("/user/edit") @ResponseBody public String edit(HttpServletRequest httpServletRequest){ return "user edit: " + httpServletRequest.getRemoteHost(); }
咱們發現每一個方法都必須使用@RequestBody來註釋。所以能夠使用RestController來簡化
新建RoleController.java
package com.edu.spring.springboot; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class RoleController { @GetMapping("/role/show") public String show(){ return "role show "; } }
@RestController 代表了當前controller的方法的返回值能夠直接用body輸出。
新建LoginController.java
package com.edu.spring.springboot; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; @Controller public class LoginController { @PostMapping("/login") public String login(@RequestParam("username") String username, @RequestParam(value = "password") String password) { if (username.equals(password)) { return "ok"; } return "fail"; } }
在main文件夾下面新建webapp,與java和resources文件夾並列。
修改application.properties
spring.mvc.view.prefix=/WEB-INF/jsp/ spring.mvc.view.suffix=.jsp
在webapp目錄下新建文件夾/WEB-INF/jsp,而後新建ok.jsp和fail.jsp
springboot默認是不支持使用jsp的
在springboot中使用jsp,須要引入依賴:
<dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency>
這樣就能夠成功訪問jsp了。
@GetMapping("/loginIndex") public String loginIndex(Model model) { model.addAttribute("username", "root"); model.addAttribute("password", "123456"); return "login"; }
新建login.jsp
<html> <h1>username; ${username}</h1> <h2>password: ${password}</h2> </html>
在springboot中使用jsp時,不能使用@RestController, 而要使用@Controller
添加pom.xml依賴:而且刪除jsp的依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>
在application.properties中刪除jsp的配置。
在springboot中使用freemarker的步驟:
1. 在pom中加入依賴,
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>2. 默認的freemaker的模板文件在classpath:/template/, 默認的文件擴展名爲:ftl
新建AccountController.java
package com.edu.spring.springboot; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; @Controller public class AccountController { @GetMapping("/reg") public String reg(){ return "reg"; } }
在resources/template下新建reg.ftl
<h1> ftl reg </h1>
能夠經過訪問 http://192.168.170.132:8081/reg來獲取這個模板頁面了
在application.properties中修改:
spring.freemarker.template-loader-path=classpath:/ftl/ 多個用逗號隔開
在resources下新建ftl文件夾,而後將reg.ftl文件移動到這個路徑下,就能夠訪問了。
在AccountController.java
@GetMapping("/logout") public String logout(Model model){ model.addAttribute("username", "admin"); model.addAttribute("logout", "true"); return "logout"; }
在ftl目錄下新建logout.ftl文件:
<h1>logout</h1> <h2>username: ${username}</h2> <h2>logout is ${logout}</h2>
這樣就傳遞參數到模板中了。
最好在項目中要麼選擇模板,要麼選擇jsp,不要兩者都選。
首先須要把tomcat排除掉。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency>
導入jetty依賴。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency>
其他都不須要改變,直接運行,輸出:
2019-05-15 21:00:56.619 INFO 14692 --- [ main] o.e.jetty.server.handler.ContextHandler : Started o.s.b.w.e.j.JettyEmbeddedWebAppContext@37d3d232{application,/,[org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory$LoaderHidingResource@30c0ccff],AVAILABLE} 2019-05-15 21:00:56.619 INFO 14692 --- [ main] org.eclipse.jetty.server.Server : Started @2652ms 2019-05-15 21:00:56.776 INFO 14692 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 2019-05-15 21:00:57.069 INFO 14692 --- [ main] o.e.j.s.h.ContextHandler.application : Initializing Spring DispatcherServlet 'dispatcherServlet' 2019-05-15 21:00:57.070 INFO 14692 --- [ main] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet' 2019-05-15 21:00:57.075 INFO 14692 --- [ main] o.s.web.servlet.DispatcherServlet : Completed initialization in 5 ms 2019-05-15 21:00:57.194 INFO 14692 --- [ main] o.e.jetty.server.AbstractConnector : Started ServerConnector@614aeccc{HTTP/1.1,[http/1.1]}{0.0.0.0:8081} 2019-05-15 21:00:57.196 INFO 14692 --- [ main] o.s.b.web.embedded.jetty.JettyWebServer : Jetty started on port(s) 8081 (http/1.1) with context path '/' 2019-05-15 21:00:57.198 INFO 14692 --- [ main] com.edu.spring.springboot.App : Started App in 2.8 seconds (JVM running for 3.23)
說明容器已經變成jetty了。
默認是不須要有項目名稱的,在application.properties文件中修改:
server.servlet.context-path=/mall
在地址欄中,須要指定/mall才能訪問。例如:http://192.168.170.132:8081/mall/logout