转载于:http://hi.baidu.com/passedbylove/item/d63f1314a0088afbdceeca03

作为程序员,在工作中或学习过程中难免遇到磕磕绊绊,能够操起手中的工具在第一时间内解决棘手

问题的人通常被敬仰为高手。

笔者最近在学习Struts 2,从书籍和资料上了解到struts提供了一个调试信息辅助标签<s:debug/>,

用户开发过程中使用,提供当前action相关的信息供开发人员查看。

当然这也是有前提的,使用此功能的前提是要打开devMode开关。

以下是开启方法,任选其一:

1。 在struts.xml文件中加入的<struts>节点下加入

<constant name="struts.devMode" value="true"/>

2。在struts.properties文件中加入devMode=true

注意:以下均基于devMode模式调试的

下面笔者以一个文件上传讲解struts2的调试技巧

方法一:在任意页面加<s:debug/>标签

当你访问此页面的时候在页面的最下面有一个[Debug]链接,如下图所示:

点击[Debug]链接展开即可查看框架堆栈中的实时信息

示例图1(文件上传前):

示例图二(文件上传后):
聪明的你应该知道,这些信息对你至关重要。虽然比不上ASP.NET的Trace功能,就目前来说在现有

的框架中来说Struts2能做到这样已经不错了。

除了以上的debug以外,笔者未能在互联网和官方找到Struts2 debug参数的详细使用方法,就连

官方的Debugging Struts都说的相当隐晦,好像故意不告诉框架使用者,可以说这篇blog是截至

目前(Struts 2.2.1.1)介绍debug参数比较详细的文章了。

1 debug=console(火狐测试通过,不支持IE6/Chrome)

特性:支持输入的参数pageDown/pageUp按钮上下翻滚

一次偶然的机会翻阅了Struts2源代码,发现有一个名为webconsole.html的文件。

用MyEclipse内置IE浏览器看了一下,面貌如下:


笔者起初尝试在form里添加debug参数传值过去,上传文件后跳转到结结果页可以弹出上面的黑色窗体。

往控制台输入参数 按下任意按钮都报错。

按图索骥 加入了struts dojo的支持。 加入了struts2-dojo-plugin-2.2.1.1.jar,果不其然可以运行了。

笔者找到了控制台相关的js代码,如下

function printResult(result_string)

{

var result_div = document.getElementById('wc-result');

var result_array = result_string.split(' ');

var new_command = document.getElementById('wc-command').value;

result_div.appendChild(document.createTextNode(new_command));

result_div.appendChild(document.createElement('br'));

for (var line_index in result_array) {

var result_wrap = document.createElement('pre')

line = document.createTextNode(result_array[line_index]);

result_wrap.appendChild(line);

result_div.appendChild(result_wrap);

result_div.appendChild(document.createElement('br'));

}

result_div.appendChild(document.createTextNode(':-> '));

result_div.scrollTop = result_div.scrollHeight;

document.getElementById('wc-command').value = '';

}

function keyEvent(event, url)

{

switch(event.keyCode){

case 13:

var the_shell_command = document.getElementById('wc-command').value;

if (the_shell_command) {

commands_history[commands_history.length] = the_shell_command;

history_pointer = commands_history.length;

var the_url = url ? url : window.opener.location.pathname;

dojo.io.bind({

url: the_url,

formNode: dojo.byId("wc-form"),

load: function(type, data, evt){ printResult(data); },

mimetype: "text/plain"

});

}

break;

case 38: // this is the arrow up

if (history_pointer > 0) {

history_pointer--;

document.getElementById('wc-command').value = commands_history[history_pointer];

}

break;

case 40: // this is the arrow down

if (history_pointer < commands_history.length - 1 ) {

history_pointer++;

document.getElementById('wc-command').value = commands_history[history_pointer];

}

break;

default:

break;

}

}

var commands_history = new Array();

var history_pointer;


从代码初步看来,它可以记录用户从控制台输入的命令记录,输入命令后按下"回车键"

就可以向控制台发送命令并返回请求结果。

笔者模拟了一次想控制台发送命令的操作,先后输入request,#request,

com.opensymphony.xwork2.ActionContext.locale

执行结果如下


此处可以输入任何ongl表达式,因为他就是ongl为struts2开放出的调试控制台。

从控制台输入的命令参数等同于<s:debug/>的效果,

笔者刚刚的com.opensymphony.xwork2.ActionContext.locale参数就是从页面复制过来的。

你可以把鼠标输入控制台,用键盘的上下翻页功能查找输入过的命令,按下回车键即可向控制台发出命令。

文件上传页面(fileupload.jsp)源代码:

<%@ page language="java" contentType="text/html; charset=GBK"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<s:set name="debugMode" value="'console'"/>
<html>
<head>

<head>
<body>

<s:form action="fileUpload.action" method="post" enctype="multipart/form-data">
<s:if test="debugMode!=''">
<input type="hidden" name="debug" value="<s:property value="debugMode"/>"/>
</s:if>
<s:file name="doc" label="File"/>
<s:submit/>
</s:form>
<s:debug/>
</body>

<html>

上传成功页面(upload_success.jsp)源代码:

<%@ page language="java" contentType="text/html; charset=GBK"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<head>
<body>
<s:debug/>
<body>
<html>

==============

2 debug=browser(支持IE,火狐,Chrome)

官方文档Debugging Struts提及到一个browser调试模式,按照官方文档的说明把struts2-config-browser-plugin-x.x.x.x.jar放入lib目录。

笔者访问了debug-browser.action的页面代码如下:

<%@ page language="java" contentType="text/html; charset=GBK"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib uri="/struts-dojo-tags" prefix="sx"%>
<html>
<head>
<sx:head/>
<head>
<body>
<s:debug/>
</body>

<html>

但笔者以常规方式访问http://localhost:8080/uploadFile/process.action的时候打印出了<s:debug/>标签的调试信息,如下图所示


当使用参数访问(地址http://localhost:8080/uploadFile/process.action?debug=browser)的时候如下所示:

效果和官方的图示一致。

上图中Expand是展开(节点)的意思。

笔者展开了servlet内置对象application的内容,如图所示:

从此图可以了解到笔者演示环境的Servlet容器版本是“Apache Tomcat/6.0.26”。聪明的你应该能明白笔者想要告诉你的是什么了~~

从访问http://localhost:8080/uploadFile/process.action和http://localhost:8080/uploadFile/process.action?debug=browser

获得视图分析:加了参数后process.action页面的debug标签信息全部消失了。

总结出debug=browser是为了方便开发者了解当前action的对象信息,

与通过Eclipse等ide调试视图方式查看当前action变量信息具有异曲同工之妙。

process.action代码如下:

<%@ page language="java" contentType="text/html; charset=GBK"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib uri="/struts-dojo-tags" prefix="sx"%>
<html>
<head>
<sx:head/>
<head>
<body>
<s:debug/>
</body>

<html>

3 debug=xml(IE、 火狐、Chrome均可用)

访问http://localhost:8080/uploadFile/process.action?debug=xml可获得以xml方式展现的调试信息,具体用途不详。

4 其他调试技巧:

1 struts.xml

<constant name="struts.configuration.xml.reload" value="true"/>

热加载被修改过后的 struts2 配置文件(通常是xml文件)具体用法请查阅网络相关文章

2 JavaRebel 提交Java开发效率

3 tomcat页面编译异常 调试技巧 jsp_precompile=true