1. 程式人生 > >Intellij IDEA 學習Spring Boot (1)

Intellij IDEA 學習Spring Boot (1)

      工作之餘,學習一下,使用Intellij IDEA 新建一個 Spring Boot專案,算是對 Spring Boot 入一個門

      新建專案

    

   完成之後,效果如下:

  

   直接執行:

   

  (這裡我沒有關注Maven,是因為我就預設使用了Intellj IDEA 自帶的Maven工具了,不想配了,大不了下載的東西 會放在C盤使用者目錄下一個叫.m2的資料夾。我們用 spring boot 就是為了 簡化各種配置的。你看上面,我們只需要 寫寫 專案應用的名字,然後就OK了。不要有強迫症,你看,這個spring boot,我即沒有關注maven,也沒有關注tomcat,為什麼? 這些根本就不是我們操心的,人家都整合好了。)


  瀏覽器執行:

  

這個404很明白的啦,沒有頁面。我們細看一下


  接下來,我們寫點東西。

  先不考慮頁面。先寫個Controller。

@Controller
public class HelloController {
    @RequestMapping("/")
    @ResponseBody
public String index(){
        return "Hello World!";
}
}
這裡先不返回view,返回資料。重新執行。

   


  接下來考慮 返回view的情況。 

  大多數 thymeleaf 模板引擎來替換掉 jsp。我們就當學習一下潮流吧。這東西其實也不難。就是不熟練而已。

  接下來,我們重新做一些有意義的事情。

  在HelloController中新增一個 requestMapping

@RequestMapping("/index.html")
public String toIndex(){
    return "index";
}

  那麼接下來return "index" 就是 index.html放在哪呢。注意不是index.jsp。就是index.html。


重新執行。

 


那麼這裡有些要注意了。不能出現單標籤的了,因為thymeleaf引擎比較嚴格。以後<input>這種肯定不行了,必須類似這樣<input></input>或者<input /> 所以我們修改一下


但是你發現重新整理並沒有效果。必須重新執行。


執行成功。但這就不爽勒,為什麼不能像以前tomcat那樣,改了重新整理就可以看到效果呢。那麼這種類似 熱部署操作是需要配置的。參考 http://www.cnblogs.com/bingshu/p/6876030.html。

1. 修改pom檔案

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-devtools</artifactId>
   <optional>true</optional>
   <scope>true</scope>
</dependency>
<build>
   <plugins>
      <plugin>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-maven-plugin</artifactId>
         <configuration>
            <fork>true</fork>
         </configuration>
      </plugin>
   </plugins>
</build>
2. 修改idea。

1. 快捷鍵: Ctrl+Alt+S

  

  OK完之後,

 2. 快捷鍵:Ctrl+Shift+Alt+ /



OK之後,再重啟專案試試。 你可以發現,你修改java程式碼,專案會重新部署,修改html檔案,重新整理瀏覽器,效果也會修改的。完美實現 “熱部署”

這不算什麼。我們還沒有用上 那個新模板引擎的語法呢。 先搞點簡單的,複雜的 各位還是請百度。

public class Student {
    private String name;
    private Integer age;    public String getName() {
        return name;
}
    public void setName(String name) {
        this.name = name;
}
    public Integer getAge() {
        return age;
}
    public void setAge(Integer age) {
        this.age = age;
}
}
新建了一個類Student。然後controller中加入這樣的程式碼。
@RequestMapping("/students.html")
public String students(Map<String,Object> map){
    List<Student> list = new ArrayList<>();
    for(int i=0;i<10;i++){
        Student student = new Student();
student.setName("張三"+i);
student.setAge(23+i);
list.add(student);
}
    map.put("sList",list);// 返回給頁面的資料
return "students";
}
下面編寫 students.html頁面
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8" />
    <title>Title</title>
</head>
<body>
所有學生
<ul th:each="stu,stuSta:${sList}">
       <li>
序號:<span th:text="${stuSta.index}"/><br/>
姓名:<th:block th:text="${stu.name}"></th:block><br/>
年齡:<div th:text="${stu.age}"></div><br/>
       </li>
   </ul>
</body>
</html>


不要糾結著寫紅線了,不是錯誤。這是 這個整合工具的問題,不要糾結,沒有錯。修復也行,alt+enter,但那句程式碼都是註釋狀態,毫無意義,還多上了java類,


真的,這樣我就覺得 毫無意義了,明明一個純html檔案裡,沒必要出現這些類名的吧。

所以,不要糾結了,看著看著就習慣了。

瀏覽器訪問


接下來,我們討論一個常用功能那就是 日誌功能。

 spring boot 日誌已經集成了,什麼log4j,slf4j,logback什麼的,所以不需要導什麼包,直接用。

like this.

Logger logger = LoggerFactory.getLogger(getClass());
@RequestMapping("/students.html")
public String students(Map<String,Object> map){
    List<Student> list = new ArrayList<>();
    for(int i=0;i<11;i++){
        Student student = new Student();
student.setName("張三"+i);
student.setAge(23+i);
list.add(student);
}
    map.put("sList",list);// 返回給頁面的資料
logger.info("studentList: {}",list);
    return "students";
}

那日誌級別什麼的怎麼調整呢。 這裡 我們 做一個配置,當然不需要新建 log4j.properties。沒有用。


我們寫點東西,這個 是帶提示的哦。

我們簡單的寫點東西

#log
logging.file=mylog.log
logging.path=logs
logging.level.root=info#thymeleaf
spring.thymeleaf.cache=false

再寫一個簡單的例子

@RequestMapping("/student/{name}.html")
public String student(@PathVariable(name="name") String name, Map<String,Object> map){
    Student student = new Student();
student.setName(name);
student.setAge(23);
map.put("student",student);
    return "student";
}
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8" />
    <title><th:block th:text="${student.name}"/>的資訊</title>
</head>
<body>
該學生資訊
    姓名:<span th:text="${student.name}"></span>
年齡:<span th:text="${student.age}"></span>
</body>
</html>

 好了,這個入門就到這了。下次繼續聊 spring boot。