论坛首页 Java企业应用论坛

struts学习笔记(标签二)

浏览 3367 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-10-10  

Struts标记分为(5组)
JSP里学习过标准标签库与自定义标签,它的原理是一个JAVA类代码实现的,通过页面的

<%@ taglib  uri="/WEB-INF/struts-html.tld" prefix="html" %>

 来导入相应的配置文件,使用别名prefix即可调用标签库设定的标记
Html  struts-html.tld
Bean   struts-bean.tld
Logic   struts-logic.tld

Tiles   struts-tiles.tld
Nested  struts-nested.tld
红字为Struts中重要掌握的标签内容。一般JSP页面都导入红字相应的标签

上一笔记中只介绍了HTML表单的标签与BEAN中Write的一些简单的使用.接下来详细介绍bean及logic里面的标签元素
BEAN

<bean:define/>  定义变量
id=变量名
value=变量值
toScope=存储范围
name=存储作用域中的键名
property=属性
type=变量类型

<bean:define id="username" value="张三" toScope="request"/> 
username:<bean:write name="username" scope="request"/> 

 
<bean:write/> 输出变量值
<bean:write>
name=变量名
filter=是否过滤html
format=输出格式
property 属性名字
__________________________________________________________________________________
例如要输出对象时,必须先给定name为对象的别名,property属性的名称。
如:User对象中有Name属性

Request.setAttribute(“user”,UserObj); 
<bean :write name = “user” property=“name”/> 
request.setAttribute("date",new java.util.Date() );  --定义一个时间对象 
<bean:write name="date"/><br />   --默认输出为Mon Jul 09 21:44:06 CST 2007 
<bean:write name="date" format="yyyy-MM-dd"/><br />              2007-07-09 
<bean:write name="date" format="yyyy年MM月dd日"/><br />         2007年07月09日 

 __________________________________________________________________________________
<bean:size/> 计算集合长度
<bean:size>
id=变量名
name=存储作用域中的键名,该键名对应一个集合
---------------------------------------------------------------------------------

<% 
java.util.ArrayList list=new java.util.ArrayList(); 
list.add("111111"); 
list.add("222222"); 
list.add("33333333"); 
request.setAttribute("list",list); 
%> 
<bean:size id="len" name="list"/><br/> 
集合长度:<bean:write name="len"/><br /> 

 -----------------------------------------------------------------------------------
<bean:message/> 读取消息文件中的消息
<bean:message>
bundle=资源文件别名,该别名在struts-config.xml中声明
key=要读取的消息的键名
name=存储作用域中的键名,以该键名对应的键值作为key
property=存储作用域中bean的属性值
arg0=消息参数
------------------------------------------------------------------------------------

<bean:message bundle="mytest" key="username"/> 

 1. 在struts-config.xml配置 
   

<message-resources key="mytest" parameter="test" /> 

    这句在action-mapping后面,struts默认也配置有一个相应的文件。Parameter填写的值是一个.properties的文件名
2. test.properties文件中配置如下 
  

 username=\u7528\u6237\u540d          # username=用户名  --#为注释 

    因为默认填写中文时读取消息会乱码,所以得先把中文转换成\u7528。。等类型
3. 转换工具在jdk\bin目录下的native2ascii.exe 命令
Copy con a.txt
[要转换的中文填写]
^Z                       结束用Ctrl+Z,最后将刚才输入的保存在了a.txt文件中
然后使用 native2ascii a.txt b.txt
                         此时这个命令会把a.txt中的所有内容转换后输出b.txt
4. 还有一种情况就是配置带参数的消息
   msg=\u4f60\u597d,{0}

<bean:message bundle="mytest" key="msg" arg0="张三"/> 

 ------------------------------------------------------------------------------------
<bean:resource/> 将文件内容保存到变量
<bean:resource>
id=变量名
name=jsp页面名
<bean:include/> 将文件内容保存到变量
<bean:include>
id=变量名
forward=struts-config.xml中的forward别名
-----------------------------------------------------------------------------------

<bean:include id="x" forward="reg"/> 
<bean:write name="x" filter="false"/><hr /> 

<bean:resource id="x2" name="reg.jsp"/> 
<bean:write name="x2" filter="false"/> 

 
------------------------------------------------------------------------------

<logic:forward name="reg"/>      转发 reg 为struts-config.xml中 
<logic:redirect forward="reg"/>  重定向 

 

<global-forwards> 
    <forward name="reg" path="/reg.jsp" /> 
  </global-forwards> 

 
------------------------------------------------------------------------------
<logic:redirect/> 重定向
<logic:forward/>

