三、DMI
Dynamic Method Invocation,即动态决定调用Action的哪个方法,即不是提前配置决定,比如一个页面有登陆按钮和注册按钮,只有一个Action类,里面有login()方法和regist()方法,如果点击注册按钮,则调用regist方法,如果点击登录按钮,则调用login()方法,此时可以使用动态方法调用;
注意:在struts2.3 中动态方法调用默认开启,因此不需要配置,否则
<constant name="struts.enable.dynamicMethodInvocation" value="true"></constant>
默认配置文件:struts2-core-2.3.1.1.jar\org\apache\struts2\default.properties;
动态方法调用语法:ActionName!methodName
比如loginAction!login则调用loginAction类中的login方法;
示例代码:
LoginAction.java
- package org.login.action;
- import com.opensymphony.xwork2.ActionSupport;
- public class LoginAction extends ActionSupport{
- private String user;
- private String password;
- private String result;
- public String login()throws Exception{
- if(user.equals("xiazdong")&&password.equals("12345")){
- setResult("登陆成功");
- return SUCCESS;
- }
- else{
- setResult("登陆失败");
- return ERROR;
- }
- }
- public String regist()throws Exception{
- setResult("注册成功");
- return SUCCESS;
- }
- public String getUser() {
- return user;
- }
- public void setUser(String user) {
- this.user = user;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public void setResult(String result) {
- this.result = result;
- }
- public String getResult() {
- return result;
- }
- }
login.jsp
- <%@ page language="java" contentType="text/html; charset=utf-8"
- pageEncoding="utf-8"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>登录界面</title>
- <script type="text/javascript">
- function regist(){
- document.forms[0].action = "loginAction!regist";
- }
- </script>
- </head>
- <body>
- <form action="loginAction!login">
- 用户名:<input type="text" name="user"/><br />
- 密码:<input type="password" name="password"/><br />
- <input type="submit" value="登录"/>
- <input type="submit" value="注册" onClick="regist();"/>
- </form>
- </body>
- </html>
struts.xml
- <?xml version="1.0" encoding="GBK" ?>
- <!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"></constant>
- <package name="MyPackage" extends="struts-default" namespace="/" >
- <action name="loginAction" class="org.login.action.LoginAction" >
- <result name="success" type="redirect">
- <param name="location">/result.jsp</param>
- </result>
- <result name="error" type="redirect">/result.jsp</result>
- </action>
- </package>
- </struts>
四、通配符使用
我们可能会遇到这种情况:需要配置很多个逻辑Action,但是名称上有许多相似的地方,比如:
- <action name="loginAction" class="org.login.action.LoginAction" >
- <result name="success" type="dispatcher">/result.jsp</result>
- <result name="error" type="dispatcher">/result.jsp</result>
- </action>
- <action name="registAction" class="org.login.action.registAction" >
- <result name="success" type="dispatcher">/result.jsp</result>
- <result name="error" type="dispatcher">/result.jsp</result>
- </action>
则如果使用通配符则可以表示成:
- <action name="*Action" class="org.login.action.{1}Action" >
- <result name="success" type="dispatcher">/result.jsp</result>
- <result name="error" type="dispatcher">/result.jsp</result>
- </action>
在name属性中使用“*”,而在class、method、result元素中都可以使用{1}、{2}表示第一个*的值、第二个*的值;
<action name="*">时可以匹配任意的action请求,因为他是位于默认命名空间;
注意:如果同时匹配多个action,则除非完全相同,否则按先后顺序决定;
