后台使用freeMarker和前端使用vue的方法及遇到的问题
一:freeMarker的使用
1:java后台使用freeMarker是通过Model,将值传给前端:
如:
@Controller
publicclassMobileNewsFreeMarkerController{
@RequestMapping("page/test")
publicStringTest(Modelmodel,HttpServletRequestrequest){
//获取项目路径
StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+""+request.getContextPath()+"/";
//将页面路径通过model传给前台
model.addAttribute("basePath",basePath);
//通过HttpServletRequest获取url中的值,如code值
Stringcode=request.getParameter("code");
//将获取的code参数传给前台
model.addAttribute("code",code);
//页面跳转
return"page/test";
}
}
2:前端页面获取后台传输的值(freeMarker传输的值只能在html页面获取)
注:这里是结合vue
第一步:在js中定义vue的相关参数:
varvm=newVue({
el:'#rrapp',
data:{
basePath:"",//项目路径
code:"",//code参数
},
..........(vue后面内容省略)
第二步:再在页面接收后台传输的值
$(document).ready(function(){ <#ifbasePath??> vm.basePath="${basePath}"; #if> <#ifuserId??> vm.code="${code}"; #if> });
二:使用中主要遇到的问题
1:Vue存在调用的先后顺序,虽然html页面将后台传输的值付给vue的data中的参数,但是在mounted中是无法使用的时候获取的还是创建的vue的时候data中赋的值,并不会使用html赋的值:
如:
varvm=newVue({
el:'#rrapp',
data:{
basePath:"",
code:"",
},
mounted:function(){
var_this=this;
console.log(_this.basePath);
//输出的还是:"",并不会输出html赋的值,所有在这里无法使用
}
2:但是在vue中methods内的方法是可以直接使用的;初始化如果需要html中传输的参数,可以使用以下方法:
$(function(){
vm.getData(vm.basePath,vm.code);
});
varvm=newVue({
el:'#rrapp',
data:{
basePath:"",
code:"",
},
methods:{
getData:function(baseUrlFlag,codeFlag){
var_this=this;
_this.basePath=baseUrlFlag;
_this.code=codeFlag;
//进行初始化业务操作!
},
}
总结
以上所述是小编给大家介绍的后台使用freeMarker和前端使用vue的方法及遇到的问题,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!