spring boot開發問題彙總

1.在添加入html以後靜態文件如css,js,img等無法加載:
a.在application.properties文件中加入一下(程序掃描靜態文件路徑,指示thymeleaf所執行的html標準,靜態文件必須放在static下)
#視圖層控制
spring.mvc.view.prefix=classpath:/templates/    //該處爲掃描html文件夾
spring.mvc.view.suffix=.html
spring.mvc.static-path-pattern=/static/**
spring.thymeleaf.mode=LEGACYHTML5
b.在springboor-web.xml中添加thymeleaf依賴
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>  
     <!--thymeleaf的依賴中不能添加版本-->

        <dependency>
            <groupId>net.sourceforge.nekohtml</groupId>
            <artifactId>nekohtml</artifactId>
            <version>1.9.22</version>
        </dependency>
c.在引用靜態資源時都要在引用之前加上static,形如static/...

2.html跳轉鏈接
springboot中所有的連接跳轉都需經過服務器進行跳轉
若無攔截字符串則:
 controller前加註解@RequestMapping("/")
若controller類前加了攔截器註解則:
在該類下的跳轉方法中跳轉後出現的html頁面裏的所有靜態資源前需加   ../

3.form表單與後端springboot交互
在跳轉form表單之前的controller類中的攔截跳轉方法中需先傳一個包含from表單中所有屬性的對象到html頁面中,在form中input與對象的屬性對應綁定
<form role="form" action="#" th:action="@{/userlogin}" th:object="${user}" >
    <input  type="text" th:field="*{name}">
    <input type="password" th:field="*{password}">
    <button type="submit" class="btn8">登錄!</button>
</form>

4.修改/刪除傳值
    <a th:href="@{/viewgoods/} + ${i.id}" class="btn btn-sm btn-success updateBtn">修改</a>
    <a th:href="@{/viewgoods/} + ${i.id}" class="btn btn-sm btn-danger deleteBtn">刪除</a>
      所傳值數據類型前後一致,否者hibate中查詢不出

5.單選框
<input type="radio" value="0" th:attr ="checked=#{goods_a.goods_ishot == false?true:false}">

6.add上傳

7.前端一直獲取不到img_path屬性
在實體類種img_path屬性不知什麼原因set和get方法的屬性名首字母都是小寫,在springboot中需要格式化第一個改爲大寫
setimg_path和getimg_path改爲setImg_path和getImg_path

8.thymeleaf前端
<th:block th:each ="i,iStat:${goodlist}">
</th:block>
i爲遍歷的對象,iStat爲對象在集合中的下標
<th:block th:if="${(iStat.index) % 2 eq 0}">
</th:block>
<th:block th:unless="${(iStat.index) % 2 eq 0}">
</th:block>
unless爲後面表達式的取反,即表達式不成立

9.springboot from經攔截器返回的頁面刷新後導致from的數據重複提交
返回頁面時通過重定向解決
return "redirect:select";
相當於瀏覽器重新請求select的攔截字符,需要重新定義select的攔截器返回頁面

10.刪除提交確認框
在a標籤中thymeleaf調用onchlik js代碼(一直不能傳參)
th:οnclick="|delcfm(${i.id})|"
js代碼也有一定的規則
<script th:inline="javascript">
            /*<![CDATA[*/
            function delcfm(url) {
                $('#url').val('/delete'+ url);//給會話中的隱藏屬性URL賦值
                $('#delcfmModel').modal();
            }
            function urlSubmit(){
                var url=$.trim($("#url").val());//獲取會話中的隱藏屬性URL
                th:window.location.href=url;
            }
            /*]]>*/
</script>

11.from中的button必須加上type="button"不然默認爲submit

12.fileinput的設置不生效
先導入fileinput的css和js才引入設置的js,不然報沒有fileinput標籤的錯誤

13.點擊提交以後跳出的js,confirm確定窗口,點擊取消以後任然提交了表單。
onClick="return confirm_update()"  在onClick='moth()' 中改爲onClick='return moth()'

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章