/*本Tag主要用于生成动态的check box界面,
*从settings.xml文件中读出相关内容关键字,
*并按照内容关键字的所属领域进行分块列示。
*每一个领域的check box取用相同的名称。*/package ronghai.proposals.tags;import java.util.*;
import java.io.*;
import java.net.URL;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import javax.servlet.http.*;
import javax.servlet.ServletContext;import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
public class CheckBoxListsTag extends TagSupport { private static final String LIST_NAME = "list_name";
private static final String CHECKBOX_NAME= "index_name";
private static final String VALUE = "value"; String superName = null;
String listName = null;
String content = null; public void setSuperName(String name) { this.superName = name; }
public String getSuperName() { return this.superName; } public void setListName(String name) { this.listName = name; }
public String getListName() { return this.listName; } public void setContent(String content) { this.content = content; }
public String getContent() { return this.content; } public int doStartTag() throws javax.servlet.jsp.JspException {
//superName can not be null
if (superName == null || listName == null) {
throw new JspException("attribute not defined in listmenu tag");
} //get xml root
Element root = null;
URL location = null;
try{
location = pageContext.getServletContext().getResource("/WEB-INF/xml/settings.xml");
System.out.println("the xml file url location is : "+location.toString());
}catch (java.net.MalformedURLException mfx) {
System.out.println("ListMenuTag url creation error: " + mfx);
return(SKIP_BODY);
} root = loadDocument(location); if (root == null) System.out.println("Root is null!");
else { //list a list menu depend on list_ame attribute. try {
//get list items by listName
System.out.println("from checkBoxListsTag super tag ,lists items start");
JspWriter out = pageContext.getOut();
//print every list area name
NodeList nodes = root.getElementsByTagName(superName); for(int loop = 0;loop < nodes.getLength();loop++){ //for1
Node node = nodes.item(loop);
//get the sub node's name
String area_name = ((Element)node).getAttribute(LIST_NAME);
//get the check box name
String checkbox_name = ((Element)node).getAttribute(CHECKBOX_NAME);
//list the p_keyword_area here
out.print("<tr>");
out.print("<td colspan=\"4\">");
out.print("<div align=\"center\">" + "<b>" + area_name + "</b>" + "</div>");
out.print("</td>");
out.print("</tr>"); //in every area,get the list and display the items
NodeList children = node.getChildNodes();
int lineController = 0; //control the row of check boxes,4 check boxes per line for(int in_loop = 0;in_loop <= children.getLength();in_loop++){ //for2 Node child = children.item(in_loop);
if((child != null) && (child.getNodeName() != null) && child.getNodeName().equals(listName)){ //if1
if (child instanceof Element) { //if2
String value = ((Element)child).getAttribute(VALUE);
String name = child.getFirstChild().getNodeValue(); //4 check boxes per line
if(lineController % 4 == 0)
out.print("<tr>");
out.print("<td>");
if(lineController == 0)
out.print("<div align=\"left\">");
out.print("<input type=\"checkbox\"name=\""+checkbox_name+"\" value=\""+value+"\"");
System.out.println("value is " + value + "content is " + content); if(isBelong(value,content))
out.print("checked");
out.print(">");
out.print(name);
if(lineController == 0) out.print("</div>");
out.print("</td>");
if(lineController % 4 == 3) out.print("</tr>"); lineController++;//increase the lineController for next loop
}//end if2
}//if1
}//end for2 }//end for1
System.out.println("from checkBoxListsTag,list items end");
} catch(Exception e) {
System.out.println("exceptions : "+e);
} }// end else
return(SKIP_BODY);
} boolean isBelong(String akeyword,String keywords)
{
if(keywords==null)
return false;
StringTokenizer st = new StringTokenizer(keywords,",");
while (st.hasMoreTokens())
{
if(akeyword.equals(st.nextToken()))
return true;
}
return false;
}
public static Element loadDocument(URL location) {
Document doc = null;
try {
InputSource xmlInp = new InputSource(location.openStream()); DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder parser = docBuilderFactory.newDocumentBuilder();
doc = parser.parse(xmlInp);
Element root = doc.getDocumentElement();
root.normalize();
return root;
} catch (SAXParseException err) {
System.out.println ("loadDocument error" + ", line " +
err.getLineNumber () + ", uri " + err.getSystemId ());
System.out.println("loadDocument error: " + err.getMessage ());
} catch (SAXException e) {
System.out.println("loadDocument error: " + e);
} catch (java.io.IOException e) {
System.out.println("loadDocument error: " + e);
} catch (Exception pce) {
System.out.println("loadDocument error: " + pce);
}
return null;
}}
*从settings.xml文件中读出相关内容关键字,
*并按照内容关键字的所属领域进行分块列示。
*每一个领域的check box取用相同的名称。*/package ronghai.proposals.tags;import java.util.*;
import java.io.*;
import java.net.URL;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import javax.servlet.http.*;
import javax.servlet.ServletContext;import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
public class CheckBoxListsTag extends TagSupport { private static final String LIST_NAME = "list_name";
private static final String CHECKBOX_NAME= "index_name";
private static final String VALUE = "value"; String superName = null;
String listName = null;
String content = null; public void setSuperName(String name) { this.superName = name; }
public String getSuperName() { return this.superName; } public void setListName(String name) { this.listName = name; }
public String getListName() { return this.listName; } public void setContent(String content) { this.content = content; }
public String getContent() { return this.content; } public int doStartTag() throws javax.servlet.jsp.JspException {
//superName can not be null
if (superName == null || listName == null) {
throw new JspException("attribute not defined in listmenu tag");
} //get xml root
Element root = null;
URL location = null;
try{
location = pageContext.getServletContext().getResource("/WEB-INF/xml/settings.xml");
System.out.println("the xml file url location is : "+location.toString());
}catch (java.net.MalformedURLException mfx) {
System.out.println("ListMenuTag url creation error: " + mfx);
return(SKIP_BODY);
} root = loadDocument(location); if (root == null) System.out.println("Root is null!");
else { //list a list menu depend on list_ame attribute. try {
//get list items by listName
System.out.println("from checkBoxListsTag super tag ,lists items start");
JspWriter out = pageContext.getOut();
//print every list area name
NodeList nodes = root.getElementsByTagName(superName); for(int loop = 0;loop < nodes.getLength();loop++){ //for1
Node node = nodes.item(loop);
//get the sub node's name
String area_name = ((Element)node).getAttribute(LIST_NAME);
//get the check box name
String checkbox_name = ((Element)node).getAttribute(CHECKBOX_NAME);
//list the p_keyword_area here
out.print("<tr>");
out.print("<td colspan=\"4\">");
out.print("<div align=\"center\">" + "<b>" + area_name + "</b>" + "</div>");
out.print("</td>");
out.print("</tr>"); //in every area,get the list and display the items
NodeList children = node.getChildNodes();
int lineController = 0; //control the row of check boxes,4 check boxes per line for(int in_loop = 0;in_loop <= children.getLength();in_loop++){ //for2 Node child = children.item(in_loop);
if((child != null) && (child.getNodeName() != null) && child.getNodeName().equals(listName)){ //if1
if (child instanceof Element) { //if2
String value = ((Element)child).getAttribute(VALUE);
String name = child.getFirstChild().getNodeValue(); //4 check boxes per line
if(lineController % 4 == 0)
out.print("<tr>");
out.print("<td>");
if(lineController == 0)
out.print("<div align=\"left\">");
out.print("<input type=\"checkbox\"name=\""+checkbox_name+"\" value=\""+value+"\"");
System.out.println("value is " + value + "content is " + content); if(isBelong(value,content))
out.print("checked");
out.print(">");
out.print(name);
if(lineController == 0) out.print("</div>");
out.print("</td>");
if(lineController % 4 == 3) out.print("</tr>"); lineController++;//increase the lineController for next loop
}//end if2
}//if1
}//end for2 }//end for1
System.out.println("from checkBoxListsTag,list items end");
} catch(Exception e) {
System.out.println("exceptions : "+e);
} }// end else
return(SKIP_BODY);
} boolean isBelong(String akeyword,String keywords)
{
if(keywords==null)
return false;
StringTokenizer st = new StringTokenizer(keywords,",");
while (st.hasMoreTokens())
{
if(akeyword.equals(st.nextToken()))
return true;
}
return false;
}
public static Element loadDocument(URL location) {
Document doc = null;
try {
InputSource xmlInp = new InputSource(location.openStream()); DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder parser = docBuilderFactory.newDocumentBuilder();
doc = parser.parse(xmlInp);
Element root = doc.getDocumentElement();
root.normalize();
return root;
} catch (SAXParseException err) {
System.out.println ("loadDocument error" + ", line " +
err.getLineNumber () + ", uri " + err.getSystemId ());
System.out.println("loadDocument error: " + err.getMessage ());
} catch (SAXException e) {
System.out.println("loadDocument error: " + e);
} catch (java.io.IOException e) {
System.out.println("loadDocument error: " + e);
} catch (Exception pce) {
System.out.println("loadDocument error: " + pce);
}
return null;
}}
解决方案 »
- JPA 中的二个问题,请教下 EntityManager 为什么为空。我用的是JDK,Tomcat6 Spring JPA
- 学习hibernate好呢,还是学习ibatis好呢?
- java如何找到工作
- STRUTS做平台型网站首页是怎么实现的
- 各位都说说web.xml可以做的事情吧。大家都来列上吧谢谢
- J2EE-ORACLE专业级技术交流平台(18891623) 欢迎专业人士加入
- B/S系统集成C/S系统的问题
- struts2自定义类型转换器的问题
- tomcat5.5.12如何启动servlet重新载入功能和启动root上下文
- 【请教各位】俺用过tomcat,现在需要用websphere/wsad,只有一个周末的时间,怎么搞定?
- 有关XML的简单问题
- 初学!struts 简单问题,请高手帮忙 在线等
<p_keywords_area list_name="数据库" index_name="database" default="所有">
<p_keywords value="101">Oracle</p_keywords> <p_keywords value="102">DB2</p_keywords>
<p_keywords value="103">Informix</p_keywords>
<p_keywords value="104">SQLServer</p_keywords>
<p_keywords value="105">Sybase</p_keywords>
<p_keywords value="106">其他</p_keywords>
然后用getInitParameter("param")取xml文件的值,就是不知道
weblogic.xml文件设置值-名称的格式。谁能告诉我,谢谢!!!