牛網(wǎng)網(wǎng)站建設互聯(lián)網(wǎng)營銷推廣怎么做
繼續(xù)上文
01.數(shù)據(jù)的部分已經完成了,此時需要考慮到前端與用戶交互的部分,這里就需要網(wǎng)頁html,服務器響應servlet。
網(wǎng)頁的部分,html文件放在resource目錄下的template文件中,servlet變成了controller包,其中的controller類功能是與servlet一致的。
controller類命名方式是xxxxcontroller
結構如下:
@Controller 這個表示 類實例化對象后交給spring容器管理
@RequestMapping(“/某個共有的路徑/”)
class xxxxcontroller{
@Autowired
private xxxxService xxxxxService; 這里是service接口,上面已經測試過了,功能可以使用
@RequestMapping(“路徑最后的部分”)
public String 方法名 (Model model) {
// model類似于request對象,可以用來保存后端的數(shù)據(jù),再傳遞到前端去,前端html那里使用thymeleaf技術,來接受后端的數(shù)據(jù)
List<Activity> list = activityService.findObjects();model.addAttribute("activity", list)
return "html的頁面名“;
//(此處因為在application.properties上面thymeleaf中有寫到前后綴的問題,所以只需要寫出html文件的名字就可以了)
}
}
03.寫html文件,需要使用到官網(wǎng)https://www.thymeleaf.org/,來查閱相關的thymeleaf的html功能代碼
The Activity Page
<table><thead><tr><th>表的列名1</th><th>表的列名2</th><th>表的列名3</th><th>表的列名4</th><th>表的列名5</th><th>表的列名6</th><th>表的列名7</th></tr></thead><tbody><tr th:each="變量名:${model對象的key(這里存儲的是一個list),在controller類上存儲的}"><td th:text="${變量名.屬性1(等于表的列名1)}"></td><td th:text="${變量名.屬性2(等于表的列名2)}"></td><td th:text="${變量名.屬性3(等于表的列名3)}"></td><td th:text="${變量名.屬性4(等于表的列名4)}"></td><td th:text="${變量名.屬性5(等于表的列名5)}"></td><td th:text="${變量名.屬性6(等于表的列名6)}"></td></tr></tbody></table>