JSP页面列表动态表的实现
客户选择在页面中列表显示的字段,然后访问列表中就是用户勾选上的字段显示。他是怎么实现的,原理。...
客户选择在页面中列表显示的字段,然后访问列表中就是用户勾选上的字段显示。他是怎么实现的,原理。
展开
2个回答
2013-04-15
展开全部
一、在页面上用复选框进行复选,然后把信息写入到XML文件中。二、进行页面展示时候读取xml文件。三、使用反射机制调用类中的get方法四、页面上用标签输出动态表头。五、双重循环在页面上输出动态列。 写XML的struts2处理类:
public class DynamicTbAction extends MySuperAction { public String List(){
return "dynamicTbList";
}
public String set(){
UserVO userVO = (UserVO)session.get("loginUser");
String path = request.getRealPath("/")+"sysconfig\\DynamicTable";
path = path.replace("\\", "/")+"/" + userVO.getUsername()+".xml";
String[] attributes = request.getParameterValues("publicAttribute");
if(attributes.length!= 0){
Element rootElement = new Element("tables");
Element tableElt = new Element("table");
Element tabInx = new Element("publicAttribute");
tableElt.addContent(tabInx);
for (int i = 0; i < attributes.length; i++) {
String attr = attributes[i];
String[] attrs = attr.split(",");
Element attrElement = new Element("column");
attrElement.setAttribute("name", attrs[0]);
attrElement.setAttribute("value", attrs[1]);
tabInx.addContent(attrElement);
}
rootElement.addContent(tableElt);
Document doc = new Document(rootElement);
XMLOutputter out = new XMLOutputter();
String xmlStr = out.outputString(doc);
System.out.println(xmlStr);
System.out.println(path);
try {
out.output(doc, new FileOutputStream(path));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return List();
}
} struts2处理到页面的列表: public String List(){
UserVO userVO = (UserVO)session.get("loginUser");
String path = request.getRealPath("/")+"sysconfig\\DynamicTable";
path = path.replace("\\", "/")+"/" + userVO.getUsername()+".xml";
List<String> columnName = JDOMUtil.getColumnNames(path, "publicAttribute");
List<String> columnValue = JDOMUtil.getColumnValue(path, "publicAttribute");
Pagination pagination = systemconfigHelper.queryPageAttribute("from PublicAttributePO", page, 5, "select count(*) from PublicAttributePO");
List<PublicAttributeVO> list = pagination.getList();
List entityList = new ArrayList();
for (Iterator iterator = list.iterator(); iterator.hasNext();) {
PublicAttributeVO publicAttributeVO = (PublicAttributeVO) iterator
.next();
if ("0".equals(publicAttributeVO.getStatus())) {
publicAttributeVO.setStatus("失效");
} else {
publicAttributeVO.setStatus("有效");
}
List<Object> entity = new ArrayList<Object>();
for (Iterator iterator1 = columnValue.iterator(); iterator1.hasNext();) {
String value = (String) iterator1.next();
String methodName = "get" + value;
try {
String[] args={};
Object name = JDOMUtil.invokeMethod(publicAttributeVO, methodName, args);
entity.add(name);
} catch (Exception e) {
e.printStackTrace();
}
}
entityList.add(entity);
}
request.setAttribute("navigation", pagination.pageNavigator("attribute_List.action"));
request.setAttribute("attributeList", entityList);
request.setAttribute("columnName", columnName);
return "attributeList";
}帮助类JDomUtilpublic class JDOMUtil { public static List<String> getColumnNames(String path, String tableName) { List<String> names = new ArrayList<String>(); SAXBuilder saxBuilder = new SAXBuilder();
try {
Document document = saxBuilder.build(path);
Element rootElement = document.getRootElement();
List beanList = rootElement.getChild("table").getChild(tableName)
.getChildren();
for (Iterator iterator = beanList.iterator(); iterator.hasNext();) {
Element elt = (Element) iterator.next();
String name = elt.getAttributeValue("name");
names.add(name);
String value = elt.getAttributeValue("value");
}
} catch (JDOMException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
return names;
} public static List<String> getColumnValue(String path, String tableName) { List<String> values = new ArrayList<String>();
SAXBuilder saxBuilder = new SAXBuilder();
try {
Document document = saxBuilder.build(path);
Element rootElement = document.getRootElement();
List beanList = rootElement.getChild("table").getChild(tableName)
.getChildren();
for (Iterator iterator = beanList.iterator(); iterator.hasNext();) {
Element elt = (Element) iterator.next();
String value = elt.getAttributeValue("value");
values.add(value);
}
} catch (JDOMException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
return values;
} public static Object invokeMethod(Object methodObject, String methodName,
Object[] args) throws Exception {
Class ownerClass = methodObject.getClass();
Class[] argsClass = new Class[args.length];
for (int i = 0, j = args.length; i < j; i++) {
argsClass[i] = args[i].getClass();
}
Method method = ownerClass.getMethod(methodName, argsClass);
return method.invoke(methodObject, args);
}}
public class DynamicTbAction extends MySuperAction { public String List(){
return "dynamicTbList";
}
public String set(){
UserVO userVO = (UserVO)session.get("loginUser");
String path = request.getRealPath("/")+"sysconfig\\DynamicTable";
path = path.replace("\\", "/")+"/" + userVO.getUsername()+".xml";
String[] attributes = request.getParameterValues("publicAttribute");
if(attributes.length!= 0){
Element rootElement = new Element("tables");
Element tableElt = new Element("table");
Element tabInx = new Element("publicAttribute");
tableElt.addContent(tabInx);
for (int i = 0; i < attributes.length; i++) {
String attr = attributes[i];
String[] attrs = attr.split(",");
Element attrElement = new Element("column");
attrElement.setAttribute("name", attrs[0]);
attrElement.setAttribute("value", attrs[1]);
tabInx.addContent(attrElement);
}
rootElement.addContent(tableElt);
Document doc = new Document(rootElement);
XMLOutputter out = new XMLOutputter();
String xmlStr = out.outputString(doc);
System.out.println(xmlStr);
System.out.println(path);
try {
out.output(doc, new FileOutputStream(path));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return List();
}
} struts2处理到页面的列表: public String List(){
UserVO userVO = (UserVO)session.get("loginUser");
String path = request.getRealPath("/")+"sysconfig\\DynamicTable";
path = path.replace("\\", "/")+"/" + userVO.getUsername()+".xml";
List<String> columnName = JDOMUtil.getColumnNames(path, "publicAttribute");
List<String> columnValue = JDOMUtil.getColumnValue(path, "publicAttribute");
Pagination pagination = systemconfigHelper.queryPageAttribute("from PublicAttributePO", page, 5, "select count(*) from PublicAttributePO");
List<PublicAttributeVO> list = pagination.getList();
List entityList = new ArrayList();
for (Iterator iterator = list.iterator(); iterator.hasNext();) {
PublicAttributeVO publicAttributeVO = (PublicAttributeVO) iterator
.next();
if ("0".equals(publicAttributeVO.getStatus())) {
publicAttributeVO.setStatus("失效");
} else {
publicAttributeVO.setStatus("有效");
}
List<Object> entity = new ArrayList<Object>();
for (Iterator iterator1 = columnValue.iterator(); iterator1.hasNext();) {
String value = (String) iterator1.next();
String methodName = "get" + value;
try {
String[] args={};
Object name = JDOMUtil.invokeMethod(publicAttributeVO, methodName, args);
entity.add(name);
} catch (Exception e) {
e.printStackTrace();
}
}
entityList.add(entity);
}
request.setAttribute("navigation", pagination.pageNavigator("attribute_List.action"));
request.setAttribute("attributeList", entityList);
request.setAttribute("columnName", columnName);
return "attributeList";
}帮助类JDomUtilpublic class JDOMUtil { public static List<String> getColumnNames(String path, String tableName) { List<String> names = new ArrayList<String>(); SAXBuilder saxBuilder = new SAXBuilder();
try {
Document document = saxBuilder.build(path);
Element rootElement = document.getRootElement();
List beanList = rootElement.getChild("table").getChild(tableName)
.getChildren();
for (Iterator iterator = beanList.iterator(); iterator.hasNext();) {
Element elt = (Element) iterator.next();
String name = elt.getAttributeValue("name");
names.add(name);
String value = elt.getAttributeValue("value");
}
} catch (JDOMException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
return names;
} public static List<String> getColumnValue(String path, String tableName) { List<String> values = new ArrayList<String>();
SAXBuilder saxBuilder = new SAXBuilder();
try {
Document document = saxBuilder.build(path);
Element rootElement = document.getRootElement();
List beanList = rootElement.getChild("table").getChild(tableName)
.getChildren();
for (Iterator iterator = beanList.iterator(); iterator.hasNext();) {
Element elt = (Element) iterator.next();
String value = elt.getAttributeValue("value");
values.add(value);
}
} catch (JDOMException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
return values;
} public static Object invokeMethod(Object methodObject, String methodName,
Object[] args) throws Exception {
Class ownerClass = methodObject.getClass();
Class[] argsClass = new Class[args.length];
for (int i = 0, j = args.length; i < j; i++) {
argsClass[i] = args[i].getClass();
}
Method method = ownerClass.getMethod(methodName, argsClass);
return method.invoke(methodObject, args);
}}
2013-04-15
展开全部
你可以描述的更清楚一点. 没有太明白你的意思. 你是不是想在某个页面选中某一项, 然后再打开另一个页面就自动选中刚才的那一项吗?如果是这样. 直接到另一个页面的时候带个参数就可以了吧..
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询