springMVC+Thymeleaf+VUE2后台向前台传值方式
1、@ModelAttribute
功能,注册以及别名,如@ModelAttribute(“testData”) Usertest testData,意思是将Usertest 对象注册一下,并且前台访问的别名为testData
后台用法:
//传递参数@ModelAttribute
testData.setUsename("测试@ModelAttribute获取");
thymeleaf 页面获取:
@ModelAttribute testData : <div th:text="${testData.usename}"></div> <br/>
thymeleaf js获取
thymeleaf模板中,VUE中可以写thymeleaf的插值表达式,模板加载时会解析成对应的字符串
var temp3 = [[${testData.usename}]]; //传递参数@ModelAttribute
alert("-----传递参数@ModelAttribute-----:"+temp3);
2、Model
类似一个map集合,将后台数据放入map集合中,传递给前台,跟struts中写map一样,只不过这里固定了类型叫Model,前台跟struts的map不一样了,到达前台时给拆分了,比如我放入一个aa,前台不是map.aa,而且直接aa
后台用法:
//传递参数Model
model.addAttribute("modelData", "测试model获取");
前台用法:
传递参数Model : <div th:text="${modelData}"></div><br/>
js获取:
alert("-----传递参数HttpServletRequest:"+temp1);
var temp2 = [[${modelData}]]; //传递参数Model
3、HttpServletRequest
这个常用,放入方法的输入参数中
后台用法:
//传递参数HttpServletRequest
request.setAttribute("requestTestData", "测试request数据");
thymeleaf页面用法:
传递参数HttpServletRequest : <div th:text="${requestTestData}"></div><br/>
thymeleaf js用法:
var temp1 = [[${requestTestData}]]; //传递参数HttpServletRequest
alert("-----传递参数HttpServletRequest:"+temp1);
demo:
/**
*初始化方法,这里默认不查询数据
* @param dc
* @return
*/
@RequestMapping({ "/" })
//跳转页面不需要@responseBody
public String init(@ModelAttribute("responseData") DataCenter dc,
@ModelAttribute("testData") Usertest testData,
Model model,
HttpServletRequest request) {
dataWrap.getPageInfo().setRowOfPage(15);
//传递参数@ModelAttribute
testData.setUsename("测试@ModelAttribute获取");
//传递参数Model
model.addAttribute("modelData", "测试model获取");
//传递参数HttpServletRequest
request.setAttribute("requestTestData", "测试request数据");
//设置参数
String query_Date = "2018-02-05 00:00:00";
model.addAttribute("query_Date", query_Date);
//获取查询条件里的交易月份,由于获取到的是2018-01-25 00:00:00 格式 需要转化为201801 故作此处理
QueryParamList paramQueryParamList = new QueryParamList();
paramQueryParamList.addParam("gdYjysbmJyyf","201802");
return "gd/yjydlsb.html";
}
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
<template th:substituteby="framework/pageset.html"></template>
</head>
<body>
<div id="app" view>
@ModelAttribute testData : <div th:text="${testData.usename}"></div> <br/>
传递参数Model : ${modelData} [[${modelData}]]<div th:text="${modelData}"></div><br/>
传递参数HttpServletRequest : <div th:text="${requestTestData}"></div><br/>
</div>
<script th:inline="javascript">
var dealFlag = "1";
var vm = new Vue({
el : "#app",
data : {
accountCode:"",
begintime:"",
endtime:"",
ipAddress:"",
onlineFlag:""
},
mounted : function(){
//页面加载完成后执行
myTable.setData(response.getAjaxDataWrap("dataWrap"));
$("#dataTotal").html("请选择交易月份");
//设置时间
var s =[[${query_Date}]];
s = s.replace(/-/g,"/");
var date = new Date(s );
this.begintime=date;
//接收参数
var temp1 = [[${requestTestData}]]; //传递参数HttpServletRequest
alert("-----传递参数HttpServletRequest:"+temp1);
var temp2 = [[${modelData}]]; //传递参数Model
alert("传递参数Model-----:"+temp2);
var temp3 = [[${testData.usename}]]; //传递参数@ModelAttribute
alert("-----传递参数@ModelAttribute-----:"+temp3);
},
methods : {
query: function(){
},
report: function(){
}
}
});
function nextpage(){
vm.query();
}
</script>
</body>
</html>