>>action中调用
this .addActionError( " actionError! " );
this .addActionMessage( " actionMessage! " );
this .addFieldError( " field " , " fieldError " );
this .addFieldError( " field2 " , " fieldError2 " );
>>jsp中调用
<%@taglib prefix="s" uri="/struts-tags"%>
<s:actionerror />
<s:fielderror>
<s:param>field</s:param>
<s:param>field2</s:param>
</s:fielderror>
或
<s:fielderror>
<s:param value="%{'field'}" />
<s:param value="%{'field2'}" />
</s:fielderror>
< s:actionmessage />
下面给出一段代码,帮助大家理解,这里只写了关键代码.
1. Action 类中的部分代码:
ActionMessages ams =
new ActionMessages();
ams.add(ActionMessages.GLOBAL_MESSAGE,
ams.add(ActionMessages.GLOBAL_MESSAGE,
new ActionMessage(
"addMessages : global message" ,
false ));
ams.add( "error" , new ActionMessage( "addMessages : error message" , false ));
ams.add( "info" , new ActionMessage( "addMessages : info message" , false ));
this .addMessages(httpServletRequest,ams);
ActionMessages errorMessage = new ActionMessages();
errorMessages.add(ActionMessages.GLOBAL_MESSAGE,
ams.add( "error" , new ActionMessage( "addMessages : error message" , false ));
ams.add( "info" , new ActionMessage( "addMessages : info message" , false ));
this .addMessages(httpServletRequest,ams);
ActionMessages errorMessage = new ActionMessages();
errorMessages.add(ActionMessages.GLOBAL_MESSAGE,
new ActionMessage(
"addErrors : global message" ,
false ));
errorMessages.add( "error" ,
errorMessages.add( "error" ,
new ActionMessage(
"addErrors : error message" ,
false ));
errorMessages.add( "info" , new ActionMessage( "addErrors : info message" , false ));
this .addErrors(httpServletRequest,errorMessages);
errorMessages.add( "info" , new ActionMessage( "addErrors : info message" , false ));
this .addErrors(httpServletRequest,errorMessages);
2. JSP文件中的部分代码:
< html:messages id ="message" >
< bean:write name ="message" />
< br />
</ html:messages >
<!-- 上面代码的输出结果:
addErrors : global message
addErrors : error message
addErrors : info message
-->
< html:messages id ="message" property ="info" >
< bean:write name ="message" />
<br />
</ html:messages >
<!-- 这段代码的输出结果为:
addErrors : info message
-->
< html:messages id ="message" message ="true" >
< bean:write name ="message" />
</ html:messages >
<!-- 这段代码的输出结果为:
addErrors : info message
-->
< html:messages id ="message" message ="true" >
< bean:write name ="message" />
<br />
</ html:messages >
<!-- 这段代码的输出结果:
addMessages : global message
addMessages : error message
addMessages : info message
-->
< html:messages id ="message" message ="true" property ="info" >
< bean:write name ="message" />
</ html:messages >
<!-- 这段代码的输出结果:
addMessages : global message
addMessages : error message
addMessages : info message
-->
< html:messages id ="message" message ="true" property ="info" >
< bean:write name ="message" />
<br />
</ html:messages >
<!-- 这段代码的输出结果为:
addMessages : info message
-->
</ html:messages >
<!-- 这段代码的输出结果为:
addMessages : info message
-->