zoukankan      html  css  js  c++  java
  • 【web开发学习笔记】Structs2 Action学习笔记(三)action通配符的使用

    action学习笔记3-有关于通配符的讨论

    使用通配符,将配置量降到最低,只是,一定要遵守"约定优于配置"的原则。

    一:前端htm


    <前端代码html>
    	</head>
    		<body>
    			<a href="<%=context %>/actions/Studentadd">加入学生</a>
    			<a href="<%=context %>/actions/Studentdelete">删除学生</a>
    			<a href="<%=context %>/actions/Teacher_add">加入老师</a>
    			<a href="<%=context %>/actions/Teacher_delete">删除老师</a>
    		</body>
    	</html>

    二:struct.xml

    //struct.xml
    <struts>
        <constant name="struts.devMode" value="true" />
        <package name="actions" extends="struts-default" namespace="/actions">
            <action name="Student*" class="com.struts2.action.StudentAction" method="{1}">
                <result>/Student{1}_success.jsp</result>
            </action>        
            <action name="*_*" class="com.struts2.action.{1}Action" method="{2}">
                <result>/{1}_{2}_success.jsp</result>
            </action>
        </package>
    </struts>

    三:类包

    //structs2调用的类包
    package com.struts2.action;
    import com.opensymphony.xwork2.ActionSupport;
    public class StudentAction extends ActionSupport {
    	public String add() {
    		return SUCCESS;
    	}
    	public String delete() {
    		return SUCCESS;
    	}	
    }

    四:过程分析

    运行步骤:点击<a href="<%=context %>/actions/Studentadd">加入学生</a> -> 通过struct.xml查找/actions/Studentadd -> 没有相匹配的action,则与namespace里面的Student*匹配  -> 调用类class="com.struts2.action.StudentAction -> 运行方法的选择,Studentadd与Student*匹配后,*就表示为add,{1},{2}的选择之后讨论。

    五:讨论

    加入一个新类
    package com.bjsxt.struts2.action;
    import com.opensymphony.xwork2.ActionSupport;
    public class TeacherAction extends ActionSupport {
    	public String add() {
    		return SUCCESS;
    	}	
    	public String delete() {
    		return SUCCESS;
    	}	
    }

    当我们点击<a href="<%=context %>/actions/TeacherAdd">加入老师</a> -> 通过struct.xml查找/actions/TeacherAdd-> 没有相匹配的action,则与namespace里面的*_*匹配  -> 调用类class="com.struts2.action.{1}Action -> 运行方法的选择,Teacher与*_*中第一个星号匹配后,第二个*就表示为add。{1},{2}的选择依据*的顺序决定。

    六:结论

    约定的好的话,配置极其简单。通过第五步的讨论,当我们继续加入新类之后,配置不须要改动。
  • 相关阅读:
    SQL Server设置登录验证模式
    怎样更改SQL Server 2008的身份验证方式
    sqlserver服务器名称改成本地IP地址登录
    零基础学python-2.2 输入 input()
    零基础学python-2.1 输出 print()
    零基础学python-1.7 第二个程序 猜数字小游戏
    零基础学python-1.6 错误的程序
    零基础学python-1.5 第一个程序
    零基础学python-1.4 hello world
    零基础学python-1.3 通过idle启动python
  • 原文地址:https://www.cnblogs.com/wgwyanfs/p/7225728.html
Copyright © 2011-2022 走看看