??xml version="1.0" encoding="utf-8" standalone="yes"?> Any existing applications that use the Cancel processing will need to modify their struts-config.xml to set the cancellable property for actions which require it. In Struts 1.2.9 the <set-property> is used to set the cancellable property for an action.... From Struts 1.3.x a new cancellable attribute can be used.... In both Struts 1.2.9 and Struts 1.3.x an exception handler can be configured to handle the InvalidCancelException
<action path="/fooAction"
input="/foo.jsp"
validate="true">
<set-property property="cancellable" value="true"/>
<forward name="success" path="/bar.jsp"/>
</action>
<action path="/fooAction"
input="/foo.jsp"
validate="true"
cancellable="true">
<forward name="success" path="/bar.jsp"/>
</action>
<action path="/fooAction"
input="/foo.jsp"
validate="true"
cancellable="true">
<forward name="success" path="/bar.jsp"/>
<exception key="errors.cancel"
type="org.apache.struts.action.InvalidCancelException"
path="/foo.jsp"/>
</action>
]]>
首先Q需要去下蝲LOG4Jq个软gq解压羃出其中的log4j.jar.在你的应用程序的classpath中包含该JAR文gQ你也可以简单地这个文件拷贝到JDK?java_home%\lib\ext目录下?br />
在作完以上工作后Q你可以下面的代码保存到名为TestLogging.java中:(x)
##############################
import org.apache.log4j.*;
// How to use log4j
public class TestLogging {
// Initialize a logging category. Here, we get THE ROOT CATEGORY
//static Category cat = Category.getRoot();
// Or, get a custom category
static Category cat = Category.getInstance(TestLogging.class.getName());
// From here on, log away! Methods are: cat.debug(your_message_string),
// cat.info(...), cat.warn(...), cat.error(...), cat.fatal(...)
public static void main(String args[]) {
// Try a few logging methods
cat.debug("Start of main()");
cat.info("Just testing a log message with priority set to INFO");
cat.warn("Just testing a log message with priority set to WARN");
cat.error("Just testing a log message with priority set to ERROR");
cat.fatal("Just testing a log message with priority set to FATAL");
// Alternate but INCONVENIENT form
cat.log(Priority.DEBUG, "Calling init()");
new TestLogging().init();
}
public void init() {
java.util.Properties prop = System.getProperties();
java.util.Enumeration enum = prop.propertyNames();
cat.info("***System Environment As Seen By Java***");
cat.debug("***Format: PROPERTY = VALUE***");
while (enum.hasMoreElements()) {
String key = (String) enum.nextElement();
cat.info(key + " = " + System.getProperty(key));
}
}
}
############################################################
Log4J 默认情况下可以记录五个层ơ(׃到高Q的日志消息?br />
1) debug
2)info
3)warn
4)error
5)fatal
在TestLoggin.class的目录中保存下列行在一个名字ؓ(f)log4j.properties 文g?默认情况下,当你在代码中使用getRoot()或getInstance("category_name")ӞLog4j?x)在应用E序?classpath中查找该文gQ?br />
############################################
log4j.rootCategory=DEBUG, dest1
log4j.appender.dest1=org.apache.log4j.ConsoleAppender
log4j.appender.dest1.layout=org.apache.log4j.PatternLayout
############################################
ConsoleAppender指定的是控制台附加器Q即日志消息?x)输出到控制CQ而PatternLayout则指定了消息输出的格式,默认情况下格式ؓ(f)%m%n,%m指定的是消息内容Q?n指定的是操作pȝq_上的换行W?q里更类gC语言中的输出控制语句?br />
现在Q你可以~译q且q行TestLogging.java了,你可以获得以下输出结果:(x)
Start of main()
Just testing a log message with priority set to INFO
Just testing a log message with priority set to WARN
Just testing a log message with priority set to ERROR
Just testing a log message with priority set to FATAL
Calling init()
***System Environment As Seen By Java***
***Format: PROPERTY = VALUE***
java.runtime.name = Java(TM) 2 Runtime Environment, Standard Edition
sun.boot.library.path = c:\jdk1.3\jre\bin
java.vm.version = 1.3.0_02
java.vm.vendor = Sun Microsystems Inc.
... and so on
如果x印消息的层次如debug,info,error{,那可以在log4j.properties 文g的最后一行上增加如下一行:(x)
log4j.appender.dest1.layout.ConversionPattern=%-5p: %m%n
q一行覆盖了默认的消息输出格?m%n,%p指定的是打印消息的层ơ(info,debug...,其中-5指定的是五个字符的宽度,-指定的是左对?,%m指定的是消息的内容,%n指定的则是操作系l^C的换行符.
当作完这些工作后Q无重新编译TestLogging.javaQ再ơ运用TestLoggQ会(x)得到以下不出的输出结果:(x)
DEBUG: Start of main()
INFO : Just testing a log message with priority set to INFO
WARN : Just testing a log message with priority set to WARN
ERROR: Just testing a log message with priority set to ERROR
FATAL: Just testing a log message with priority set to FATAL
DEBUG: Calling init()
INFO : ***System Environment As Seen By Java***
DEBUG: ***Format: PROPERTY = VALUE***
INFO : java.runtime.name = Java(TM) 2 Runtime Environment, Standard Edition
INFO : sun.boot.library.path = c:\jdk1.3\jre\bin
INFO : java.vm.version = 1.3.0_02
INFO : java.vm.vendor = Sun Microsystems Inc.
... and so on
如果不想输出日志的DEBUG与INFO消息Q那么可以修?log4j.rotCategory=DEBUG,dest1"为:(x)
log4j.rootCategory=WARN,dest1
该行文g告诉Log4j跌层次低于WARN的消息输出,也就是说如DEBUG,INFO层次的消息将不会(x)产生输出Q再ơ运行TestLogging.classQ得C下结果:(x)
####################
WARN : Just testing a log message with priority set to WARN
ERROR: Just testing a log message with priority set to ERROR
FATAL: Just testing a log message with priority set to FATAL
####################
W二部分 Log4j 详解
Log4j有三个主要的lgQcategory ,附g器和布局?br />
在程序中Q你可以初始化一个category q且调用它的各种日志Ҏ(gu)来将消息字符串记录到日志中?br />
一个category可以被配|用来输出到多个目标Q这些日志目标在Log4j框架中被UCؓ(f)附g?q些附g器可以包括控制台、文本文件、HTML文g?XML文g甚至是Windowsq的事g日志pȝQ甚臛_以被作ؓ(f)邮g被发送。而这些所有的目标都是通过log4j.properties文g来进行配|,对于使用Log4j框架的程序来讲只是简单地调用cM于info()、debug(){的Ҏ(gu)?br />
附g器类可以是ConsoleAppender, FileAppender, SMTPAppender, SocketAppender, NTEventLogAppender, SyslogAppender, JMSAppender, AsyncAppender ?NullAppender{?附g器类可以使用布局QlayoutQ来在发送消息到目标之前q行格式化。例如HTMLLayout会(x)把消息格式化为HTML 格式?br />
除了可以记录消息字符串到日志文g之外Q同时还可以记录日期、时间、消息层ơ、类名、源代码的行数、方法名U、线E名UC?qing)其它信息,而具体的输出需要由附g器的布局理器来配置?br />
category的名字是大小写区分以"."分隔的一个字W串。一般情况下我们通常使用your_class_name.class.getName()来获得一个JAVAcd来作为category的名字,例如testproj.util.test?br />
Each word in the category name is said to be an ancestor of the subsequent words and a parent of the immediately following word. This is important because Log4j has this concept of inheriting priorities and appenders from ancestors until overridden in a particular category.
有一个没有名U的category叫root,它就像xml的document元素Q是所有category的祖先?br />
可以使用以下代码来初始一个根category或指定的category?br />
################
Category cat = Category.getRoot();
Category cat2 = Category.getInstance("your.category.name");
###################
代表层次的常量由高到ơ是FATAL、ERROR、WARN、INFO和DEBUGQ可以在log4j.properties中指定category所属的层次Q例如指定log4j.rootCategory=WARN,simple则意呌用rootq个category的程序只?x)记录WARN?WARN以上的消息。如果没有ؓ(f)一个category指定默认的categoryQ那么category会(x)从其父category来ѝ?br />
常见的Categorycȝ日志Ҏ(gu)有:(x)
public void log(Priority p, Object message);
// Convenient shortcuts to the generic logging method
public void debug(Object message);
public void info(Object message);
public void warn(Object message);
public void error(Object message);
public void fatal(Object message);
log4j 只记录层ơ与预设层次相等或更高别的消息Q如以下代码Q?br />
Category cat = Category.getRoot();
cat.setPriority(Priority.ERROR);//讄预设层次为ERRORU?br />
// Later...
//cat.info("Started processing..."); //q条消息不?x)输出,ERROR
cat.error("User input is erroneous!"); //消息输出Q层ơ相{?br />
cat.fatal("Cannot process user input. Program terminated!"); //消息输出Q层ơ高于预讑ֱ?br />
W三部分 Log4j 配置
所有的配置工作应该在log4j.properties文g中完成,而该文g一般须攑֜应用E序的相同的目录中?br />
在日志系l用之前,我们必须首先配置log4j.配置log4j意味着增加附g器到Categoryq且为每一个Category讄一个Layout?br />
category之间是有l承关系Q但他们增加到l(f)og4j.properties文g中的序是不固定的?br />
CZ一:
#############################################################
# 讄log4j的根category所使用的预讑ֱơ是DEBUG,而只使用A1q个附g?
log4j.rootCategory=DEBUG, A1
#附g器A1被设|ؓ(f)控制台附件器?br />
log4j.appender.A1=org.apache.log4j.ConsoleAppender
#附g器用的布局是PatternLayout,x式布局
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
#附g器A1的模式是%-4r [%t] %-5p %c %x - %m%n,其中%m代表消息字符Ԍ%n代表换行W?其它?开头的字符代表的含义如下文?br />
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
#################################################################
CZ二:(x)
#########################################################
#### Use two appenders, one to log to console, another to log to a file
log4j.rootCategory=debug, stdout, R
# Print only messages of priority WARN or higher for your category
log4j.category.your.category.name=WARN
# Specifically inherit the priority level
#log4j.category.your.category.name=INHERITED
#### First appender writes to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
#### Second appender writes to a file
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=example.log
# Control the maximum log file size
log4j.appender.R.MaxFileSize=100KB
# Archive log files (one backup file here)
log4j.appender.R.MaxBackupIndex=1
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
########################################################
/**
* @author Jstar
*
*
* H口 > 首选项 > Java > 代码生成 > 代码和注?br />
*/
import java.math.BigDecimal;
/**
* ׃Java的简单类型不能够_的对点数进行运,q个工具cL供精
* 的点数运,包括加减乘除和四舍五入?br />
*/
public class Arith {
//默认除法q算_ֺ
private static final int DEF_DIV_SCALE = 10;
//q个cM能实例化
private Arith() {
}
/**
* 提供_的加法运?br />
* @param v1 被加?br />
* @param v2 加数
* @return 两个参数的和
*/
public static double add(double v1, double v2) {
bigdecimal b1 = new BigDecimal(Double.toString(v1));
bigdecimal b2 = new BigDecimal(Double.toString(v2));
return b1.add(b2).doubleValue();
}
/**
* 提供_的减法运?br />
* @param v1 被减?br />
* @param v2 减数
* @return 两个参数的差
*/
public static double sub(double v1, double v2) {
bigdecimal b1 = new BigDecimal(Double.toString(v1));
bigdecimal b2 = new BigDecimal(Double.toString(v2));
return b1.subtract(b2).doubleValue();
}
/**
* 提供_的乘法运?br />
* @param v1 被乘?br />
* @param v2 乘数
* @return 两个参数的积
*/
public static double mul(double v1, double v2) {
bigdecimal b1 = new BigDecimal(Double.toString(v1));
bigdecimal b2 = new BigDecimal(Double.toString(v2));
return b1.multiply(b2).doubleValue();
}
/**
* 提供Q相对)(j)_的除法运,当发生除不尽的情冉|Q精到
* 数点以?0位,以后的数字四舍五入?br />
* @param v1 被除?br />
* @param v2 除数
* @return 两个参数的商
*/
public static double div(double v1, double v2) {
return div(v1, v2, DEF_DIV_SCALE);
}
/**
* 提供Q相对)(j)_的除法运。当发生除不的情况Ӟ由scale参数?br />
* 定精度,以后的数字四舍五入?br />
* @param v1 被除?br />
* @param v2 除数
* @param scale 表示表示需要精到数点以后几位?br />
* @return 两个参数的商
*/
public static double div(double v1, double v2, int scale) {
if (scale < 0) {
throw new IllegalArgumentException("The scale must be a positive integer or zero");
}
bigdecimal b1 = new BigDecimal(Double.toString(v1));
bigdecimal b2 = new BigDecimal(Double.toString(v2));
return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
}
/**
* 提供_的小C四舍五入处理?br />
* @param v 需要四舍五入的数字
* @param scale 数点后保留几位
* @return 四舍五入后的l果
*/
public static double round(double v, int scale) {
if (scale < 0) {
throw new IllegalArgumentException("The scale must be a positive integer or zero");
}
bigdecimal b = new BigDecimal(Double.toString(v));
bigdecimal one = new BigDecimal("1");
return b.divide(one, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
}
};
l过试验得到下面的一些规则:(x)
一. response.sendRedirect()
? response.setHeader("Location","")
? <jsp:forward page="" />
然后在WEB-INF目录下新建urlrewrite.xml
在其中进行重写规则的定义Q它使用正则表达式来q行规则的定?/p>
<?xml version="1.0" encoding="utf-8"?> <!-- Configuration file for UrlRewriteFilter --> 上面是我的一个简单的试 <rule>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 2.6//EN"
"
http://tuckey.org/urlrewrite/
<urlrewrite>
<rule>
<from>/test.html</from>
<to type="redirect">%{context-path}/page.html</to>
</rule>
<rule>
<from>/param/(.*)</from>
<to>/param.jsp?param=$1</to>
</rule>
</urlrewrite>
<rule>
<from>/test.html</from>
<to type="redirect">%{context-path}/page.html</to>
</rule>
是将test.html的访问请求{发给page.html
<from>/param/(.*)</from>
<to>/param.jsp?param=$1</to>
</rule>
param.jsp?param=111q种h重写?param/111
]]>