SUN社区-Struts专题
Struts里的html:Cancel标签是在Form中经常运用的一个标签,主要功能就是cancel当前Form,一般写法如下:
=======================================================
<html:cancel>
<bean:message key="createuser.cancelbutton"/>
</html:cancel>
=======================================================
这个标签将生成如下的HTML代码:
<input type="submit" name="org.apache.struts.taglib.html.CANCEL" value="返回" onclick="bCancel=true;">
bCancel=true是一段javascript,bCancel是在使用Struts的Validator时,Struts自动为我们加的一段Javascript代码里的一个变量
这段Javascript简略摘要如下:
=======================================================
<script type="text/javascript" language="Javascript1.1">
<!-- Begin
var bCancel = false;
function validateCreateUserForm(form) {
if (bCancel)
return true;
else
return validateMaxLength(form) && validateRequired(form) && validateMinLength(form);
}
。。。以下省略
=======================================================
由上可以看到,这个bCancel=true时,Javascript将自动将表单提交(return true),也就是说,如果我们在后台Action的代码里
没有对这个Cancel动作写特定代码的话,这个Cancel标签产生的效果和submit按钮产生的动作完全一致!!(因为这个按钮的
type也等于submit)
这一点需要非常的注意!所以,一般来说,我们在Action类的execute方法里面会加上如下的一段代码来处理这个Cancel动作:
=======================================================
// Was this transaction cancelled?
if (isCancelled(request)) {
return (mapping.findForward("createusersuccess"));
}
=======================================================
有了以上的代码,Cancel动作就有了相应的处理代码,转到相关的页面了。
本来事情已经解决,但本着对Struts源码研究的精神,我们还需要对以上代码研究一下
OK,让我们来看一下isCancelled这个方法在什么地方被定义了,内容是什么?
首先发现,这个方法被定义在Action类里面,代码如下:
=======================================================
/**
* <p>Returns <code>true</code> if the current form's cancel button was
* pressed. This method will check if the <code>Globals.CANCEL_KEY</code>
* request attribute has been set, which normally occurs if the cancel
* button generated by <strong>CancelTag</strong> was pressed by the user
* in the current request. If <code>true</code>, validation performed
* by an <strong>ActionForm</strong>'s <code>validate()</code> method
* will have been skipped by the controller servlet.</p>
*
* @param request The servlet request we are processing
* @see org.apache.struts.taglib.html.CancelTag
*/
protected boolean isCancelled(HttpServletRequest request) {
return (request.getAttribute(Globals.CANCEL_KEY) != null);
}
=======================================================
哦,原来是在request对象中查找Globals.CANCEL_KEY这个key值是否绑定了一个对象,如果是,那么就代表按下Cancel按钮后,
Struts会在request对象中绑定一个对象,并以这个key值来命名
那Struts是在什么地方绑定了这个对象呢?很自然的,让我们从头找起
从ActionServlet的process方法开始找起,历经多次方法调用,终于找到了根源,原来是在RequestProcessor.java中,代码如下:
=======================================================
/**
* <p>Process an <code>HttpServletRequest</code> and create the
* corresponding <code>HttpServletResponse</code>.</p>
*
* @param request The servlet request we are processing
* @param response The servlet response we are creating
*
* @exception IOException if an input/output error occurs
* @exception ServletException if a processing exception occurs
*/
public void process(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
//。。。省略代码若干
// Process any ActionForm bean related to this request
ActionForm form = processActionForm(request, response, mapping);
//答案就在这个processPopulate方法中
processPopulate(request, response, form, mapping);
if (!processValidate(request, response, form, mapping)) {
return;
}
/**
* Populate the properties of the specified ActionForm instance from
* the request parameters included with this request. In addition,
* request attribute <code>Globals.CANCEL_KEY</code> will be set if
* the request was submitted with a button created by
* <code>CancelTag</code>.
*
* @param request The servlet request we are processing
* @param response The servlet response we are creating
* @param form The ActionForm instance we are populating
* @param mapping The ActionMapping we are using
*
* @exception ServletException if thrown by RequestUtils.populate()
*/
protected void processPopulate(HttpServletRequest request,
HttpServletResponse response,
ActionForm form,
ActionMapping mapping)
throws ServletException {
if (form == null) {
return;
}
// Populate the bean properties of this ActionForm instance
if (log.isDebugEnabled()) {
log.debug(" Populating bean properties from this request");
}
form.setServlet(this.servlet);
form.reset(mapping, request);
if (mapping.getMultipartClass() != null) {
request.setAttribute(Globals.MULTIPART_KEY,
mapping.getMultipartClass());
}
RequestUtils.populate(form, mapping.getPrefix(), mapping.getSuffix(),
request);
// Set the cancellation request attribute if appropriate
if ((request.getParameter(Constants.CANCEL_PROPERTY) != null) ||
(request.getParameter(Constants.CANCEL_PROPERTY_X) != null)) {
request.setAttribute(Globals.CANCEL_KEY, Boolean.TRUE);
}
}
=======================================================
OK,看最后几行代码,Struts从request中取得Constants.CANCEL_PROPERTY这个参数,如果这个参数不为空,那么他就将
TRUE这个对象以Globals.CANCEL_KEY为key值,放到了request对象中
至于这个Constants.CANCEL_PROPERTY这个值是什么,现在都可以猜到了,显然就是html:Cancel这个标签生成的HTML代码
中,Cancel这个按钮的名称嘛!查了一下,果然是:
<input type="submit" name="org.apache.struts.taglib.html.CANCEL" value="返回" onclick="bCancel=true;">
而Constants.CANCEL_PROPERTY这个值就是org.apache.struts.taglib.html.CANCEL
至此,真相大白,看来以后使用html:Cancel这个标签要小心了 :)
--------------------------------------------------------------------------------
Java来自于Sun公司的一个叫Green的项目,其原先的目的是为家用消费电子产品开发一个分布式代码系统,这样我们可以把E-mail发给电冰箱、电视机等家用电器,对它们进行控制,和它们进行信息交流。
开始,准备采用C++,但C++太复杂,安全性差,最后基于C++开发了一种新的语言Oak(Java的前身),Oak是一种用于网络的精巧而安全的语言,Sun公司曾以此投标一个交互式电视项目,但结果被SGI打败。
可怜的Oak几乎无家可归,恰巧这时Mark Ardreesen开发的Mosaic和Netscape启发了Oak项目组成员,他们用Java编 制 了HotJava浏 览器,得到了Sun公司首席执行官Scott McNealy的支持,触发了Java进 军Internet。
Java的取名也有一则趣闻,有一天,几 位Java成员组的会员正在讨论给这个新的语言取什么名字,当时他们正在咖啡馆喝着Java(爪哇)咖啡,有一个人灵机一动说就叫Java怎 样,得到了其他人的赞赏,于是,Java这个名字就这样传开了。
Struts中的html:link标签是一个很常用的标签,其功能也比较明显:生成<a href="yoururl"></a>这样的HTML代码
在刚开始接触这个标签的时候,我知道这个标签可以这样用:
=========================================================
<html:link page="/html-link.do" paramId="stringProperty" paramName="newValue">
String via paramId and paramName
</html:link>
=========================================================
这个newValue一般就是一个String类型的变量,上述的代码将生成这样的HTML代码:
<a href="http://serveriportno/projectcontextname/html-link.do?stringProperty=$newValue">String via paramId and paramName</a>
其中$newValue表示的是newValue这个变量的值
也可以这样用:
=========================================================
<html:link page="/html-link.do"
paramId="booleanProperty"
paramName="testbean" paramProperty="booleanProperty">
Boolean via paramId, paramName, and paramValue
</html:link>
=========================================================
这里多了一个paramProperty属性,这里的name属性和property属性和bean:write标签一样,一般来说name的值是一个被绑定在
request、session、application或page范围内的对象的绑定key值,Property属性的值则是这个对象的一个成员变量的名称
使用了name和Property之后,struts将在上述四种范围内,以name变量值为key查找对象,并将对象读出后,取出Property定义的
成员变量的值。
所以以上的标签被转化成HTML代码之后如下:
<a href="http://serveriportno/projectcontextname/html-link.do?booleanProperty=$testbean.booleanProperty"> Boolean via paramId, paramName, and paramValue</a>
OK,了解到这里之后,感觉到一丝遗憾,因为如果只有paramId,paramName,paramProperty这三个属性的话,那么对于html:link标签来说,就永远只能
自定义一个参数,如果需要定义多个参数,如http://serveriportno/projectcontextname/html-link.do?a=3&b=5&y=9这样的链接URL来说,就无能为力了
如果要定义多个参数,就需要手动拼URL,定义在page属性里面,这就失去了使用标签的意义了。
在此情况下,怀着失望的心情,打开了Struts的源码,开始查看这个标签的源码,忽然发现,这个标签完全是可以自定义多个参数的,关键就在name属性上
首先来看一段如何自定义多个参数的代码,如下:
=========================================================
<%
java.util.HashMap newValues = new java.util.HashMap();
newValues.put("floatProperty", new Float(444.0));
newValues.put("intProperty", new Integer(555));
newValues.put("stringArray", new String[]
{ "Value1", "Value2", "Value3" });
pageContext.setAttribute("newValues", newValues);
%>
<html:link page="/html-link.do" name="newValues">
Float, int, and stringArray via name (Map)
</html:link>
=========================================================
原来使用name属性就可以将一个Map类型的变量放入request、session、application、page四种范围的容器内,然后取个key值,填在name属性里就可以了
Struts将自动读取这个Map类型对象的所有key和key所定义的对象,然后自动生成HTML代码
好比上述的例子,将生成如下的HTML代码:
<a href="http://serveriportno/projectcontextname/html-link.do?stringArray=Value1&stringArray=Value2&stringArray=Value3&floatProperty=444.0&intProperty=555">
Float, int, and stringArray via name (Map)</a>
愿望终于达到了,也充分证明了Struts的实力,但在这段实现中,查找name定义的对象这点在前面几次的源码剖析中已经很明确了(使用的是TagUtils.java中的lookup方法)
但得到这个对象后,Struts是如何读出key值集合和key值对应的对象的呢?代码在此:
=========================================================
// Add the required request parameters
boolean question = temp.indexOf('?') >= 0;
//这里的params变量就是我们输入的Map类型的对象
//就是这句代码可以得到key值集合
Iterator keys = params.keySet().iterator();
while (keys.hasNext()) {
String key = (String) keys.next();
Object value = params.get(key);
if (value == null) {
if (!question) {
url.append('?');
question = true;
} else {
url.append(separator);
}
url.append(this.encodeURL(key));
url.append('='); // Interpret null as "no value"
} else if (value instanceof String) {
if (!question) {
url.append('?');
question = true;
} else {
url.append(separator);
}
url.append(this.encodeURL(key));
url.append('=');
url.append(this.encodeURL((String) value));
} else if (value instanceof String[]) {
String values[] = (String[]) value;
for (int i = 0; i < values.length; i++) {
if (!question) {
url.append('?');
question = true;
} else {
url.append(separator);
}
url.append(this.encodeURL(key));
url.append('=');
url.append(this.encodeURL(values[i]));
}
} else /* Convert other objects to a string */ {
if (!question) {
url.append('?');
question = true;
} else {
url.append(separator);
}
url.append(this.encodeURL(key));
url.append('=');
url.append(this.encodeURL(value.toString()));
}
}
=========================================================
已经不用再做多余的解释了,代码已经说明一切。
其实在PropertyMessageResource.java中,也有类似的代码,就是将property文件读出的一段代码,在ActionServlet初始化时被调用
在bean:message标签研究一文中可以找到这段代码
最后想说的是,Struts的源码写的相当的优秀,不停的学习对提高自身的水平是很有裨益的
--------------------------------------------------------------------------------
Java来自于Sun公司的一个叫Green的项目,其原先的目的是为家用消费电子产品开发一个分布式代码系统,这样我们可以把E-mail发给电冰箱、电视机等家用电器,对它们进行控制,和它们进行信息交流。
开始,准备采用C++,但C++太复杂,安全性差,最后基于C++开发了一种新的语言Oak(Java的前身),Oak是一种用于网络的精巧而安全的语言,Sun公司曾以此投标一个交互式电视项目,但结果被SGI打败。
可怜的Oak几乎无家可归,恰巧这时Mark Ardreesen开发的Mosaic和Netscape启发了Oak项目组成员,他们用Java编 制 了HotJava浏 览器,得到了Sun公司首席执行官Scott McNealy的支持,触发了Java进 军Internet。
Java的取名也有一则趣闻,有一天,几 位Java成员组的会员正在讨论给这个新的语言取什么名字,当时他们正在咖啡馆喝着Java(爪哇)咖啡,有一个人灵机一动说就叫Java怎 样,得到了其他人的赞赏,于是,Java这个名字就这样传开了。
logic:Iterator标签(以下简称“该标签”)是Struts里非常常用的一个标签,其作用在于循环显示给定容器对象中的值
如此常用的标签,其源代码当然需要拿出来研究一下,以下列举几条研究成果:
1、该标签内部使用Collection来表示给定的容器,所有的给定容器对象(如ArrayList,Map等)都会被其转化成为Collection
2、该标签自己维护循环索引
3、该标签常见的几个属性如下:
name、property、scope、id
4、结合以上标签,给出一段源代码来解释其工作的机理
这段源代码中,一开始就可以看到这样一句:
collection = TagUtils.getInstance().lookup(pageContext, name, property, scope);
这局代码在之前的几次Struts源码分析中已经分析到了,作用如下:
1、如果property的值为null,那么
在scope定义的范围内(即request、session、application、page)查找以name变量值命名的对象(返回值是一个Object,然后转化成Collection)
2、如果property的值不为null,那么
做完1步骤的事情后,她将调用org.apache.commons.beanutils.PropertyUtils类中的getProperty方法,得到目标对象,转化成Collection类型
所以,我们在编码时,可以自己构建一个ArrayList,然后放到session或request范围内,然后在logic:Iterator标签中可以这样定义:
name=对象在session或request中绑定的key值
property可以不写(因为没有再将这个ArrayList包装进一个对象)
scope也可以不写(不写将发生pageContext.findAttribute方法调用,在四种scope中依次寻找),或写session或request
之后的代码也很好理解,Struts得到Collection之后,动态判断其进一步的类型,然后调用相关方法获得Iterator
最后,Struts使用得到的Iterator对象,开始对Collection进行循环,将Collection中每个元素对象取出,以id变量值绑定到
pageContext上。看到这里,心急的人可能会问,怎么就这么结束了么?她不将元素对象取出,然后显示么?
=========================================================
public int doStartTag() throws JspException {
// Acquire the collection we are going to iterate over
Object collection = this.collection;
if (collection == null) {
collection = TagUtils.getInstance().lookup(pageContext, name, property, scope);
}
if (collection == null) {
JspException e = new JspException(messages.getMessage("iterate.collection"));
TagUtils.getInstance().saveException(pageContext, e);
throw e;
}
// Construct an iterator for this collection
if (collection.getClass().isArray()) {
try {
// If we're lucky, it is an array of objects
// that we can iterate over with no copying
iterator = Arrays.asList((Object[]) collection).iterator();
} catch (ClassCastException e) {
// Rats -- it is an array of primitives
int length = Array.getLength(collection);
ArrayList c = new ArrayList(length);
for (int i = 0; i < length; i++) {
c.add(Array.get(collection, i));
}
iterator = c.iterator();
}
} else if (collection instanceof Collection) {
iterator = ((Collection) collection).iterator();
} else if (collection instanceof Iterator) {
iterator = (Iterator) collection;
} else if (collection instanceof Map) {
iterator = ((Map) collection).entrySet().iterator();
} else if (collection instanceof Enumeration) {
iterator = IteratorUtils.asIterator((Enumeration) collection);
} else {
JspException e = new JspException(messages.getMessage("iterate.iterator"));
TagUtils.getInstance().saveException(pageContext, e);
throw e;
}
// Calculate the starting offset
if (offset == null) {
offsetValue = 0;
} else {
try {
offsetValue = Integer.parseInt(offset);
} catch (NumberFormatException e) {
Integer offsetObject = (Integer) TagUtils.getInstance().lookup(pageContext, offset, null);
if (offsetObject == null) {
offsetValue = 0;
} else {
offsetValue = offsetObject.intValue();
}
}
}
if (offsetValue < 0) {
offsetValue = 0;
}
// Calculate the rendering length
if (length == null) {
lengthValue = 0;
} else {
try {
lengthValue = Integer.parseInt(length);
} catch (NumberFormatException e) {
Integer lengthObject = (Integer) TagUtils.getInstance().lookup(pageContext, length, null);
if (lengthObject == null) {
lengthValue = 0;
} else {
lengthValue = lengthObject.intValue();
}
}
}
if (lengthValue < 0) {
lengthValue = 0;
}
lengthCount = 0;
// Skip the leading elements up to the starting offset
for (int i = 0; i < offsetValue; i++) {
if (iterator.hasNext()) {
iterator.next();
}
}
// Store the first value and evaluate, or skip the body if none
if (iterator.hasNext()) {
Object element = iterator.next();
if (element == null) {
pageContext.removeAttribute(id);
} else {
pageContext.setAttribute(id, element);
}
lengthCount++;
started = true;
if (indexId != null) {
pageContext.setAttribute(indexId, new Integer(getIndex()));
}
return (EVAL_BODY_TAG);
} else {
return (SKIP_BODY);
}
}
=========================================================
不急,其实该标签做到这一步已经可以了,因为在我们使用logic:Iterator标签的同时,一般还会使用bean:write标签,如下一段:
=========================================================
<logic:iterate id="myuserinfo" name="browseresult" scope="request">
<tr>
<td align="center">
<bean:write name="myuserinfo" property="username" filter="true"/>
</td>
<td align="center">
<bean:write name="myuserinfo" property="userdesc" filter="true"/>
</td>
</tr>
</logic:iterate>
=========================================================
所以,bean:write这个标签将会到pageContext里面,以id变量值为key值,查找这个元素对象,然后将其属性(property属性定义)取出、显示。
OK,至此,应该已经很清楚了。最后还要一提id这个属性,很多example中可能会对我们有误解,认为id属性的定义也对应一个实体类(实体bean),
其实不然,通过以上的源代码,可以看到,id这个属性只是一个key而已,Struts用这个值来将Collection中的每个元素对象绑定到pageContext
里面去,所以,对于id属性的值,完全可以自定义,只要遵守一条规则:
在logic:Iterator标签中定义的id属性值必须和下面bean:write标签中的name属性的值一致!
--------------------------------------------------------------------------------
Java来自于Sun公司的一个叫Green的项目,其原先的目的是为家用消费电子产品开发一个分布式代码系统,这样我们可以把E-mail发给电冰箱、电视机等家用电器,对它们进行控制,和它们进行信息交流。
开始,准备采用C++,但C++太复杂,安全性差,最后基于C++开发了一种新的语言Oak(Java的前身),Oak是一种用于网络的精巧而安全的语言,Sun公司曾以此投标一个交互式电视项目,但结果被SGI打败。
可怜的Oak几乎无家可归,恰巧这时Mark Ardreesen开发的Mosaic和Netscape启发了Oak项目组成员,他们用Java编 制 了HotJava浏 览器,得到了Sun公司首席执行官Scott McNealy的支持,触发了Java进 军Internet。
Java的取名也有一则趣闻,有一天,几 位Java成员组的会员正在讨论给这个新的语言取什么名字,当时他们正在咖啡馆喝着Java(爪哇)咖啡,有一个人灵机一动说就叫Java怎 样,得到了其他人的赞赏,于是,Java这个名字就这样传开了。
初学Struts,写了一个很简单的应用,主要功能和页面如下:
1、首页显示一个“添加新用户”的链接,点击该链接出发一个forward动作,页面导向到添加用户的jsp页面
2、添加用户的jsp页面中,可供用户输入“用户名”和“用户描述”两项
3、用户输入完毕,将做输入数据合法性检查,检查通过,将输入信息保存进入文件(使用了Properties类),然后返回首页;检查失败返回添加用户页面
4、数据合法性检查分成两块,第一部分检查条件使用Struts的Validator,检查条件配置在Validator.xml中;第二部分检查放在ActionForm中,
检查失败将错误信息置入ActionErrors中,然后返回到添加用户的页面并显示错误信息。
JSP页面、ActionForm和Action类的代码书写都参照了struts-example应用,所以这里代码不再列举,请看附件中的代码包
这里值得一提的是,在开发过程中,碰到了一个小问题,正是由于该问题,才导致查看Struts源码,刨根问底的查找错误原因的过程
该错误发生在Struts的配置文件中,首先将错误的配置文件列出如下:
====================================================
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<!-- ======================================== Form Bean Definitions -->
<form-beans>
<form-bean
name="CreateUserForm"
type="com.zchome.CreateUserForm"/>
</form-beans>
<!-- ================================= Global Exception Definitions -->
<global-exceptions>
</global-exceptions>
<!-- =================================== Global Forward Definitions -->
<global-forwards>
<!-- Default forward to "Welcome" action -->
<!-- Demonstrates using index.jsp to forward -->
<forward name="welcome" path="/Welcome.do"/>
</global-forwards>
<!-- =================================== Action Mapping Definitions -->
<action-mappings>
<!-- Default "Welcome" action -->
<!-- Forwards to Welcome.jsp -->
<action
path="/Welcome"
type="org.apache.struts.actions.ForwardAction"
parameter="/jsp/Welcome.jsp"/>
<action path="/createuserpage" forward="/jsp/createuser.jsp">
</action>
<action
path="/docreateuser"
type="com.zchome.CreateUserAction"
name="CreateUserForm"
scope="request"
input="createuser">
<forward name="createusersuccess" path="/jsp/Welcome.jsp"/>
<forward name="createuser" path="/jsp/createuser.jsp"/>
</action>
</action-mappings>
<!-- ===================================== Controller Configuration -->
<controller>
<set-property property="processorClass" value="org.apache.struts.tiles.TilesRequestProcessor"/>
</controller>
<!-- ================================ Message Resources Definitions -->
<message-resources parameter="resources.application"/>
<!-- ======================================= Plug Ins Configuration -->
<!-- ========== Tiles plugin =================== -->
<!-- -->
<!--
This plugin initialize Tiles definition factory. This later can takes some
parameters explained here after. The plugin first read parameters from web.xml, then
overload them with parameters defined here. All parameters are optional.
The plugin should be declared in each struts-config file.
- definitions-config: (optional)
Specify configuration file names. There can be several comma
separated file names (default: ?? )
- moduleAware: (optional - struts1.1)
Specify if the Tiles definition factory is module aware. If true (default),
there will be one factory for each Struts module.
If false, there will be one common factory for all module. In this later case,
it is still needed to declare one plugin per module. The factory will be
initialized with parameters found in the first initialized plugin (generally the
one associated with the default module).
true : One factory per module. (default)
false : one single shared factory for all modules
- definitions-parser-validate: (optional)
Specify if xml parser should validate the Tiles configuration file.
true : validate. DTD should be specified in file header. (default)
false : no validation
Paths found in Tiles definitions are relative to the main context.
-->
<!-- comment following if struts1.0.x -->
<plug-in className="org.apache.struts.tiles.TilesPlugin" >
<set-property property="definitions-config"
value="/WEB-INF/tiles-defs.xml" />
<set-property property="moduleAware" value="true" />
<set-property property="definitions-parser-validate" value="true" />
</plug-in>
<!-- end comment if struts1.0.x -->
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property
property="pathnames"
value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
</plug-in>
</struts-config>
====================================================
首先描述一下系统的出错背景:
1、从首页点击链接来到添加用户的页面 正常
2、在添加用户页面中输入Vlidator.xml文件中定义的错误数据,弹出Javascript对话框,提示出错 正常
3、在添加用户页面中输入合法数据,数据保存进入文件并重定向到首页 正常
4、在添加用户页面中输入ActionForm中定义的非法数据,系统应返回到添加用户的页面 出错!!!
OK,来着重看这个添加动作的定义,如下:
====================================================
<action
path="/docreateuser"
type="com.zchome.CreateUserAction"
name="CreateUserForm"
scope="request"
input="createuser">
<forward name="createusersuccess" path="/jsp/Welcome.jsp"/>
<forward name="createuser" path="/jsp/createuser.jsp"/>
</action>
====================================================
从以上的定义可以看出,如果Validate验证出错,Struts应该为我们重定向到input域所定义的uri,即/jsp/createuser.jsp
看起来应该没有问题,再来看看出错信息,如下:
====================================================
java.lang.IllegalArgumentException: Path createuser does not start with a "/" character
at org.apache.catalina.core.ApplicationContext.getRequestDispatcher(ApplicationContext.java:1179)
at org.apache.catalina.core.ApplicationContextFacade.getRequestDispatcher(ApplicationContextFacade.java:174)
at org.apache.struts.action.RequestProcessor.doForward(RequestProcessor.java:1062)
at org.apache.struts.tiles.TilesRequestProcessor.doForward(TilesRequestProcessor.java:274)
at org.apache.struts.action.RequestProcessor.internalModuleRelativeForward(RequestProcessor.java:1012)
at org.apache.struts.tiles.TilesRequestProcessor.internalModuleRelativeForward(TilesRequestProcessor.java:345)
at org.apache.struts.action.RequestProcessor.processValidate(RequestProcessor.java:980)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:255)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
。。。以下省略。。。
====================================================
出错信息清楚的说明,“createuser”这个path应该以“/”字符开头
为定位这个错误,从以上错误信息,开始打开Struts的源码RequestProcessor.java进行研究,首先来到这一段:
====================================================
public class RequestProcessor {
。。。。。。
protected boolean processValidate(HttpServletRequest request,
HttpServletResponse response,
ActionForm form,
ActionMapping mapping)
throws IOException, ServletException {
if (form == null) {
return (true);
}
// Was this request cancelled?
if (request.getAttribute(Globals.CANCEL_KEY) != null) {
if (log.isDebugEnabled()) {
log.debug(" Cancelled transaction, skipping validation");
}
return (true);
}
// Has validation been turned off for this mapping?
if (!mapping.getValidate()) {
return (true);
}
// Call the form bean's validation method
if (log.isDebugEnabled()) {
log.debug(" Validating input form properties");
}
ActionMessages errors = form.validate(mapping, request);
if ((errors == null) || errors.isEmpty()) {
if (log.isTraceEnabled()) {
log.trace(" No errors detected, accepting input");
}
return (true);
}
// Special handling for multipart request
if (form.getMultipartRequestHandler() != null) {
if (log.isTraceEnabled()) {
log.trace(" Rolling back multipart request");
}
form.getMultipartRequestHandler().rollback();
}
// Has an input form been specified for this mapping?
String input = mapping.getInput();
if (input == null) {
if (log.isTraceEnabled()) {
log.trace(" Validation failed but no input form available");
}
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
getInternal().getMessage("noInput",
mapping.getPath()));
return (false);
}
// Save our error messages and return to the input form if possible
if (log.isDebugEnabled()) {
log.debug(" Validation failed, returning to '" + input + "'");
}
request.setAttribute(Globals.ERROR_KEY, errors);
if (moduleConfig.getControllerConfig().getInputForward()) {
ForwardConfig forward = mapping.findForward(input);
processForwardConfig( request, response, forward);
} else {
internalModuleRelativeForward(input, request, response);
}
return (false);
}
====================================================
在出错信息中,提到了internalModuleRelativeForward这个方法,所以着重看以上代码的最后几行,可以看到,如果
moduleConfig.getControllerConfig().getInputForward()这个方法返回了false,那么internalModuleRelativeForward
这个方法将被调用。inputForward是什么?ModuleConfig是管理所有配置信息的一个manager类,那么moduleConfig.getControllerConfig()
这个方法返回的肯定是ControllerConfig这个类的一个实例,那么inputForward肯定是ControllerConfig类的一个成员变量了
再看看struts-config.xml,里面有<controller>这个标签,初步猜测ControllerConfig应该是读取这个标签的一个配置类
而<controller>这个标签应该定义了ActionServlet作为Controller的一些行为!
OK,再来看ControllerConfig这个类中有关inputForward这个成员变量的一些代码,如下:
====================================================
/**
* <p>Should the <code>input</code> property of {@link ActionConfig}
* instances associated with this module be treated as the
* name of a corresponding {@link ForwardConfig}. A <code>false</code>
* value treats them as a module-relative path (consistent
* with the hard coded behavior of earlier versions of Struts.</p>
*
* @since Struts 1.1
*/
protected boolean inputForward = false;
public boolean getInputForward() {
return (this.inputForward);
}
public void setInputForward(boolean inputForward) {
this.inputForward = inputForward;
}
====================================================
开始有点明白了,原来inputForward这个属性默认值是false,那么由于没有配置这个属性,那么上述的那个方法
moduleConfig.getControllerConfig().getInputForward()自然就返回false了,Bingo!
那么重点就转移到了internalModuleRelativeForward这个方法了,看这个方法的源代码,如下:
====================================================
protected void internalModuleRelativeForward(
String uri,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
// Construct a request dispatcher for the specified path
uri = moduleConfig.getPrefix() + uri;
// Delegate the processing of this request
// FIXME - exception handling?
if (log.isDebugEnabled()) {
log.debug(" Delegating via forward to '" + uri + "'");
}
doForward(uri, request, response);
}
protected void doForward(
String uri,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
// Unwrap the multipart request, if there is one.
if (request instanceof MultipartRequestWrapper) {
request = ((MultipartRequestWrapper) request).getRequest();
}
RequestDispatcher rd = getServletContext().getRequestDispatcher(uri);
if (rd == null) {
response.sendError(
HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
getInternal().getMessage("requestDispatcher", uri));
return;
}
rd.forward(request, response);
}
====================================================
从上可以看到,这个方法是将uri = moduleConfig.getPrefix() + uri;这个东东传给了doForward方法
而doForward这个方法又调用了javax.servlet.ServletContext的方法getRequestDispatcher这个方法
既然出错信息中是路径出了问题,那么看来这个参数uri非常的重要,极有可能就是这个uri发生了错误导致了出错
OK,开始剖析这个uri,从头开始看,这个uri是这样被赋值的:
uri = moduleConfig.getPrefix() + uri
1、moduleConfig.getPrefix()这个方法返回的应该是""
(这个请看ActionServlet的Init方法,如果在web.xml文件中定义ActionServlet的时候,给定了一些init-params,那么这个prefix就有可能
不为空,这里不再列举了)
2、代码右边的这个uri是从processValidate这个方法中定义的input,如下:
String input = mapping.getInput();
这个input应该是struts-config.xml文件中定义的那个action的input,也就是“createuser”,如果Struts将其做了进一步的解析,那么这个
input应该进一步被转化成为“/jsp/createuser.jsp”
好,到此为止,可以看到,这个uri不是“createuser”,那就是“/jsp/createuser.jsp”,再来看getRequestDispatcher这个方法的定义,
翻开Servlet的API文档,可以看到如下一段话:
====================================================
public RequestDispatcher
getRequestDispatcher(java.lang.String path)Returns a RequestDispatcher object that acts as a wrapper
for the resource located at the given path. A RequestDispatcher object can be used to forward a request
to the resource or to include the resource in a response. The resource can be dynamic or static.
The pathname must begin with a "/" and is interpreted as relative to the current context root.
Use getContext to obtain a RequestDispatcher for resources in foreign contexts. This method returns null
if the ServletContext cannot return a RequestDispatcher.
====================================================
终于有拨云见日的感觉了,因为这段话和出错信息实在是太一致了!由上面这段话,我们可以断定,uri这个变量的值
肯定是“createuser”,而不是我们所希望的“/jsp/createuser.jsp”。为什么会这样呢?显然是struts-config.xml中配置
有些还是不对,或是缺了点什么。想到这里,很自然的就联想到上面所提到的InputForward这个配置项了,因为从字面意思上
看来,这个配置项的用处就应该是将input的值解析成forward中对应的值,而且在ControllerConfig中,这个变量默认值是
false,所以猜测将其改成true是不是就可以了呢?
为了寻找答案,再次翻开struts-example(因为这个例子中的action也定义了input),终于找到了答案,和之前猜测的果然
十分吻合,如下:
====================================================
<controller>
<set-property property="inputForward" value="true"/>
</controller>
====================================================
至此,问题解决,正确的action配置可以是如下两种:
====================================================
1、不使用inputForward
<action
path="/docreateuser"
type="com.zchome.CreateUserAction"
name="CreateUserForm"
scope="request"
input="/jsp/createuser.jsp">
<forward name="createusersuccess" path="/jsp/Welcome.jsp"/>
</action>
2、使用InputForward
<action
path="/docreateuser"
type="com.zchome.CreateUserAction"
name="CreateUserForm"
scope="request"
input="createuser">
<forward name="createusersuccess" path="/jsp/Welcome.jsp"/>
<forward name="createuser" path="/jsp/createuser.jsp"/>
</action>
<controller>
<set-property property="inputForward" value="true"/>
<set-property property="processorClass" value="org.apache.struts.tiles.TilesRequestProcessor"/>
</controller>
====================================================
而且,从问题的定位过程中,还学到了一招,就是javax.servlet.RequestDispatcher:
RequestDispatcher rd = getServletContext().getRequestDispatcher(uri);
if (rd == null) {
response.sendError(
HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
getInternal().getMessage("requestDispatcher", uri));
return;
}
rd.forward(request, response);
以后再做页面重定向,只要给定相对的uri就可以了,再也不用写上一层的虚拟目录名或自己拼URL了
--------------------------------------------------------------------------------
Java来自于Sun公司的一个叫Green的项目,其原先的目的是为家用消费电子产品开发一个分布式代码系统,这样我们可以把E-mail发给电冰箱、电视机等家用电器,对它们进行控制,和它们进行信息交流。
开始,准备采用C++,但C++太复杂,安全性差,最后基于C++开发了一种新的语言Oak(Java的前身),Oak是一种用于网络的精巧而安全的语言,Sun公司曾以此投标一个交互式电视项目,但结果被SGI打败。
可怜的Oak几乎无家可归,恰巧这时Mark Ardreesen开发的Mosaic和Netscape启发了Oak项目组成员,他们用Java编 制 了HotJava浏 览器,得到了Sun公司首席执行官Scott McNealy的支持,触发了Java进 军Internet。
Java的取名也有一则趣闻,有一天,几 位Java成员组的会员正在讨论给这个新的语言取什么名字,当时他们正在咖啡馆喝着Java(爪哇)咖啡,有一个人灵机一动说就叫Java怎 样,得到了其他人的赞赏,于是,Java这个名字就这样传开了。
Struts中非常常用的有这样的一个标签:
<bean:message key="welcome.title"/>
众所周知,这个标签做的事情是这样的:
访问在struts-config.xml中定义的资源文件,一般是application.properties,一般是这样定义的:
<message-resources parameter="resources.application"/>
根据以上的定义,Struts将到WEB-INF/classes/resource/下去找application.properties文件,这是从以上配置信息
的表面上看起来是这样,但通过查看Struts的源码,可以看到如下的代码:
在org.apache.struts.util.PropertyMessageResources类中,有如下的代码:
通过这段代码,可以看到
A、.properties扩展名是写死在代码中的,所以资源文件必须使用这个扩展名
B、Struts并不是单纯去寻找application.properties文件,而是首先找到application,赋给name变量
然后加上下划线"_",然后再加上localeKey(如zh,en),然后再加上.properties
C、确定了文件名之后,Struts使用了ClassLoader类的getResourceAsStream方法得到了一个InputStream
D、然后Struts使用了java.util.Properties类的load方法,将资源文件中的所有资源读出放到了一个HashMap里面
E、然后Struts就可以根据key值取出不同的message给前台了
// Set up to load the property resource for this locale key, if we can
String name = config.replace('.', '/');
if (localeKey.length() > 0) {
name += "_" + localeKey;
}
name += ".properties";
InputStream is = null;
Properties props = new Properties();
// Load the specified property resource
if (log.isTraceEnabled()) {
log.trace(" Loading resource '" + name + "'");
}
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
if (classLoader == null) {
classLoader = this.getClass().getClassLoader();
}
is = classLoader.getResourceAsStream(name);
if (is != null) {
try {
props.load(is);
} catch (IOException e) {
log.error("loadLocale()", e);
} finally {
try {
is.close();
} catch (IOException e) {
log.error("loadLocale()", e);
}
}
}
D步骤中的load方法可以参看JDK的帮助文档,load方法要求这个资源文件必须以ISO8859编码进行书写方能正确解析
所以,如果我们在资源文件中写入了中文,并在运行时出现了中文编码问题(?出现),那么只需确认您的资源文件
是否是以ISO8859为编码进行书写的即可
另外,Struts在查找资源文件时,首先是按照如上的描述进行$Filename_$Locale.properties文件的查找
如果他找不到,那么他就会用默认的$Filename.properties来找,如果还找不到,那就报错了
这个Struts的查找顺序并不是我的杜撰,有如下Struts源码为证(这个方法是PropertyMessageResources.java中的):
public String getMessage(Locale locale, String key) {
if (log.isDebugEnabled()) {
log.debug("getMessage(" + locale + "," + key + ")");
}
// Initialize variables we will require
String localeKey = localeKey(locale);
String originalKey = messageKey(localeKey, key);
String messageKey = null;
String message = null;
int underscore = 0;
boolean addIt = false; // Add if not found under the original key
// Loop from specific to general Locales looking for this message
while (true) {
// Load this Locale's messages if we have not done so yet
loadLocale(localeKey);
// Check if we have this key for the current locale key
messageKey = messageKey(localeKey, key);
synchronized (messages) {
message = (String) messages.get(messageKey);
if (message != null) {
if (addIt) {
messages.put(originalKey, message);
}
return (message);
}
}
// Strip trailing modifiers to try a more general locale key
addIt = true;
underscore = localeKey.lastIndexOf("_");
if (underscore < 0) {
break;
}
localeKey = localeKey.substring(0, underscore);
}
// Try the default locale if the current locale is different
if (!defaultLocale.equals(locale)) {
localeKey = localeKey(defaultLocale);
messageKey = messageKey(localeKey, key);
loadLocale(localeKey);
synchronized (messages) {
message = (String) messages.get(messageKey);
if (message != null) {
messages.put(originalKey, message);
return (message);
}
}
}
// As a last resort, try the default Locale
===================================这里可以看到Struts最后将localeKey赋空
===================================这样资源文件就是$Filename.properties了
localeKey = "";
messageKey = messageKey(localeKey, key);
===================================loadLocale这个方法将读取资源文件,填入HashMap
===================================这个方法的代码在上面已经列出来了
loadLocale(localeKey);
synchronized (messages) {
message = (String) messages.get(messageKey);
if (message != null) {
messages.put(originalKey, message);
return (message);
}
}
// Return an appropriate error indication
if (returnNull) {
return (null);
} else {
return ("???" + messageKey(locale, key) + "???");
}
}
至于这个$Locale的值是多少,通过很长时间的查找之后,发现了这样一些代码:
在org.apache.struts.util.RequestUtils类中的600多行左右,有这样一个方法:
public static Locale getUserLocale(HttpServletRequest request, String locale) {
Locale userLocale = null;
HttpSession session = request.getSession(false);
if (locale == null) {
locale = Globals.LOCALE_KEY; //这个值是org.apache.struts.action.LOCALE
}
// Only check session if sessions are enabled
if (session != null) {
userLocale = (Locale) session.getAttribute(locale);
}
if (userLocale == null) {
// Returns Locale based on Accept-Language header or the server default
userLocale = request.getLocale();
}
return userLocale;
}
可以看出,Struts将Locale的实例对象放到了session中,但是他什么时候将这个对象放到session里面的,尚未找到
(很多配置在ActionServlet中读取并储存的,因为这个类是第一个启动的类,但这个类中没发现Locale对象的储存)
--------------------------------------------------------------------------------
浙公网安备 33010602011771号