<bean:header/>
<bean:cookie/>
<bean:page/> 取内置对象
-------------------------------------------------------------------------------------

<logic:equal/>判断值是否相等与设定的值
-------------------------------------------------------------------------------------
如:判断从username对象中value=” 张三” 成立的话就执行其标签中的代码

<logic:equal value="张三" name="username"> 
username的值为:张三<br/> 
</logic:equal> 
<logic:notEqual/>判断值是否不相等与设定的值 


<logic:notEqual value="张三" name="username"> 
username的值不为:张三<br/> 
</logic:notEqual> 
<logic:greaterThan/>判断值是否大与给定的值 

<logic:greaterThan value="30" name="age"> 
age大于30.<br /> 
</logic:greaterThan> 
<logic:lessThan/>判断值是否小与设定的值 

<logic:lessThan value="30" name="age"> 
age小于30.<br /> 
</logic:lessThan> 
<logic:match/>判断给定的值是否包含设定的值 

<logic:match value="三" name="username"> 
username中包含:三<br /> 
</logic:match> 
<logic:notMatch/>判断给定的值是否不包含设定的值 

<logic:notMatch value="三" name="username"> 
username中包含:三<br /> 
</logic:notMatch> 

 
-----------------在match中有个属性 location="end"为end时判断结尾是否包含------------
-----------------在match中有个属性 location="start"为start时判断开头是否包含------------
-------------------------------------------------------------------------------------


<logic:empty/>判断值是否为空,前提要存在这个值
-------------------------------------------------------------------------------------

<logic:empty name="username" scope="request"> 
  其值为空.<br /> 
</logic:empty> 

 
-------------------------------------------------------------------------------------

<logic:notEmpty/>判断值是否不为空,前提该值要存在
-------------------------------------------------------------------------------------

<logic:notEmpty name="username" scope="request"> 
  其值非空.<br /> 
</logic:notEmpty> 

 <logic:present/> 判断值或对象是否存在?
-------------------------------------------------------------------------------------

<logic:present name="username" scope="request"> 
request中包含username, 
<logic:empty name="username" scope="request"> 
  其值为空.<br /> 
</logic:empty> 
<logic:notEmpty name="username" scope="request"> 
  其值非空.<br /> 
</logic:notEmpty> 
</logic:present> 

 <logic:notPresent/>判断值或对象是否不存在
-------------------------------------------------------------------------------------

<logic:notPresent name="age" scope="request"> 
request中不存在age.<br/> 
</logic:notPresent> 

 <logic:messagesPresent/>判断消息是否存在
-------------------------------------------------------------------------------------
如:登陆的时候用户名或密码错误则显示错误信息的例子
1. 在ApplicationResources.properties文件中配置好相应的错误信息后
2. 在提交表单处理的Action中处理后加载错误消息的处理

import org.apache.struts.action.ActionErrors; 
import org.apache.struts.action.ActionMessage; 
//生成错误消息,先要导入相关的两个包 
            ActionErrors errors=new ActionErrors(); 
            ActionMessage msg=new ActionMessage("error.exist.username"); 
//构造里填写的就是配置文件中的key的名字 
            errors.add("username",msg); 
            //将消息集合存储到request 
            this.saveErrors(request,errors); 
            

 最后把错误消息存入在request中
在页面上直接如下判断即可,如果错误消息存在则执行<html:errors/>把错误消息全输出

<logic:messagesPresent> 
<html:errors/> 
</logic:messagesPresent> 

 <logic:messagesNotPresent/>判断消息是否不存在
-------------------------------------------------------------------------------------
如果没有则执行标签之间的内容
<logic:iterate/>迭带器一般与<bean:write>一起使用输出
-------------------------------------------------------------------------------------

<h1>iterate标记测试</h1> 
<% 
java.util.ArrayList list=new java.util.ArrayList(); 
list.add("111111"); 
list.add("222222"); 
list.add("33333333"); 
list.add("44444444"); 
list.add("55555555"); 
list.add("666666666666"); 
request.setAttribute("list",list); 
%> 
<logic:iterate scope="request" name="list" id="item" indexId="i"> 
<bean:write name="i"/>:          <!—name为填写的别名(key),id是临时的对象名称 --> 
<bean:write name="item"/><br /><!—name为迭带器每行存的临时对象名称 --> 
</logic:iterate> 
<hr /> 
<logic:iterate scope="request" name="list" 
id="item" indexId="i" offset="2" length="3"> 
<!—offset为迭带器要输出的起始位置 length为要输出的长度 indexId为下标--> 
<bean:write name="i"/>: 
<bean:write name="item"/><br /> 
</logic:iterate> 

 -------------------------------------------------------------------------------------

 

论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics