
类:ActionContextgetContext()getSession()put("ID", ID);
前台:<input type="hidden" id="ID" value="${sessionScopeID}"/>
js:var tID = documentgetElementById("ID")value;
通过request的getParameter的方法获取
1、通过requestgetParameter("参数名")获取后台的值
2、通过<%=变量%>的方式赋给js变量
示例:
<script>
<%
String s2 = (String)requestgetParameter("参数名");//获取后台参数给s2变量,<%%>表示这范围内是服务器解析的语言
%>
var s = '<%=s2%>';//输出s2给js变量s
</script>
方法一:直接使用<%=%>调用
前台JS:
[javascript] view plain copy
<script type="text/javascript">
var methodStr = "<%=BehindMethod() %>";
alert(methodStr);
</script>
后头方法:
[csharp] view plain copy
public static string BehindMethod()
{
return "这是一个后台的方法";
}
方法二:用ajax调用
前台js:
[javascript] view plain copy
<script type="text/javascript" src="js/jquery-143minjs"></script>
<script type="text/javascript">
var params = '{ext:"p9hp"}'; //参数,注意参数名要注意和后台方法参数名要一致
$(function(){
$("#btnOk")click(function(){
$ajax({
type:"POST", //请求方式
url:"AjaxDemoaspx/GetImg", //请求路径:页面/方法名字
data: params, //参数
dataType:"text",
contentType:"application/json; charset=utf-8",
beforeSend:function(XML>
获取id ${userid}
获取name ${username}
获取company ${usercompany}
JS中直接从java后台获得对象的值(数组的值)
这里举得例子是:JS直接从后台Contorller中(SpringMVC中的model中)获得数值的值
Contorller 此处将 talentIntegralRecordsDay talentIntegralRecordsIntegral 两个数组用JSONtoJSONString()封装。
@SuppressWarnings("deprecation")
@RequestMapping("/integralParadise") public ModelAndView Welcome(>
Users user = userServicecurrentUser(request);
usergetTalentUser()getIntegral();
Systemoutprintln(usergetTime()getDate());
TalentIntegralRecord[] signInTalentIntegralRecords= wechatIntegralServicegetUserSignInTalentIntegralRecords(usergetId()); int size = signInTalentIntegralRecordslength; int[] talentIntegralRecordsDay = new int[size];
Long[] talentIntegralRecordsIntegral = new Long[size]; for(int i=0;i<signInTalentIntegralRecordslength;i++){
talentIntegralRecordsDay[i]=signInTalentIntegralRecords[i]getOperatorTime()getDate();
talentIntegralRecordsIntegral[i]=signInTalentIntegralRecords[i]getIntegral();
}
Map<String,Object> map = new HashMap<String,Object>();
mapput("talentIntegralRecordsDay", JSONtoJSONString(talentIntegralRecordsDay));
mapput("talentIntegralRecordsIntegral", JSONtoJSONString(talentIntegralRecordsIntegral)); return new ModelAndView("wechat/integralParadise/rili",map);
}
前台JSP 因为用到两个数组数据的JS代码为页面引用的JS代码所以要在页面中先声明获得后台两个数组(这段JS代码应在引用的JS文件前面)
<script type="text/javascript">
$(document)ready(function(){
windowtalentIntegralRecordsDay = ${talentIntegralRecordsDay};
windowtalentIntegralRecordsIntegral = ${talentIntegralRecordsIntegral};
});</script>
引用的JS文件 开始就获得了两个数组的值
$(function() { var signFun = function() {
var dateArray = windowtalentIntegralRecordsDay;// 假设已经签到的
var talentIntegralRecordsIntegral = windowtalentIntegralRecordsIntegral; var $dateBox = $("#js-qiandao-list"),
$currentDate = $("current-date"),
$qiandaoBnt = $("#js-just-qiandao"),
_html = '',
_handle = true,
myDate = new Date();
$currentDatetext(myDategetFullYear() + '年' + parseInt(myDategetMonth() + 1) + '月' + myDategetDate() + '日'); var monthFirst = new Date(myDategetFullYear(), parseInt(myDategetMonth()), 1)getDay(); var d = new Date(myDategetFullYear(), parseInt(myDategetMonth() + 1), 0); var totalDay = dgetDate(); //获取当前月的天数
for (var i = 0; i < 42; i++) {
_html += ' <li><div class="qiandao-icon"></div></li>'
}
$dateBoxhtml(_html) //生成日历网格
var $dateLi = $dateBoxfind("li"); for (var i = 0; i < totalDay; i++) {
$dateLieq(i + monthFirst)addClass("date" + parseInt(i + 1)); for (var j = 0; j < dateArraylength; j++) { if (i == dateArray[j]) {
$dateLieq(i)addClass("qiandao"); var integral; if(talentIntegralRecordsIntegral[j]==0){
integral="大转盘";
}else{
integral="+"+talentIntegralRecordsIntegral[j];
}
$dateLieq(i)find("div")text(integral);
}
}
} //生成当月的日历且含已签到
$("date" + myDategetDate())addClass('able-qiandao');
$dateBoxon("click", "li", function() { if ($(this)hasClass('able-qiandao') && _handle) {
$(this)addClass('qiandao');
qiandaoFun();
}
首先确定你后台传过来的str是你所说的那样
其次你js应该写成
var indexdata2 = <%=str %>;
去掉外边的双引号,因为你这个是js的数组,带双引号就是字符串了,所以你不能直接用
以上就是关于js如何获取后台java传过来的String字符串全部的内容,包括:js如何获取后台java传过来的String字符串、如何用JavaScript获得后台传过来的参数、javascript怎么调用后台方法等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)