首页 > 代码库 > struts2学习笔记之八:Action中方法的动态调用

struts2学习笔记之八:Action中方法的动态调用

方法一:action名称+!+方法名称+后缀
Action类中增加addUser()和delUser()方法,
package com.djoker.struts2;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.Action;

public class UserAction {

    private String username;
    
    private String password;
    
    private int age;
    
    private String address;
    
    private String email;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
    
    public String execute() throws Exception {
        System.out.println("username: " + username);
        System.out.println("password: " + password);
        System.out.println("age:" + age);
        System.out.println("address" + address);
        System.out.println("email: " + email);
        return "success";
    }
    
    public String addUser() throws Exception {
        ServletActionContext.getRequest().setAttribute("msg", username);
        return Action.SUCCESS;
    }
    
    public String delUser() throws Exception {
        ServletActionContext.getRequest().setAttribute("msg", username);
        return Action.SUCCESS;
    }
}

 

struts.xml中Action配置(注意,配置文件中没有制定详细的方法,只是指定了一个name,)
<package name="user" extends="struts-default">
        <action name="user" class="com.djoker.struts2.UserAction">
            <result>/success.jsp</result>
            <result name="error">/error.jsp</result>
        </action>
    </package>

 

jsp中如何调用该Action中不同的方法
    <form action="user!addUser.action" method="post">
<!-- form action="user!delUser.action" method="post" -->
        用户名:<input type="text" name="username"><br>
        密码:<input type="password" name="password"><br>
        年龄:<input type="text" name="age"><br>
        地址:<input type="text" name="address"><br>
        邮箱:<input type="text" name="email"><br>
        <input type="submit" value="登录">
    </form>

 

方法二:多方法调用,创建多个action来指定不同的方法(不推荐该方法,如果有多个方法需要写多个Action)
struts.xml方法中的配置
<package name="user" extends="struts-default">
        <action name="addUser" class="com.djoker.struts2.UserAction" method="addUser">
            <result>/success.jsp</result>
            <result name="error">/error.jsp</result>
        </action>
        <action name="delUser" class="com.djoker.struts2.UserAction" method="delUser">
            <result>/success.jsp</result>
            <result name="error">/error.jsp</result>
        </action>
    </package>

 

jsp中如何调用
<form action="addUser.action" method="post">
<!-- form action="delUser.action" method="post" -->
        用户名:<input type="text" name="username"><br>
        密码:<input type="password" name="password"><br>
        年龄:<input type="text" name="age"><br>
        地址:<input type="text" name="address"><br>
        邮箱:<input type="text" name="email"><br>
        <input type="submit" value="登录">
    </form>

 

方法三:使用通配符配置,需要遵循一定的规范
struts.xml中的配置使用通配符
    <package name="user" extends="struts-default">
        <action name="*User" class="com.djoker.struts2.UserAction" method="{1}User">
            <result>/success.jsp</result>
            <result name="error">/error.jsp</result>
        </action>
    </package>

 

jsp中需要按照该规范来调用

 

struts2学习笔记之八:Action中方法的动态调用