HelloBean.java
package com.tw511.common.action; public class HelloBean{ private String msg; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } }
BeanTagAction.java
package com.tw511.common.action; import com.opensymphony.xwork2.ActionSupport; public class BeanTagAction extends ActionSupport{ public String execute() { return SUCCESS; } }
bean.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> </head> <body> <h1>Struts2 <s:bean>標籤範例</h1> <s:bean name="com.tw511.common.action.HelloBean" var="hello"> <s:param name="msg">Hello Bean Tag</s:param> </s:bean> The HelloBean's msg property value : <s:property value="#hello.msg"/> </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="beanTagAction" class="com.tw511.common.action.BeanTagAction" > <result name="success">pages/bean.jsp</result> </action> </package> </struts>
http://localhost:8080/struts2beantag/beanTagAction.action
在瀏覽器中開啟上面的網址,結果如下所示:
程式碼下載 - http://pan.baidu.com/s/1jGJFv86