Struts2通配符详解

比如index.jsp里有多个请求

<a href="<%=path %>/usersAction!add.action">adda><br>
    <a href="<%=path %>/usersAction!show.action">showa><br>
    <a href="<%=path %>/usersAction!update.action">updatea><br>
    <a href="<%=path %>/usersAction!delete.action">deletea><br>

action里面

package com.cj.action;

import com.opensymphony.xwork2.ActionSupport;

public class UsersAction extends ActionSupport{


    public String add() throws Exception {
        System.out.println("进入add");
        return SUCCESS;
    }

    public String show() throws Exception {
        System.out.println("进入show");
        return SUCCESS;
    }

    public String update() throws Exception {
        System.out.println("进入update");
        return SUCCESS;
    }

    public String delete() throws Exception {
        System.out.println("进入delete");
        return SUCCESS;
    }
}

struts.xml配置




<struts>

    
    <constant name="struts.enable.DynamicMethodInvocation" value="true" />

    
    <constant name="struts.devMode" value="true">constant>
    
    <constant name="struts.ui.theme" value="simple">constant>
    
    <package name="test" extends="struts-default">
        
        <action name="usersAction" class="com.cj.action.UsersAction" method="add">
            <result name="success">/index.jspresult>
        action>

        <action name="usersAction" class="com.cj.action.UsersAction" method="show">
            <result name="success">/index.jspresult>
        action>


        <action name="usersAction" class="com.cj.action.UsersAction" method="update">
            <result name="success">/index.jspresult>
        action>

        <action name="usersAction" class="com.cj.action.UsersAction" method="delete">
            <result name="success">/index.jspresult>
        action>

    package>


struts>

像xml这里面有多个重复文件,这里就需要通配符

name="usersAction" class="com.cj.action.UsersAction" method="{*}">
            <result name="success">/index.jspresult>
        

就可以完成上面4种请求

但如果有多个action,上边只有一个用户user的 ,如果再来一个新闻的就还要加一个

name="usersAction" class="com.cj.action.UsersAction" method="{*}">
            <result name="success">/index.jspresult>
        
        name="newsAction" class="com.cj.action.NewsAction" method="{*}">
            <result name="success">/index.jspresult>
        

这又会出现两个相似的,怎么将这两个相似的合并

在index.jsp里面添加新闻的增删该查

<a href="<%=path%>/NewsAction!add.action">adda>
    <br>
    <a href="<%=path%>/NewsAction!show.action">showa>
    <br>
    <a href="<%=path%>/NewsAction!update.action">updatea>
    <br>
    <a href="<%=path%>/NewsAction!delete.action">deletea>
    <br>

struts.xml

将news和users用号替代 号代表所有 {1}代表第一个*号

name="*Action" class="com.cj.action.{1}Action" method="{*}">
            <result name="success">/index.jspresult>
        

index.jsp里面请求地址也要相应变化

没用通配符*

<a href="<%=path%>/usersAction!add.action">adda>

使用通配符*
就得用类名替代

<a href="<%=path%>/UsersAction!add.action">adda>

进一步优化
_ 哪个类的哪个方法

name="*_*" class="com.cj.action.{1}Action" method="{2}">
            <result name="{1}success">/{1}index.jspresult>
        

jsp里面

<a href="<%=path%>/News_add!add.action">adda>

你可能感兴趣的:(JavaWeb)