1、通过request.getParameter("参数名")获取后台的值
2、通过<%=变量%>的方式赋给js变量
示例:
<script>
<%
String s2 = (String)request.getParameter("参数名")//获取后台参数给s2变量,<%%>表示这范围内是服务器解析的语言
%>
var s = '<%=s2%>'//输出s2给js变量s
</script>
基于Razor视图引擎(MVC3以上)的写法。View:
@using (Html.BeginForm("Sort", "YourControllerName"))
{
<input type="submit" value="NewsType" />
}
Controller的写法不变
比如说:后台Controller层:@Controller@RequestMapping("/holidymaintain")publicclassHolidyControllerextendsBaseController{privatestaticLogERROR=PssLogFactory.getErrorLog()@AutowiredprivateHolidyServiceholidyService@RequestMapping("/create")@ResponseBody@RequireLoginpublicResultResponsesaveHolidy(HttpServletRequestrequest,Holidyholidy,Modelmodel){ResultResponserr=newResultResponse(false)Calendara=Calendar.getInstance()a.setTime(holidy.getEndDate())a.get(Calendar.YEAR)try{holidy.setYear(a.get(Calendar.YEAR)+"")Booleanava=this.holidyService.judgeAvailable(holidy.getYear(),holidy.getName(),holidy.getId())if(!ava){rr.setMessage("Holidayisexist!")returnrr}LonguserId=getLoginUser().to(UserInfo.class).getId()holidy.setCreateBy(userId)holidy.setCreateDate(getDate())this.holidyService.save(holidy,userId)rr.setResult(true)}catch(Exceptione){rr.setMessage("Backenderror")ERROR.error(this.getClass().getSimpleName(),e)}returnrr}}前台调用:$("#holidySave").click(function(){varmsg=""msg+=validateName()msg+=validateStartTime()msg+=validateEndTime()msg+=validateMax()if(msg!=null&&msg.length>1){$.alert(msg)return}else{varplanVo=$("form").serialize()$.post("${path}/arwen/holidymaintain/create",planVo,function(data){if(data){if(data.result==true){$.alert("SaveSuccess",{ok:function(){window.location.href="${path}/arwen/holidymaintain/list"}})}else{$.alert(data.message)}}})}})