Struts2 second-level linkage call weather forecast webservice to display weather conditions in the browser

Source: Internet
Author: User

 

If you use wsimport to survive java files through this address, an error is returned. Because the wsdl contains references such as ref = s: schema. Jaxb is not supported. Therefore, manually download and modify the wsdl, and then generate a java file.

The modification method is as follows:

Set all Change

Then generate a java file through wsimport. If an error is reported

After the java file is generated, let's start our journey.

 

1. Generate a java File

2. After improvement, we write our own services

The returned type of the generated code is the ArrayOfString provided to us. We can parse the data into a map.

Interface

 

package com.webservice.weather;import java.util.Map;public interface WeatherService {public Map
 
   getSupportCity(String province);public Map
  
    getSupportProvince();public Map
   
     getWeatherbyCityName(String ctiy);}
   
  
 

Implementation class

 

 

package com.webservice.weather;import java.util.LinkedHashMap;import java.util.Map;import cn.com.webxml.ArrayOfString;import cn.com.webxml.WeatherWebService;import cn.com.webxml.WeatherWebServiceSoap;public class WeatherServiceImpl implements WeatherService {public Map
 
   getSupportCity(String province) {WeatherWebService wws = new WeatherWebService();  WeatherWebServiceSoap soap = wws.getWeatherWebServiceSoap();  ArrayOfString aos = soap.getSupportCity (province);  Map
  
    map = new LinkedHashMap
   
    ();for (String s : aos.getString())     {     map.put(s.substring(s.indexOf(()+1, s.indexOf())), s.substring(0, s.indexOf(()).trim());    }  return map;}public Map
    
      getSupportProvince() {WeatherWebService wws = new WeatherWebService();  WeatherWebServiceSoap soap = wws.getWeatherWebServiceSoap();  ArrayOfString aos = soap.getSupportProvince ();  Map
     
       map = new LinkedHashMap
      
       ();for (String s : aos.getString()) { map.put(s, s); } return map;}public Map
       
         getWeatherbyCityName(String ctiy) {WeatherWebService wws = new WeatherWebService(); WeatherWebServiceSoap soap = wws.getWeatherWebServiceSoap(); ArrayOfString aos = soap.getWeatherbyCityName (ctiy); Map
        
          map = new LinkedHashMap
         
          ();String[] key = new String[]{province,cityname,citycode,cityjpg,lastupdate,intradayTemp,generalSituation,WindDirectionAndForce ,beginTendencyJpg,endTendencyJpg,now,exponent,intradayTemp2,generalSituation2,WindDirectionAndForce2,beginTendencyJpg2,endTendencyJpg2,intradayTemp3,intradayTemp3,WindDirectionAndForce3,beginTendencyJpg3,endTendencyJpg3,areaInfo};int index = 0;for (String s : aos.getString()) { map.put(key[index], s);index++; } return map;}}
         
        
       
      
     
    
   
  
 

3. The called action class. Here we use the json plug-in provided by struts2, and struts2 directly returns json data to us.

 

 

package net.itcast.action;import java.util.Map;import com.opensymphony.xwork2.ActionSupport;import com.webservice.weather.WeatherService;import com.webservice.weather.WeatherServiceImpl;@SuppressWarnings(serial)public class WeatherAction extends ActionSupport{private String provinceParam;private String cityParam;private Map
 
   provinceMap;private Map
  
    cityMap;private Map
   
     weatherMap;private WeatherService service = new WeatherServiceImpl();public String querySupportCity (){cityMap = service.getSupportCity(provinceParam);return suppCity;}public String querySupportProvince  (){provinceMap = service.getSupportProvince();return suppProvince;}public String queryWeatherbyCityName  (){weatherMap = service.getWeatherbyCityName(cityParam);return weatherinfo;}public String getProvinceParam() {return provinceParam;}public void setProvinceParam(String provinceParam) {this.provinceParam = provinceParam;}public String getCityParam() {return cityParam;}public void setCityParam(String cityParam) {this.cityParam = cityParam;}public Map
    
      getProvinceMap() {return provinceMap;}public void setProvinceMap(Map
     
       provinceMap) {this.provinceMap = provinceMap;}public Map
      
        getCityMap() {return cityMap;}public void setCityMap(Map
       
         cityMap) {this.cityMap = cityMap;}public Map
        
          getWeatherMap() {return weatherMap;}public void setWeatherMap(Map
         
           weatherMap) {this.weatherMap = weatherMap;}}
         
        
       
      
     
    
   
  
 

4. Page code

 

 

    <Script type = text/javascript src = http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js> </script> <script type = text/javascript> $ (function () {$. ajax ({type: POST, url: weather! QuerySupportProvince. action, dataType: json, data :{}, success: function (data) {$. each (data. provinceMap, function (key, value) {$ (# region: input: first ). append ( + Value +) ;}) ;}}); $ (# Region: input: first). change (function () {.. ajax ({type: POST, url: weather! QuerySupportCity. action, dataType: json, data: {'provinceparam': $ (# region: input: first ). val ()}, success: function (data) {$ (# region: input: eq (1 )). empty (). append ( Select); $. Each (data. cityMap, function (key, value) {$ (# region: input: eq (1). append ( + Value +) ;}) ;}};}); $ (# Region: input: eq (1 )). change (function () {$. ajax ({type: POST, url: weather! QueryWeatherbyCityName. action, dataType: json, data: {'cityparam': $ (# region: input: eq (1 )). val ()}, success: function (data) {$ (div. weather ). empty (); $. each (data. weatherMap, function (key, value) {if (key = beginTendencyJpg | key = beginTendencyJpg2 | key = beginTendencyJpg3 | key = endTendencyJpg | key = endTendencyJpg2 | key = Beijing) {$ (div. weather ). append ( 

);} Else {$ (div. weather). append (

+ Value +

) ;}}) ;};}) ;}); </Script>
Finally:

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.