PropertyTagAction.java
package com.tw511.common.action; import com.opensymphony.xwork2.ActionSupport; public class PropertyTagAction extends ActionSupport{ private String name = "Name from PropertyTagAction.java"; public String getName() { return name; } public String execute() throws Exception { return SUCCESS; } }
Person.java
package com.tw511.common; public class Person { private String name = "Name from Person.java"; public String getName() { return name; } }
property.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> </head> <body> <h1>Struts 2 property tag example</h1> <h2>1. Call getName() from propertyTagAction.java</h2> <s:property value="name" /> <h2>2. Call getName() from Person.java</h2> <s:bean name="com.tw511.common.Person" var="personBean" /> <s:property value="#personBean.name" /> </body> </html>
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <action name="propertyTagAction" class="com.tw511.common.action.PropertyTagAction" > <result name="success">pages/property.jsp</result> </action> </package> </struts>
http://localhost:8080/struts2propertytag/propertyTagAction.action
在瀏覽器中開啟上面的URL,顯示結果如下:
程式碼下載 - http://pan.baidu.com/s/1t7m2M