欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

struts2使用通配符与动态方法调用

程序员文章站 2022-07-12 16:13:48
...
1. 使用通配符

新建项目HeadFirstStruts2Chap02_05


GradeAction.java

package com.andrew.action;
import com.opensymphony.xwork2.ActionSupport;
public class GradeAction extends ActionSupport {
    private String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String list()throws Exception{
        System.out.println("班级查找");
        name="班级查找";
        return SUCCESS;
    }
    public String add()throws Exception{
        System.out.println("班级添加");
        name="班级添加";
        return SUCCESS;
    }
    public String update()throws Exception{
        System.out.println("班级修改");
        name="班级修改";
        return SUCCESS;
    }
    public String delete()throws Exception{
        System.out.println("班级删除");
        name="班级删除";
        return SUCCESS;
    }
}

StudentAction.java

package com.andrew.action;
import com.opensymphony.xwork2.ActionSupport;
public class StudentAction extends ActionSupport {
    private String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String list()throws Exception{
        System.out.println("学生查找");
        name="学生查找";
        return SUCCESS;
    }
    public String add()throws Exception{
        System.out.println("学生添加");
        name="学生添加";
        return SUCCESS;
    }
    public String update()throws Exception{
        System.out.println("学生修改");
        name="学生修改";
        return SUCCESS;
    }
    public String delete()throws Exception{
        System.out.println("学生删除");
        name="学生删除";
        return SUCCESS;
    }
}


1.1) 使用通配符 xxx_*

struts.xml

<package name="manage" namespace="/" extends="struts-default">
    <action name="student_*" class="com.andrew.action.StudentAction" method="{1}">
        <result name="success">success.jsp</result>
    </action>
    <action name="grade_*" class="com.andrew.action.GradeAction" method="{1}">
        <result name="success">success.jsp</result>
    </action>
</package>

index.jsp

<h1>通配符</h1>
<a href="student_list" target="_blank">学生信息查询</a>&nbsp;
<a href="student_add" target="_blank">学生信息添加</a>&nbsp;
<a href="student_update" target="_blank">学生信息修改</a>&nbsp;
<a href="student_delete" target="_blank">学生信息删除</a>&nbsp;
<br/><br/>
<a href="grade_list" target="_blank">班级信息查询</a>&nbsp;
<a href="grade_add" target="_blank">班级信息添加</a>&nbsp;
<a href="grade_update" target="_blank">班级信息修改</a>&nbsp;
<a href="grade_delete" target="_blank">班级信息删除</a>&nbsp;<br/>

success.jsp

Ok!${name }

http://localhost:8080/HeadFirstStruts2Chap02_05/index.jsp
Ok!学生查找


1.2) 使用通配符 *_*

第一个字母大写!!!

struts.xml

<package name="manage" namespace="/" extends="struts-default">
    <action name="*_*" class="com.andrew.action.{1}Action" method="{2}">
        <result name="success">success.jsp</result>
    </action>
</package>

index.jsp

<h1>通配符</h1>
<a href="Student_list" target="_blank">学生信息查询</a>&nbsp;
<a href="Student_add" target="_blank">学生信息添加</a>&nbsp;
<a href="Student_update" target="_blank">学生信息修改</a>&nbsp;
<a href="Student_delete" target="_blank">学生信息删除</a>&nbsp;
<br/><br/>
<a href="Grade_list" target="_blank">班级信息查询</a>&nbsp;
<a href="Grade_add" target="_blank">班级信息添加</a>&nbsp;
<a href="Grade_update" target="_blank">班级信息修改</a>&nbsp;
<a href="Grade_delete" target="_blank">班级信息删除</a>&nbsp;<br/>

success.jsp

Ok!${name }

http://localhost:8080/HeadFirstStruts2Chap02_05/index.jsp
Ok!学生查找


2. 动态方法调用

开启动态方法调用:
<constant name="struts.enable.DynamicMethodInvocation" value="true" />

struts.xml

<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<package name="manage" namespace="/" extends="struts-default">
    <action name="student" class="com.andrew.action.StudentAction">
        <result name="success">success.jsp</result>
    </action>
    <action name="grade" class="com.andrew.action.GradeAction">
        <result name="success">success.jsp</result>
    </action>
</package>

index.jsp

<h1>动态方法调用</h1>
<a href="student!list" target="_blank">学生信息查询</a>&nbsp;
<a href="student!add" target="_blank">学生信息添加</a>&nbsp;
<a href="student!update" target="_blank">学生信息修改</a>&nbsp;
<a href="student!delete" target="_blank">学生信息删除</a>&nbsp;
<br/><br/>
<a href="grade!list" target="_blank">班级信息查询</a>&nbsp;
<a href="grade!add" target="_blank">班级信息添加</a>&nbsp;
<a href="grade!update" target="_blank">班级信息修改</a>&nbsp;
<a href="grade!delete" target="_blank">班级信息删除</a>&nbsp;<br/>

success.jsp

Ok!${name }

http://localhost:8080/HeadFirstStruts2Chap02_05/index.jsp
Ok!学生查找
相关标签: Java struts2