最近使用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 = request.getParameter( "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 = params[param.toLowerCase()]; 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‘ /> ">