java

位置:IT落伍者 >> java >> 浏览文章

Struts源码研究 - html-Link标签篇


发布日期:2019年09月14日
 
Struts源码研究 - html-Link标签篇

Struts里的html:Cancel标签是在Form中经常运用的一个标签主要功能就是cancel当前Form一般写法如下

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

<html:cancel>

<bean:message key=createusercancelbutton/>

</html:cancel>

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

这个标签将生成如下的HTML代码

<input type=submit name=orgapaclCANCEL value=返回 onclick=bCancel=true;>

bCancel=true是一段javascriptbCancel是在使用Struts的Validator时Struts自动为我们加的一段Javascript代码里的一个变量

这段Javascript简略摘要如下

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

<script type=text/javascript language=Javascript>

<! 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 (mappingfindForward(createusersuccess));

}

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

有了以上的代码Cancel动作就有了相应的处理代码转到相关的页面了

本来事情已经解决但本着对Struts源码研究的精神我们还需要对以上代码研究一下

OK让我们来看一下isCancelled这个方法在什么地方被定义了内容是什么?

首先发现这个方法被定义在Action类里面代码如下

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

/**

* <p>Returns <code>true</code> if the current forms cancel button was

* pressed This method will check if the <code>GlobalsCANCEL_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 orgapaclCancelTag

*/

protected boolean isCancelled(HttpServletRequest request) {

return (requestgetAttribute(GlobalsCANCEL_KEY) != null);

}

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

原来是在request对象中查找GlobalsCANCEL_KEY这个key值是否绑定了一个对象如果是那么就代表按下Cancel按钮后

Struts会在request对象中绑定一个对象并以这个key值来命名

那Struts是在什么地方绑定了这个对象呢?很自然的让我们从头找起

从ActionServlet的process方法开始找起历经多次方法调用终于找到了根源原来是在RequestProcessorjava中代码如下

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

/**

* <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>GlobalsCANCEL_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 RequestUtilspopulate()

*/

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 (logisDebugEnabled()) {

logdebug( Populating bean properties from this request);

}

formsetServlet(thisservlet);

formreset(mapping request);

if (mappinggetMultipartClass() != null) {

requestsetAttribute(GlobalsMULTIPART_KEY

mappinggetMultipartClass());

}

RequestUtilspopulate(form mappinggetPrefix() mappinggetSuffix()

request);

// Set the cancellation request attribute if appropriate

if ((requestgetParameter(ConstantsCANCEL_PROPERTY) != null) ||

(requestgetParameter(ConstantsCANCEL_PROPERTY_X) != null)) {

requestsetAttribute(GlobalsCANCEL_KEY BooleanTRUE);

}

}

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

OK看最后几行代码Struts从request中取得ConstantsCANCEL_PROPERTY这个参数如果这个参数不为空那么他就将

TRUE这个对象以GlobalsCANCEL_KEY为key值放到了request对象中

至于这个ConstantsCANCEL_PROPERTY这个值是什么现在都可以猜到了显然就是html:Cancel这个标签生成的HTML代码

Cancel这个按钮的名称嘛!查了一下果然是

<input type=submit name=orgapaclCANCEL value=返回 onclick=bCancel=true;>

而ConstantsCANCEL_PROPERTY这个值就是orgapaclCANCEL

上一篇:struts2标签中字符串及字符替换

下一篇:JavaWeb应用中获取Spring的ApplicationContext