大四找到作業之后就一直在寢室茍著,很多開發的技巧和tips都忘了,上周特地重新寫了一個springboot的專案,很簡單,增刪查改,登陸注冊,管理員權限,將資料傳遞到前端,對每特定的資料進行展示和評論等,總之就是一個練手的專案,但也遇到了很多的坑,特總結如下
- 如何訪問templates下的靜態資源

新建工具類如下,即可訪問templates下的index,別名為login.html
public class MyConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("login.html").setViewName("index");
}
}
- 如何訪問pom中的webjar依賴
還是圖1中的類,重寫方法
這樣就可以在頁面中參考前端資源,例如layui,vue的jar包,否則訪問會報錯(可能還有其他的方法訪問webjar,歡迎大家討論)
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/webjars/**") .addResourceLocations("classpath:/META-INF/resources/webjars/");
}
- 常用的注釋
@Configuration:定義配置類,暫時只在WebMvcConfigurer的實作類下使用
@Controller:定義控制層
@RequestMapping(“dis”) 訪問控制層方法的路徑,可用在方法和類上
@Autowired:自動注入
@ResponseBody:方法的回傳值是不再是資源路徑,而是 json格式的字串

@Mapper:定義dao層(注解開發還要用到@Select,@Update,@Insert,@Delete)
@Service:定義service層
@SpringBootApplication:專案主類,啟動專案用(一般不用自己寫)
- 前端撰寫注意事項
οnblur="f1()"當控制元件失去焦點時,呼叫f1方法()
required 必填項,常配合input使用
我比較作死,前期用的bootstrip,后來改的layui,如果一個頁面引入了這兩個包,emmmm自行體會吧 - json和ajax的使用
json:一種鍵值對的格式,物件用{},陣列用[]
ajax:
function f1(){
$.get("/user/name",{"na":$("#name").val()},function (data) {
if(data.toString()==0){
$("#g").css("color","red")
window.alert(data);
}
$("#pp").html=(data);
})
}
上述代碼就是ajax的使用,判斷注冊的用戶名是否重復
get的()三個引數,第一個是請求路徑,第二個是要驗證的字串(#name是找前端id為name的控制元件,.val()是獲取控制元件的值,fun(data是獲取后臺回傳的json資料))
-
layui和bootstrip的區別,layui的優勢
我個人感覺,layui更美觀,更適合不咋會前端開發的后端開發者,比如我 -
layui表格需要的資料格式
layui動態表格要接收一個json格式的資料,但和@ResponseBody回傳的json略有差異,這個時候我們要用到一個工具類進行一下轉化
public class LayuiTypeJson<T> {
private int code=0;
private String msg="";
private int count;
private List<T> data=new ArrayList<T>();
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public List<T> getData() {
return data;
}
public void setData(List<T> data) {
this.data = data;
}
}
應用如下
@RequestMapping("list")
@ResponseBody
public LayuiTypeJson<Ngrade> findAll(){
List<Ngrade> all = dao.findAll();
LayuiTypeJson<Ngrade> json = new LayuiTypeJson<>();
json.setCount(all.size());
json.setData(all);
return json;
}
注意一下,回傳值型別是LayuiTypeJson
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/282288.html
標籤:其他
