实例3:返回数组

来源:互联网 发布:国际日期变更线的算法 编辑:程序博客网 时间:2024/05/19 09:13

Example3 (listCode() return String[]: all logincodes). 
1. mysql: database(spring), testing table(spring) (same as Example1)
2. axisclasspath: ...... D:/mydemos/axis_text/src (same as Example1)
3. java codes for server: (D:/mydemos/axis_text/src/)
a) hibernate.cfg.xml (same as Example1)
b) Tblogin.hbm.xml (same as Example1)
c). LoginDaoImpl.java (should be put in the directory as: %webapp%/web-inf/classes/....)
package cn.com.mytest.dao;

import java.io.Serializable;
import java.util.List;
import java.util.ArrayList;

import org.hibernate.Session;
import org.hibernate.HibernateException;
import org.hibernate.Transaction;
import org.hibernate.Query;

import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;

import cn.com.mytest.dao.hibernate.config.SessionFactoryClass;

public class LoginDaoImpl {
    private static final Log log = LogFactory.getLog(LoginDaoImpl.class);
    public void add(Serializable object){....(same as Example1)}
    public List list(){....(same as Example1)}
    public Object get(Integer loginid){ ... (same as example2)...  }
         

}
4) LoginImpl.java (should be put in the directory of server side as: %webapp%/web-inf/classes/....)
package cn.com.mytest.server;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.com.mytest.dao.hibernate.Tblogin;
import cn.com.mytest.dao.LoginDaoImpl;

public class LoginImpl {
    private static final Log log = LogFactory.getLog(LoginImpl.class);
    LoginDaoImpl dao = new LoginDaoImpl();
    public List list(){... same as Example1...}
   public String[] listCodes(){
        List list = dao.list();
        if(list!=null){
            String[] codes = new String[list.size()];
           
            for(int i=0;i<list.size();i++){
                Tblogin login = (Tblogin) list.get(i);
                log.info("the login result: number " + i + ", loginname=" + login.getLogincode().trim().toUpperCase());
                codes[i] = login.getLogincode().trim().toUpperCase();
            }
            return codes;
        }else{
            log.info("there is no rows in the table Tblogin");
            return null;
        }
    }

}
4. wsdd file: deploy.wsdd (cn/com/mytest/server/deploy.wsdd)
<deployment xmlns="http://xml.apache.org/axis/wsdd/"
            xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">

 <service name="LoginService" provider="java:RPC">
  <parameter name="className" value="cn.com.mytest.server.LoginImpl"/>
  <parameter name="allowedMethods" value="*"/>
  <beanMapping qname="myNS:Tblogin" xmlns:myNS="urn:LoginImpl" languageSpecificType="java:cn.com.mytest.dao.hibernate.Tblogin"/>
 </service>

</deployment>
wsdd file: undeploy.wsdd(cn/com/mytest/server/undeploy.wsdd) (same as Example10
5) Testing from client:
package cn.com.mytest.client;

import java.util.List;
import javax.xml.namespace.QName;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;

public class TestClient {
    public static void main(String [] args)
    {
        try {
            String endpointURL = "http://localhost:8080/axis/services/LoginService";
            Service  service = new Service();
            Call     call    = (Call) service.createCall();
            QName    qn      = new QName( "urn:LoginImpl", "Tblogin" );
         
            call.registerTypeMapping(String[].class, qn,
                    new org.apache.axis.encoding.ser.ArraySerializerFactory(),
                    new org.apache.axis.encoding.ser.ArrayDeserializerFactory());
            call.setTargetEndpointAddress( new java.net.URL(endpointURL) );
            call.setOperationName( new QName("http://server.mytest.com.cn", "listCodes") );
            call.setReturnType(org.apache.axis.encoding.XMLType.XSD_ANYTYPE);
            String[] names = (String[]) call.invoke(new Object[]{});
            if(names!=null){
                for(int i=0;i<names.length;i++){
                    String name = names[i];
                    System.out.println(name);
                }
            }
           
        } catch (Exception e) {
            System.err.println(e.toString());
        }
    }
}

原创粉丝点击