美文网首页
struts2动态方法调用

struts2动态方法调用

作者: 周大侠_2409 | 来源:发表于2018-09-16 22:34 被阅读0次

    上一篇:struct2 核心文件

    struts2动态方法调用

    method属性

    1. 在struts.xml文件中的配置如下:
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
    <struts>
       <package name="default"  extends="struts-default">
               <action name="login_add" class="action.HelloWorldAction" method="add"> 
                   <result name="add">/add.jsp</result>
               </action>
               <action name="login_delete" class="action.HelloWorldAction" method="delete"> 
                   <result name="delete">/delete.jsp</result>
               </action>
       </package>
    </struts>    
    
    1. 在src/action建立java类HelloWorldAction
    package action;
    
    import com.opensymphony.xwork2.ActionSupport;
    public class HelloWorldAction extends ActionSupport{
        public String add(){
            return "add";
        }
        public String delete(){
            return "delete";
        }
        @Override
        public String execute() throws Exception{
            // TODO Auto-generated method stub
            System.out.println("执行Action");
            return "success";
        }
    }
    

    3.在webRoot下建立两个页面:

    • add.jsp
    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <html>
      <body>
           add<br>
      </body>
    </html>
    
    • delete.jsp
    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <html>
      <body>
           delete<br>
      </body>
    </html>
    
    1. 发布tomcat,输入地址http://localhost:8080/test/login_delete.action
      http://localhost:8080/test/login_add.action检验

    感叹号方式(不推荐)

    在方式下实现动态加载需要加上:
    <constant name="structs.enable.DynamicMethodInvocation" value="true"/>

    1. 修改struts.xml文件
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
    <struts>
       <constant name="structs.enable.DynamicMethodInvocation" value="true"/>
       <package name="default"  extends="struts-default">
               <action name="login" class="action.HelloWorldAction" > 
                   <result name="add">/add.jsp</result>
                   <result name="delete">/delete.jsp</result>
               </action>
       </package>
    </struts>    
    
    1. 键入地址 http://localhost:8080/test/login!add.action
      http://localhost:8080/test/login!delete.action进行检验

    通配符方式

    1. 修改struts.xml文件
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
    <struts>
       <package name="default"  extends="struts-default">
               <action name="login_*" class="action.HelloWorldAction" method="{1}"> 
                   <result name="add">/add.jsp</result>
                   <result name="delete">/delete.jsp</result>
               </action>
       </package>
    </struts>    
    
    1. 部署tomcat服务器,地址栏输入http://localhost:8080/test/login_add.action
      http://localhost:8080/test/login_delete.action

    相关文章

      网友评论

          本文标题:struts2动态方法调用

          本文链接:https://www.haomeiwen.com/subject/sdrqnftx.html