zoukankan      html  css  js  c++  java
  • struts2静态方法和动态方法调用

    1 jsp页面

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">   
        <title>struts2静态方法和动态方法调用</title>
      </head>
      
      <body>
        <a href="${pageContext.request.contextPath }/methodLogin.a">通过静态方法调用进入login</a><br />
        <a href="${pageContext.request.contextPath }/methodLoginA.b">通过静态方法调用进入loginA</a><br />
        <a href="${pageContext.request.contextPath }/methodDync!login.action">通过动态方法调用进入login</a><br />
        <a href="${pageContext.request.contextPath }/methodDync!loginA.action">通过动态方法调用进入loginA</a><br />
         <a href="${pageContext.request.contextPath }/methodDync!loginB.action">通过动态方法调用进入loginB</a><br />
          
      </body>
    </html>
    

    2 method.xml页面(必须在struts2.xml里定义,定义的格式为:<include file="method.xml"></include>)

    <?xml version="1.0" encoding="UTF-8" ?>
    
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">
    
    <struts>
        
        <package name="methodPackage" extends="struts-default">
              <!-- 
                 	定义了两个静态方法      method是指定跳转到类的某个自定义方法
               -->      
              <action name="methodLogin" class="cn.etop.struts2.method.MethodAction"  method="login">
                <result name="test">/method/success.jsp</result>
              </action>
              <action name="methodLoginA" class="cn.etop.struts2.method.MethodAction" method="loginA">
                <result name="test">/method/success.jsp</result>
              </action>
              <!-- 
                                         定义了一动态方法,但在jsp请求路径中,必须在类名后加“!要执行的方法名”
                                          如:action的name为:login,动态执行的方法为test,那么jsp的路径为:login!test.action
                                         开启动态方法有三种方式
                 	 第一种:在web.xml里配置参数
    		              <init-param>
    			            <param-name>struts.enable.DynamicMethodInvocation</param-name>
    			            <param-value>true</param-value>
    			         </init-param> 
    			         
    			           第二种:在src下创建一个属性文件:struts.properties(名字struts是固定的,不能是别的名字) 
    			          struts.enable.DynamicMethodInvocation = true
    			          
    			           第三种:在struts.xml里配置参数
    			           <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>                        
               -->
              <action name="methodDync" class="cn.etop.struts2.method.MethodAction" >
                <result name="test">/method/success.jsp</result>
              </action>
        </package>
      
    </struts>
    


    3 java代码

    package cn.etop.struts2.method;
    
    import java.io.IOException;
    
    import javax.servlet.http.HttpServletResponse;
    
    import org.apache.struts2.ServletActionContext;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    /**
     * 测试静态方法和动态方法
     * 自定义从xml请求的路径执行的方法
     * 
     * @author Administrator
     *
     */
    public class MethodAction extends ActionSupport {
       public String login(){
    	   HttpServletResponse response=ServletActionContext.getResponse();
    	   response.setContentType("text/html;charset=utf-8");
    	   try {
    		response.getWriter().print("方法login");
    	} catch (IOException e) {
    		e.printStackTrace();
    	}
    	return null;  
       }
       public String loginA(){
    	   HttpServletResponse response=ServletActionContext.getResponse();
    	   response.setContentType("text/html;charset=utf-8");
    	   try {
    		response.getWriter().print("方法loginA");
    	} catch (IOException e) {
    		e.printStackTrace();
    	}
    	return null;  
       }
       public String loginB(){
    	   HttpServletResponse response=ServletActionContext.getResponse();
    	   response.setContentType("text/html;charset=utf-8");
    	   try {
    		response.getWriter().print("方法loginB");
    	} catch (IOException e) {
    		e.printStackTrace();
    	}
    	return null;  
       }
       
       public String loginC(){
    	   HttpServletResponse response=ServletActionContext.getResponse();
    	   response.setContentType("text/html;charset=utf-8");
    	   try {
    		response.getWriter().print("方法loginC");
    	} catch (IOException e) {
    		e.printStackTrace();
    	}
    	return null;  
       }
    }
    

    4 在web.xml里配置动态方法

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" 
    	xmlns="http://java.sun.com/xml/ns/javaee" 
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
     
        <!-- 定义Struts 2的FilterDispatcher的Filter -->
    
             <filter>
                   <!-- 定义核心Filter的名字 -->
    	         <filter-name>struts2</filter-name>
    	           <!-- 定义核心Filter的实现类 -->
    	         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    	         <!-- 配置动态方法 -->
    	         <init-param>
    	            <param-name>struts.enable.DynamicMethodInvocation</param-name>
    	            <param-value>true</param-value>
    	         </init-param>
             </filter>
            <!-- FilterDispatcher用来初始化Struts 2并且处理所有的Web请求 -->
    
             <filter-mapping>
    	         <filter-name>struts2</filter-name>
    	         <url-pattern>/*</url-pattern>
             </filter-mapping>
    
         <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
    </web-app>
    

    5 在struts2.properties里配置动态方法

    #设置是否启动动态方法
    struts.enable.DynamicMethodInvocation = true
    #设置struts2的后缀
    struts.action.extension=action,a,b
    #设置是否启动开发模式
    struts.devMode = true
    #设置国际化属性文件的basename
    struts.custom.i18n.resources=message
    #设置上传文件的大小
    struts.multipart.maxSize=20971520

    6 在struts2.xml配置动态方法

    <?xml version="1.0" encoding="UTF-8" ?>
    
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">
    
    <struts>
    <!-- 表示自动扫描包结构中包含配置的字符 并带有注解的action-->
    	<constant name="struts.convention.package.locators" value="action,actions,struts,struts2"/>
    	<!-- 表示自动扫描以某个后缀结尾的类 要求必须带有注解 -->
    	<constant name="struts.convention.action.suffix" value="Action"/>
    	<!-- 表示从哪个包开始扫描 -->
    	<constant name="struts.convention.package.locators.basePackage" value="cn.et"/>
        <!-- include是其他的xml必须在Struts里定义,相当于继承 -->  
         <include file="method.xml"></include>    
    </struts>
    





  • 相关阅读:
    OK335xS-Android mkmmc-android-ubifs.sh hacking
    OK335xS-Android pack-ubi-256M.sh hacking
    OK335xS Ubuntu 12.04.1 版本 Android 开发环境搭建
    Qt Quick Hello World hacking
    Qt QML referenceexamples attached Demo hacking
    QT 5.4.1 for Android Ubuntu QtWebView Demo
    I.MX6 working note for high efficiency
    QT 5.4.1 for Android Windows环境搭建
    mkbootimg hacking
    Generate And Play A Tone In Android hacking
  • 原文地址:https://www.cnblogs.com/t0404/p/10290981.html
Copyright © 2011-2022 走看看