struts tags详解之<s:bean>
Description
Bean标签,当然需要一个JavaBean。它的属性值的操作是经由Bean标签中的参数属性来进行赋值。当然,它还有一个id属性可以进行赋值,这样就可以在上下文中使用这个Bean
如果在BeanTag中设置了var属性值,那么它将把实例化后的bean放入到stack's Context中。
Parameters
名称 |
必需 |
数据类型 |
描述 |
Id |
False |
String |
已弃用,var代替 |
Name |
true |
String |
bean的实例化类的名称(必须遵循JavaBean规范) |
Var |
False |
String |
用于引用到Value Stack中的值的名称 |
Examples
1 <-- in jsp form --> 2 <s:bean name="org.apache.struts2.example.counter.SimpleCounter" var="counter"> 3 <s:param name="foo" value="BAR" /> 4 The value of foot is : <s:property value="foo"/>, when inside the bean tag 5 </s:bean>
这个例子实例化了一个名叫SimpleCounter的bean,并设置foo属性(setFoo(‘BAR’))。然后将SimpleCounter对象压入值栈(Valuestack)中,这意味着我们可以调用property tag的访问方法(getFoo())来得到它的值。
在上面的例子中,id的属性值已经设为counter,这意味着SimpleCounter对象压入值栈(Valuestack)后可以通过如下标签来访问它:
1 <-- jsp form --> 2 <s:property value="#counter" />
1.WebRoot/pages/dataTags/beanTag.jsp,代码如下:
1 <%@ page contentType="text/html; charset=GBK" %> 2 <%@ taglib prefix="s" uri="/struts-tags" %> 3 <html> 4 <head> 5 <title>Bean Tag 示例</title> 6 </head> 7 <body> 8 <h2>Bean Tag 示例</h2> 9 <s:bean name="com.sterning.companyName" id="uid"> 10 <s:param name="name">sterning</s:param> 11 <s:property value="%{name}" /><br> 12 </s:bean> 13 </body> 14 </html>
其关联的JavaBean是com.sterning.companyName,同时参数name赋值为sterning。
2.首先创建Action进行跳转, src/com/sterning/beanTag.java,代码如下:
package com.sterning; import com.opensymphony.xwork2.ActionSupport; public class beanTag extends ActionSupport { public String execute() throws Exception{ return SUCCESS; } }
然后创建JavaBean,src/com/sterning/companyName.java,代码如下:
1 package com.sterning; 2 public class companyName { 3 private String name; 4 public void setName(String name){ 5 this.name =name ; 6 } 7 public String getName(){ 8 return name; 9 } 10 11 }
3.Struts.xml的配置
这里配置很简单,与前面的例子差不多。
1 <action name="beanTag" class="com.sterning.beanTag"> 2 <result name="success">/pages/dataTags/beanTag.jsp</result> 3 </action>
运行结果如下:
struts2 tags详解之<s:property>
Description
用于获取一个属性的值。Property顾名思义,可以与<s:bean>标签结合使用,一个是给bean赋值,一个是从bean中读取值。
Examples
1 <s:push value="myBean"> 2 <!-- Example 1: --> 3 <s:property value="myBeanProperty" /> 4 <!-- Example 2: -->TextUtils 5 <s:property value="myBeanProperty" default="a default value" /> 6 </s:push>
result:
Example 1 prints the result of myBean's getMyBeanProperty() method.
Example 2 prints the result of myBean's getMyBeanProperty() method and if it is null, print 'a default value' instead.
Struts2 tags详解之<s:set>
Set标签比较简单。Set标签用户将某一值赋给某一变量,因此,任何对该项值的引用都可以通过该变量来得到该值。该变量的活动范围可自定义。如下例中,定义一健/值对,对值的引用,直接引用值就可以。。请看示例
1.WebRoot/pages/dataTags/ setTag.jsp
1 <%@ page contentType="text/html; charset=GBK" %> 2 3 <%@ taglib prefix="s" uri="/struts-tags" %> 4 5 <html> 6 7 <head> 8 9 <title>Set Tag 示例</title> 10 11 </head> 12 13 <body> 14 15 <h2>Set Tag 示例</h2> 16 17 <s:set name="technologyName" value="%{'Java'}"/> 18 19 Technology Name: <s:property value="#technologyName"/> 20 21 </body> 22 23 </html>
2.Struts.xml配置
<action name="setTag"> <result>/pages/dataTags/setTag.jsp</result> </action>
3.运行效果