Struts的异常处理机制是同过类反射,映射到相应的key,再从properties文件中找出相应字符显示。可是这样的话如果要想丰富异常的显示结果是很困难的,且不好跟踪到底是哪儿出问题了。还有一点就是,在做异常显示的显示上我们都是将异常信息写到了Exception中,如new Exception("登陆名为空!");这样在前台页面上只需读取其信息就行了,可是struts却把地层做的这些功夫全“屏蔽”了,统一显示了相同信息----因为都是Exception异常类。各位大哥,有什么办法可以既运用struts的异常捕获(有时也挺好使的,不必自己捕获,自动跳转。),又能读取到异常的信息呢?
解决方案 »
- struts2的bean标签取值问题
- 大家讨论一下。。。 关于栈和堆。。 释放内存问题
- 急问一个单选按钮方面的问题,望高手指点!
- 关于在配置文件中怎样写我“数据库”的参数,比如:用户名密码之类的
- 使用过ResourceOne+eclipse的朋友请
- httpconnect通讯问题(请大家多多指教)
- 求助:jsp中向mysql数据库插入数据时报异常错误
- 问个JDBC连接sql server问题?
- 关于JSP的几个问题
- 登录账户及密码均为“admin”(直接在LoginServlet类中判断)代码我已经写好了,求指点!!!
- 无法updata数据库!(请帮忙看看啊!)
- 向高手求教HttpURLConnection的问题,非常着急,谢谢帮忙。
errors.add(ActionErrors.GLOBAL_ERROR,new ActionError("key"));
this.saveErrors(request,errors);
页面上用<html:errors/>
好像现在统一用ActionMessages了
errors.add(ActionErrors.GLOBAL_ERROR,new ActionError("key"));
this.saveErrors(request,errors);
页面上用<html:errors/>
好像现在统一用ActionMessages了
我也试过你说的方法,但这样的话同样也无法获得地层作的异常说明。而且得取一下properties文件。
我想知道的是struts获得Exception的控制权后会将他放在哪儿?
public void setBodyContent(javax.servlet.jsp.tagext.BodyContent);
public int doAfterBody(); 那么由于BodyTag继承了javax.servlet.jsp.tagext.Tag接口,所以我们再来看一下Tag中的方法: public void release();
public javax.servlet.jsp.tagext.Tag getParent();
public void setParent(javax.servlet.jsp.tagext.Tag);
public int doEndTag();
public int doStartTag();
public void setPageContext(javax.servlet.jsp.PageContext); 说了这么多方法,是不是有点头晕了?呵呵,那么究竟BodyTag的处理过程是怎么样的呢?下面我们就来说一下它的处理流程: 1.当容器创建一个新的标签实例后,通过setPageContext来设置标签的页面上下文.
2.使用setParent方法设置这个标签的上一级标签,如果没有上一级嵌套,设置为null.
3.设置标签的属性,这个属性在标签库描述文件中定义,如果没有定义属性,就不调用此类方法.
4.调用doStartTag方法,这个方法可以返回EVAL_BODY_INCLUDE和SKIP_BODY,当返回EVAL_BODY_INCLUDE时,就计算标签的body,如果返回SKIP_BODY,就不再计算标签的body.
5.调用setBodyContent设置当前的BodyContent.
6.调用doInitBody,如果计算BodyContent时需要进行一些初始化,就在这个方法中进行.
7.每次计算完Body后调用doAfterBody,如果返回EVAL_BODY_TAG,表示继续计算一次Body,知道返回SKIP_BODY才继续往下执行.
8.调用doEndTag方法,这个方法可以返回EVAL_PAGE或者SKIP_PAGE,当返回EVAL_PAGE时,容器将在标签结束时继续计算JSP页面其他的部分;如果返回SKIP_PAGE,容器将在标签结束时停止计算JSP页面其他的部分.
9.调用release()方法释放标签程序占用的任何资源。 知道了标签处理的流程了,那么下面我们就开始写一个自己的java类了 我的标签类的源码:package test.jsp.tags; import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.JspTagException; public class BodyTagExample
extends BodyTagSupport { private int counts; public BodyTagExample() {
super();
} public void setCounts(int counts) {
this.counts = counts;
} public int doStartTag() throws JspTagException {
System.out.println("doStartTag...");
if (counts > 0) {
return EVAL_BODY_TAG;
}
else {
return SKIP_BODY;
}
} public void setBodyContent(BodyContent bodyContent) {
System.out.println("setBodyContent...");
this.bodyContent = bodyContent;
} public void doInitBody() throws JspTagException {
System.out.println("doInitBody....");
} public int doAfterBody() throws JspTagException {
System.out.println("do After body..." + counts);
if (counts > 1) {
counts--;
return EVAL_BODY_TAG;
}
else {
return SKIP_BODY;
}
} public int doEndTag() throws JspTagException {
System.out.println("do end Tag...");
try {
if (bodyContent != null) {
bodyContent.writeOut(bodyContent.getEnclosingWriter());
}
}
catch (java.io.IOException e) {
throw new JspTagException("IO Error: " + e.getMessage());
}
return EVAL_PAGE;
}
}
写完了java类,下面我们该用一个描述文件来描述我们写的这个类了: 我的tld描述文件源码:<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
"http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.1</jspversion> <shortname>Application Tag Library</shortname>
<uri>http://jakarta.apache.org/taglibs/struts-example-1.0</uri>
<info>tag loop</info> <tag>
<name>loop</name>
<tagclass>test.jsp.tags.BodyTagExample</tagclass>
<bodycontent>JSP</bodycontent>
<attribute>
<name>counts</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag></taglib> 我们对这个文件进行一个小小的说明: 最重要的是上面的黑体字部分,其他的部分我们可以拷贝粘贴就可以了。 tag中的name属性:定义了我们的tag名称,在后面会用到。
tag中的tagclass属性:指定了我们这个tag的实现类。
tage中的bodycontent属性:指定我们的页面内容是什么性质的。(注意:在jsp开发中这里必须写JSP)
tage中的attribute属性:定义了我们的这个tag可能有的属性。 attribute中的name属性:指定了属性的名称。它和我们类中定义的“int counts;”必须一样,并且在类中还必须包含一个setCounts(int counts)方法,否则这个属性就不能设置。
attribute中的required属性:表示这个属性是否是必须的。
attribute中的rtexprvalue属性:表示这个属性是否可以用JSP的程序段的结果输出。 现在我们的tag就写完了,下面就是我们怎么在页面中用了。 首先,我们要在web.xml中声明这个tag 我的web.xml源码:<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<taglib>
<taglib-uri>demotag</taglib-uri>
<taglib-location>/WEB-INFtldmytag.tld</taglib-location>
</taglib>
</web-app>
其次,我们要在页面中引用这个tag。 我的jsp源码:<%@ page contentType="text/html; charset=GBK" language="java"%>
<%@ taglib uri="demotag" prefix="demo" %>
<html>
<body>
<br>
<demo:loop counts="5">
现在时间是:<%=new java.util.Date().toString()%><br>
</demo:loop>
<br>
</body>
</html>
好了,一个tag就开发完了,下面是页面的显示结果:现在时间是:Thu Sep 22 12:33:31 CST 2005
现在时间是:Thu Sep 22 12:33:31 CST 2005
现在时间是:Thu Sep 22 12:33:31 CST 2005
现在时间是:Thu Sep 22 12:33:31 CST 2005
现在时间是:Thu Sep 22 12:33:31 CST 2005 这里是后台打出来的信息:doStartTag...
setBodyContent...
doInitBody....
do After body...5
do After body...4
do After body...3
do After body...2
do After body...1
do end Tag... 一切OK了!
public static final String where()
{
StackTraceElement[] elems = new Exception().getStackTrace();
return (elems.length < 2) ? "" : elems[1].getFileName() + "@" +
elems[1].getLineNumber() + ":" + elems[1].getClassName() + "." + elems[1].getMethodName() + "()";
}public class ObjectAnalyzer
{
public static String toString(Object obj)
{
StringBuffer str=new StringBuffer("");
Class c = obj.getClass();
Field[] fields = c.getDeclaredFields();
AccessibleObject.setAccessible(fields,true);
for(int i=fields.length-1;i>=0;i--)
{
Field f = fields[i];
str.append(" [ ").append(f.getName()).append("=");
try
{
Object o = f.get(obj);
if(o == null)
{
str.append("null");
}
else
{
str.append(o.toString());
}
}
catch(Exception e)
{
e.printStackTrace();
}
str.append(" ] ,");
}
// System.out.println("Object.toString is : "+ str);
return str.toString();
}public static void main(String args[])
{
try
{
Object applyData = Class.forName("com.zte.cpc.project.projectCreate.bo.ProjectTreeData").newInstance();
//System.out.println("applyData is : "+ applyData);
System.out.println(ObjectAnalyzer.toString(applyData));
}
catch (InstantiationException e)
{
// TODO 自动生成 catch 块
e.printStackTrace();
}
catch (IllegalAccessException e)
{
// TODO 自动生成 catch 块
e.printStackTrace();
}
catch (ClassNotFoundException e)
{
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}
package com.scjp;import java.io.File;
import java.io.FileWriter;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.dom4j.Node;
public class ParserXmlDom4j
{
public final static String filePath = "com\\scjp\\MyXml.xml"; public Document parse(File file) throws DocumentException
{
SAXReader reader = new SAXReader();
Document document = reader.read(file); return document;
} public void getXml(Document document) throws DocumentException
{
Element root = document.getRootElement();
Iterator it = root.elementIterator(); //Iterator it = root.elementIterator("name");
//Iterator it = root.attributeIterator();
while (it.hasNext())
{
Element sub = (Element) it.next();
}
} public void getNode(Document document) throws DocumentException
{
//List list = document.selectNodes("//row/person");
Node node = document.selectSingleNode("//row/person/name");
String value = node.getText();
System.out.println("value : " + value);
}
public void writeXml() throws Exception
{
Document doc = DocumentHelper.createDocument();
Element root = doc.addElement("root");
root.addComment("this is test xml file");
ArrayList children = new ArrayList();
Element ele1 = root.addElement("element1");
ele1.addAttribute("attr1","a");
ele1.addAttribute("attr2","b");
ele1.setText("this is element1");
Element ele2 = root.addElement("element2");
ele2.addAttribute("attr21","c");
ele2.setText("this is element2");
XMLWriter writer = null;
OutputFormat format = OutputFormat.createPrettyPrint();
writer = new XMLWriter(new FileWriter(new File("com\\scjp\\MyXml3.xml")),format);
writer.write(doc);
writer.close();
}
public static void main(String[] args)
{
ParserXmlDom4j pxd = new ParserXmlDom4j(); try
{
File file = new File(filePath);
Document document = pxd.parse(file);
Element root = document.getRootElement();
List list = root.selectNodes("//root/standards");
for(Iterator it = list.iterator();it.hasNext();)
{
Element subElement = (Element)it.next();
Attribute subAttr = subElement.attribute("type");
if("a3".equals(subAttr.getValue()))
{
for(Iterator ita = subElement.elementIterator("standard");ita.hasNext();)
{
Element belement = (Element)ita.next();
String sno = belement.attributeValue("sno");
System.out.println("sno : "+sno );
}
/*
List a3List = subElement.selectNodes("/@sno");
for(Iterator it3 = a3List.iterator();it3.hasNext();)
{
Attribute attr3 = (Attribute)it3.next();
String novalue = attr3.getValue();
System.out.println("novalue : "+ novalue);
}
*/
}
}
pxd.writeXml();
/*
List list = root.selectNodes("//root/standards/standard/@sno");
for(int i=0; i<list.size(); i++)
{
Attribute attrs = (Attribute)list.get(i);
String no = attrs.getValue();
System.out.println("no value : "+ no);
}
*/
/*
for(Iterator it = root.elementIterator("author"); it.hasNext();)
{
Element sub = (Element)it.next();
String name = sub.attributeValue("name");
String location = sub.attributeValue("location");
String fulName = sub.getText();
System.out.println("name "+name+" location : "+ location+" fulName "+ fulName);
}
*/
//pxd.getNode(doc);
//SAXReader reader = new SAXReader();
//Document document = reader.read(filePath);
/*
List list = document.selectNodes("//c/a/b/@color"); for (Iterator iter = list.iterator(); iter.hasNext();)
{
Attribute attribute = (Attribute) iter.next();
String color = attribute.getValue();
System.out.println("color : "+ color);
if (color.equals("255.255.0"))
{
attribute.setValue("0.0.0");
}
}
*/
}
catch (Exception e)
{
e.printStackTrace();
}
}
}<?xml version="1.0" encoding="UTF-8"?><!--
<xml-body>
<config>
<font>
<name>HanZi</name>
<size unit="pt">36</size>
</font>
</config>
</xml-body><row>
<person>
<name>王小明</name>
<college>信息学院</college>
<telephone>6258113</telephone>
<notes>男,1955年生,博士,95年调入海南大学</notes>
</person>
</row><c>
<a name="a">
<b color="255.255.0"/>
</a>
<a name="b">
<b color="255.255.1"/>
</a>
</c><root>
<author name="James" location="UK">James Strachan</author>
<author name="Bob" location="US">Bob McWhirter</author>
</root>
--> <standards type="a3">
<standard sno="1" svalue="400" />
<standard sno="3" svalue="400" />
</standards>
<standards type="b9">
<standard sno="1" svalue="300" />
</standards>