paramtagaction.java
package com.h3.common.action; import com.opensymphony.xwork2.actionsupport; public class actiontagaction extends actionsupport{ public string execute() { return success; } public string sayhello(){ return "sayhello"; } public string saystruts2(){ return "saystruts2"; } public string saysysout(){ system.out.println("sysout sysout sysout"); return "saysysout"; } }
下面的jsp页面显示如何使用“action”标签。如果 executeresult=”true”,动作标签被指定方法执行且结果页面将直接显示; 否则,它只是执行的方法,结果页面不会显示出来。
action.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <head><title>struts2 action标签示例<title> </head> <body> <h1>struts2 action标签示例</h1> <ol> <li> execute the action's result, render the page here. <s:action name="sayhelloaction" executeresult="true"/> </li> <li> doing the same as above, but call action's saystruts2() method. <s:action name="sayhelloaction!saystruts2" executeresult="true"/> </li> <li> call the action's saysysout() method only, no result will be rendered, by defautlt, executeresult="false". <s:action name="sayhelloaction!saysysout" /> </li> </ol> </body> </html>
sayhello.jsp
<html> <head> </head> <body> <div><div class="ads-in-post hide_if_width_less_800"> <script async class="lazy" data-original="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <!-- 728x90 - after2ndh4 --> <ins class="adsbygoogle hide_if_width_less_800" data-ad-client="ca-pub-2836379775501347" data-ad-slot="3642936086" data-ad-region="h3region"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div></div><h2>hello hello hello ~ from sayhello.jsp</h2> </body> </html>
saystruts2.jsp
<html> <head> </head> <body> <h2>struts 2 struts 2 struts 2 ~ from saystruts2.jsp</h2> </body> </html>
saysysout.jsp
<html> <head> </head> <body> <h2>sysout sysout sysout ~ from saysysout.jsp</h2> </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="actiontagaction" class="com.h3.common.action.actiontagaction" > <result name="success">pages/action.jsp</result> </action> <action name="sayhelloaction" class="com.h3.common.action.actiontagaction" method="sayhello"> <result name="sayhello">sayhello.jsp</result> <result name="saystruts2">saystruts2.jsp</result> <result name="saysysout">saysysout.jsp</result> </action> </package> </struts>
http://localhost:8080/struts2actiontag/actiontagaction.action
在浏览器中打开上面的url,显示结果如下图:
代码下载 - http://pan.baidu.com/s/1kt8ntuf