jsp页⾯获取参数的⽅法(url解析、el表达式赋值、session取
值)【原创】
最近使⽤myEclispse做⽹站,使⽤jsp+js+css做页⾯,⽹站中常⽤到从列表进⼊详情页⾯的跳转,下⾯对详情页⾯的值填充⽅式做⼀个简单总结:
1.url中使⽤request获取参数
jsp上⽅添加type参数
<%@page contentType= "text/html; charset=utf-8" autoFlush="true" %>
<%@taglib prefix= "s" uri ="/struts-tags" %>
<%
String type = Parameter( "type");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>...</html>
html的dom元素中获取该type值
< input id ="type" name ="type" type ="hidden" value ="<%= type%>">
2.url解析获取参数
//根据url获取参数的hash表
var getUrl = function() {
var url = window.location.href;
var paraString = url.substring(url.indexOf( "?") + 1, url.length).split("&" );
//参数组
var paraObj = {}
for (i = 0; j = paraString[i]; i++) {
paraObj[j.substring(0, j.indexOf( "=")).toLowerCase()] = j.substring(j.indexOf("=" ) + 1, j.length);
}
return paraObj;
};
//获取url中某参数的值
var getParam = function(params,param){
var returnValue = LowerCase()];
if (typeof (returnValue) == "undefined") {
return "" ; //不存在,返回""
} else {
return returnValue; //存在,返回
}
}
使⽤⽰例eg:
url =.....jsp?type="add"&id=1;
var params = getUrl(url);
var type = getParam(params,"type");
var id = getParam(params,"id");
3.后台对url解析,⽤el表达式给前端赋值
url = ...jsp?type=update
后台区分type为update后,将值⽤el表达式给前端页⾯
eg:
<input id = "mode" type= "text" value =' ${adInfo.title} ' />
4.使⽤session取值
后台将数据放⼤session中,前端使⽤<s:property />⽅式获取
< input id = "imageUrl" type = "hidden" value = "< s:property value = '#session.server_image_url' /> ">
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论