這篇文章主要介紹了JSP Spring設定檔中傳值的實例詳解的相關JSP資料,對JSP有興趣的朋友可以參考下本篇文章。
JSP Spring設定檔中傳值的實例詳解
透過spring提供方法,在設定檔中取傳值
呼叫get方法
targetObject
## target
Object
## :指定為該「getter」方法## 1:
public class Test1 { private String name = "nihao"; public String getName() { return name; } }
Xml程式碼:
<bean id="t1" class="i.test.Test1" /> <!-- name = t1.getName() --> <bean id="name" class="org.springframework.beans.factory.config.PropertyPathFactoryBean" p:targetObject-ref="t1" p:propertyPath="name" />
範例2:
Java程式碼
##
public class Test3 { private Test1 t1 = new Test1(); private String name; public void setName(String name) { this.name = name; } public Test1 getT1() { return t1; } }
<!-- 将t1的name属性值赋给t3的name --> <bean id="t3" class="i.test.Test3"> <property name="name"> <!-- 执行t3.getT1().getName() --> <bean id="t3.t1.name" class="org.springframework.beans.factory.config.PropertyPathFactoryBean" /> </property> </bean>
存取類別的Filed屬性
public class Test3 { public String name = "nihao"; public static final int MAX_LENGTH = 10; }
<bean id="t3" class="i.test.Test3" /> <!-- 访问成员Field 需要实例 --> <bean id="name" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean" p:targetObject-ref="t3" p:targetField="name" />
<!-- 访问静态field只需要类路径 --> <bean id="length" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean" p:targetClass="i.test.Test3" p:targetField="MAX_LENGTH" /> <!-- 或者 --> <bean id="length" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean" p:staticField="i.test.Test3.MAX_LENGTH" />
呼叫類別的方法
public class Test3 { public void execute(String str, int i) { System.err.println(str + "--" + i); } }
<bean id="t3" class="i.test.Test3" /> <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean" p:targetObject-ref="t3" p:targetMethod="execute"> <property name="arguments"> <list> <value>nihao</value> <value>56</value> </list> </property> </bean>
以上是JSP Spring設定檔中傳值的實例詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!