Strurt2中的方法的呼叫方式
阿新 • • 發佈:2018-11-29
Strurt2中的方法的呼叫方式
訪問action方法的三種方式
1.定義一個Action類
//演示動態方法呼叫 public class Demo1Acton { public String add(){ System.out.println("新增使用者"); return "success"; } public String delete(){ System.out.println("刪除使用者"); return "success"; } public String update(){ System.out.println("修改使用者"); return "success"; } public String find(){ System.out.println("查詢使用者"); return "success"; } }
方式1.無腦式寫法寫多個action 訪問不同action中的name屬性值即可訪問對應的方法
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="dynamic" namespace="/dynamic" extends="struts-default"> <action name="Demo1Action_add" class="cn.test.Demo1Acton" method="add"> <result name="success">/hello.jsp</result> </action> <action name="Demo1Action_delete" class="cn.test.Demo1Acton" method="delete"> <result name="success">/hello.jsp</result> </action> <action name="Demo1Action_upfate" class="cn.test.Demo1Acton" method="update"> <result name="success">/hello.jsp</result> </action> <action name="Demo1Action_find" class="cn.test.Demo1Acton" method="find"> <result name="success">/hello.jsp</result> </action> </package> </struts>
方式1: 動態呼叫方式
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <!-- 設定動態方法呼叫是否開啟常量 預設是關閉的,需要開啟 http://localhost:8080/struts2_day01/dynamic/Demo1Action!find.do 上述方式 不推薦 name!method 其中.do是配置的<constant name="struts.action.extension" value="do"></constant>配置 --> <!-- 字尾名配置 --> <constant name="struts.action.extension" value="do"></constant> <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant> <package name="dynamic" namespace="/dynamic" extends="struts-default"> <action name="Demo1Action" class="cn.test.Demo1Acton"> <result name="success">/hello.jsp</result> </action> </package> </struts>
方式2:萬用字元形式
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- http://localhost:8080/dynamic/Demo1Action_add -->
<package name="dynamic" namespace="/dynamic" extends="struts-default">
<!-- 動態方法呼叫方式:萬用字元
{1}取出第一個新號通配中的內容
同樣的
Demo1Action_*_*
{2} 取第2個匹配中的內容
-->
<action name="Demo1Action_*" class="cn.test.Demo1Acton" method="{1}">
<result name="success">/hello.jsp</result>
</action>
</package>
</struts>