struts2二级联动调用天气预报webservice服务在浏览器中显示天气情况

wsdl地址 http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl

直接使用wsimport 通过该地址生存java 文件时,会报错。因为该wsdl里面包含 ref = "s:schema" 这样的引用。而jaxb是不支持的。所以手动将该wsdl下载下来做下修改,然后再生成java文件。

修改方法为:

将所有的<s:element ref="s:schema"/> 改成 <s:any minOccurs="2" maxOccurs="2"/>

然后再通过wsimport生成java文件。 如果还是报错,就将wsdl文件里面的<wsdl:documentation 节点全部删除。应该就没问题了

生成好java文件之后 ,开始我们的旅程吧。


1.生成好java文件


2.经过改良,我们自己写的服务

由于生成的代码,返回类型都是它给我们提供的ArrayOfString,我们可以把其中的数据解析成为map

接口

package com.webservice.weather;

import java.util.Map;

public interface WeatherService {
	public Map<String,String> getSupportCity(String province);
	public Map<String,String> getSupportProvince();
	public Map<String,String> getWeatherbyCityName(String ctiy);
}

实现类

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<String, String> getSupportCity(String province) {
		WeatherWebService wws = new WeatherWebService();  
		WeatherWebServiceSoap soap = wws.getWeatherWebServiceSoap();  
		ArrayOfString aos = soap.getSupportCity (province);  
		Map<String,String> map = new LinkedHashMap<String,String>();
		for (String s : aos.getString()) 
	    {     
			map.put(s.substring(s.indexOf("(")+1, s.indexOf(")")), s.substring(0, s.indexOf("(")).trim());
	    }  
		return map;
	}

	public Map<String, String> getSupportProvince() {
		WeatherWebService wws = new WeatherWebService();  
		WeatherWebServiceSoap soap = wws.getWeatherWebServiceSoap();  
		ArrayOfString aos = soap.getSupportProvince ();  
		Map<String,String> map = new LinkedHashMap<String,String>();
		for (String s : aos.getString()) 
	    {     
			map.put(s, s);
	    }  
		return map;
	}

	public Map<String, String> getWeatherbyCityName(String ctiy) {
		WeatherWebService wws = new WeatherWebService();  
		WeatherWebServiceSoap soap = wws.getWeatherWebServiceSoap();  
		ArrayOfString aos = soap.getWeatherbyCityName (ctiy);  
		Map<String,String> map = new LinkedHashMap<String,String>();
		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.调用的action类,这里我们用的是struts2提供的json插件,由struts2直接给我们返回json数据

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<String,String> provinceMap;
	private Map<String,String> cityMap;
	private Map<String,String> 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<String, String> getProvinceMap() {
		return provinceMap;
	}
	public void setProvinceMap(Map<String, String> provinceMap) {
		this.provinceMap = provinceMap;
	}
	public Map<String, String> getCityMap() {
		return cityMap;
	}
	public void setCityMap(Map<String, String> cityMap) {
		this.cityMap = cityMap;
	}
	public Map<String, String> getWeatherMap() {
		return weatherMap;
	}
	public void setWeatherMap(Map<String, String> weatherMap) {
		this.weatherMap = weatherMap;
	}
	
	
}

4.页面代码

<!DOCTYPE html>
<html>
  <head>
    <title>天气预报</title>
	
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    
    <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("<option value="+key+">"+value+"</option>");
					});
				}
			});
		
		$("#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("<option value=‘-1‘>请选择</option>");
					$.each(data.cityMap,function(key,value)
					{
						$("#region :input:eq(1)").append("<option value="+key+">"+value+"</option>");
					});
				}
			});
		});
		
		$("#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=="endTendencyJpg3")
						{
							$("div.weather").append("<p><img src=images/weather/"+value+" /></p>");
						}
						else
						{
							$("div.weather").append("<p>"+value+"</p>");
						}
					});
				}
			});
		});
	});
	</script>
  </head>
  
  <body>
    <form id="region" method="post">
    	地域:
    	<select name="province" id="province"  style="width:70px">
			<option value="-1">请选择</option>
		</select>
		<select name="city"  id="city"  style="width:70px">
			<option value="-1">请选择</option>
		</select>
    </form>
    <div class="weather" style="width:1000px;height:1000px;border:1px solid #000">
    
    </div>
  </body>
</html>

 最终效果图:


struts2二级联动调用天气预报webservice服务在浏览器中显示天气情况,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。