struts2 标签初识 no Form ui Tags

本文详细解析了Struts2框架中用于输出Action实例信息的UI标签,包括actionerror、actionmessage、div和fielderror的用法和特性。同时介绍了如何在页面中使用这些标签以及自定义组件的灵活应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、actionerroractionmessage

这两个标签用法完全一样,作用也几乎完全一样,都是负责输出Action实例里封装的信息,区别是actionerror标签负责输出Action实例的getActionError()方法的返回值,而actionmessage标签负责输出Action实例的getActionMessage()方法的返回值。
对于这两个标签而言,几乎没有自己的专有属性,故使用起来非常简单。
下面是本示例应用中的Action类,这个Action类仅仅添加了两条ActionError和ActionMessage,并没有做过多处理,下面是Action类的代码:
public class DemoAction extends ActionSupport
{
@Override
public String execute()
{
//添加两条Error信息
addActionError("第一条错误消息!");
addActionError("第二条错误消息!");
//添加两条普通信息
addActionMessage("第一条普通消息!");
addActionMessage("第二条普通消息!");
return SUCCESS;
}
}
上面的Action的execute方法仅仅在添加了四条消息后,直接返回success字符串,success字符串对应的JSP页面中使用<s:actionerrror/>和<s:actionmessage/>来输出ActionError和ActionMessage信息。下面是该JSP页面中使用这两个标签的示例代码:
<!-- 输出getActionError()方法返回值 -->
<s:actionerror/>
<!-- 输出getActionMessage()方法返回值 -->
<s:actionmessage />
在另一个页面中使用<s:action .../>标签来调用上面的Action,调用Action的标签代码片段如下:
<s:action name="demo" executeResult="true"/>
从上面的<s:action .../>标签中可以看出,上面代码将demoAction的处理结果包含到本页面中来。

2、component标签
component标签用于使用自己的自定义组件,这是一个非常灵活的用法,如果经常需要使用某个效果片段,就可以考虑将这个效果片段定义成一个自定义组件,然后在页面中使用component标签来使用该自定义组件。
因为使用自定义组件还是基于主题、模板管理的,因此在使用component标签时,常常需要指定如下三个属性:
 theme:自定义组件所使用的主题,如果不指定该属性,默认使用xhtml主题。
 templateDir:指定自定义组件的主题目录,如果不指定,默认使用系统的主题目录,即template目录。
 template:指定自定义组件所使用的模板。
除此之外,还可以在cmponent标签内使用param子标签,子标签表示向该标签模板中传入额外的参数。如果希望在模板中取得该参数,总是采用如下形式:$parameters.paramname,或者$parameters['paramname']。
提示:自定义的模板文件可以采用FreeMarker、JSP和Velocity三种技术来书写。
看下面的JSP页面,该页面多次使用了<s:component .../>标签来使用自定义组件,下面是该页面使用<s:component .../>标签的代码片段:
17: <b>Example 1:</b>  
18: This example load the template from the webapp context path using
19: the default (ftl) as its template.
20:     <s:component
21:         theme="customTheme"
22:         templateDir="customTemplateDir"
23:         template="ftlCustomTemplate">
24:         <s:param name="paramName" value="%{'paramValue1'}" />
25:     </s:component>
26: <p/>
27:    
28: <b>Example 2:</b>
29: This example load the template from the webapp context path using
30: jsp as its template (notice the *.jsp extension to the template).      
31:     <s:component
32:         theme="customTheme"
33:         templateDir="customTemplateDir"
34:         template="jspCustomTemplate.jsp">
35:         <s:param name="paramName" value="%{'paramValue2'}" />
36:     </s:component>     
37: <p/>
38:    
39: <b>Example 3</b>
40: This example load the template from the webapp context path,
41: using the default template directory and theme (default to
42: 'template' and 'xhtml' respectively)
43:     <s:component template="mytemplate.jsp">
44:         <s:param name="paramName" value="%{'paramValue3'}" />
45:     </s:component>
46: <p/>
47:    
48:    
49: <b>Example 4</b>   
50: This example load the template from the webapp classpath using
51: a custom themplate directory and theme.
52:     <s:component
53:         theme="myTheme"
54:         templateDir="myTemplateDir"
55:         template="myAnotherTemplate">
56:         <s:param name="paramName" value="%{'paramValue4'}" />
57:     </s:component>
58: <p/>
项目根目录/customTemplateDir/customTheme/ftlCustomTemplate.ftl

<div style="background-color:yellow;">
<p>
Freemarker Custom Template -
parameter 'paramName' - ${parameters.paramName}
</p>
</div>
项目根目录/customTemplateDir/customTheme/jspCustomTemplate.jsp

<%@taglib prefix="s" uri="/struts-tags" %>

<div style="background-color:yellow;">
<p>
JSP Custom Template -
parameter 'paramName' - <s:property value="%{top.parameters.paramName}" />
</p>
</div>
3、div

<s:div></s:div>-----表示一个块,类似于html的<div></div>

4、fielderror

actionerroractionmessage的用法相似,在程序中添加例如addFieldError("password","Invalid password!");

默认主题下, <s:form />能显示错误信息,能自动添加表格排版,

  修改主题:<s:form theme="simple"> 不能显示错误信息,不能自动添加表格排版才可以自己进行排版

 

   <!-- example 1 -->
   <s:fielderror />

   <!-- example 2 -->
   <s:fielderror>
        <s:param>field1</s:param>
        <s:param>field2</s:param>
   </s:fielderror>
   <s:form .... >
      ....
   </s:form>

   OR

   <s:fielderror>
         <s:param value="%{'field1'}" />
         <s:param value="%{'field2'}" />
   </s:fielderror>
   <s:form .... >
      ....
   </s:form>

   OR

   <s:fielderror fieldName="field1" />
原地址:http://www.roseindia.net/struts/struts2/struts2uitags/fielderror-tag.shtml


    

   <!-- example 1 -->
   <s:fielderror />

   <!-- example 2 -->
   <s:fielderror>
        <s:param>field1</s:param>
        <s:param>field2</s:param>
   </s:fielderror>
   <s:form .... >
      ....
   </s:form>

   OR

   <s:fielderror>
         <s:param value="%{'field1'}" />
         <s:param value="%{'field2'}" />
   </s:fielderror>
   <s:form .... >
      ....
   </s:form>

   OR

   <s:fielderror fieldName="field1" />
原地址:http://www.roseindia.net/struts/struts2/struts2uitags/fielderror-tag.shtml


    

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值