[color=red]html:[/color]
<tr>
<td height="30" bgcolor="#DAEDFE"> <strong>郵便番号:</strong></td>
<td height="30" bgcolor="#DAEDFE"> 
<s:textfield id="postCode" name="preKokyakuMBean.ko_post" maxLength="8" size="25" οnblur="CtoH(this);value=value.replace(/[^\d|-]/g,'')"required="true" />
<input type="button" onClick="getAddressInfo()" name="addressSearch" style="width:100px;height:24px;" align="middle"value="住所検索">
</td></tr>

[color=red]js:[/color]
/****************************************************************
* 機 能: 住所検索処理
* 引 数: postCode
* 戻り値:画面の住所(必須)(上)項目
****************************************************************/
function parseMessage(){
if(req.responseText == ''){
alert('指定の郵便番号は不存在。');
$("#postCode").focus();
return false;
}
var result=eval(""+req.responseText+"");
var address=result[0].address;
var city=result[0].city;
var state=result[0].state;
var resultAddress=state+city+address;
if(resultAddress==null ||resultAddress==""){
alert('指定の郵便番号は不存在。');
$("#postCode").attr("value","");
$("#postCode").focus();
return false;
}else{
$("#add1").attr("value",resultAddress);
}
}
function callback(){

if(req.readyState == 4){
if(req.status == 200){
parseMessage();
}
}
}
function getAddressInfo(){
var postCode = $("#postCode").val();
if(postCode == ''||postCode==null){
alert('郵便番号は「666-8888」まだは「6668888」ようなフォームで入力して下さい。');
$("#postCode").focus();
return false;
}
else{
var pattern=/^((^([0-9]{3})-([0-9]{4})$)|(^[0-9]{7}$))$/;
var result = pattern.test(postCode);
if(result == false){
alert('郵便番号は「666-8888」まだは「6668888」ようなフォームで入力して下さい。');
$("#postCode").attr("value","");
$("#postCode").focus();
return false;
}
postCode= postCode.replace("-","");
}

var url="[color=green]searchAddressBack.action[/color]?postCode="+postCode;
if(window.XMLHttpRequest){
req=new XMLHttpRequest();
}else if(window.ActiveXObject){
req=new ActiveXObject("Microsoft.XMLHTTP");
}
if(req){
req.open("GET",url,true);
req.onreadystatechange=callback;
req.send(null);
}
}

[color=green]配置文件[/color][color=red]callContext.xml[/color]
<bean name="shiryouInputAction" scope="prototype"
class="jp.co.syspro.action.calldata.ShiryouInputAction">
<property name="shiryouInputService">
<ref local="shiryouInputService" />
</property>
</bean>

<bean id="shiryouInputService" scope="singleton"
class="jp.co.syspro.service.calldata.ShiryouInputService">
<property name="clientDaoImpl">
<ref local="clientDaoImpl" />
</property>
<property name="systen3DaoImpl">
<ref local="systen3DaoImpl" />
</property>
<property name="systen1DaoImpl">
<ref local="systen1DaoImpl" />
</property>
<property name="db" ref="DBCommon" />
</bean>

<bean id="system3DaoImpl" scope="singleton"
class="jp.co.syspro.persistence.sqlmapdao.Systen3DaoImpl">
</bean>
<bean id="clientDaoImpl" scope="singleton"
class="jp.co.syspro.persistence.sqlmapdao.ClientDaoImpl">
</bean>
<bean id="systen3DaoImpl" scope="singleton"
class="jp.co.syspro.persistence.sqlmapdao.Systen3DaoImpl">
</bean>
<bean id="systen1DaoImpl" scope="singleton"
class="jp.co.syspro.persistence.sqlmapdao.Systen1DaoImpl">
</bean>

[color=red]struts-call-config.xml[/color]
<action name="[color=green]searchAddressBack"[/color] class="shiryouInputAction" method="getAddress">
<result type="json"/>
</action>

<action name="[color=indigo]changeCustGyoMClass[/color]" class="shiryouInputAction" method="changeCustGyoMClassList">
<result type="json"/>
</action>

[color=red]ACTION:[/color]
/**
*
* @description 住所検索処理
* @return JSONString
* @throws SQLException
* @note なし
*/
public String getAddress() {
HttpServletRequest request = ServletActionContext.getRequest();
String postCode = (String) request.getParameter("postCode");
postCode = postCode.trim();
String state = "";
String city = "";
String address = "";
DocumentBuilderFactory dbf =DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = null;
try {
docBuilder = dbf.newDocumentBuilder();

URL url = new URL("http://zip.cgis.biz/xml/zip.php?zn=" + postCode);

InputStream inputStream = url.openStream();
Document doc = docBuilder.parse(inputStream);

Node node = null;
Element root = doc.getDocumentElement();
NodeList nodelist = root.getChildNodes();
NodeList subNodelist = null;
for (int i = 0; i < nodelist.getLength(); i++) {
node = nodelist.item(i);
String name = node.getNodeName();
if ("ADDRESS_value".equals(name)) {
subNodelist = node.getChildNodes();
break;
}
}
if (null != subNodelist) {
for (int i = 0; i < subNodelist.getLength(); i++) {
node = subNodelist.item(i);
if (!node.hasAttributes()) {
continue;
}
NamedNodeMap nodeMap = node.getAttributes();

if (i == 9) {
state = nodeMap.getNamedItem("state").getTextContent();
} else if (i == 11) {
city = nodeMap.getNamedItem("city").getTextContent();
} else if (i == 13) {
address = nodeMap.getNamedItem("address").getTextContent();
}
}
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

[color=blue] GetAddressTelNameInfoVO[/color] vo = new GetAddressTelNameInfoVO();
vo.setAddress(address);
vo.setCity(city);
vo.setState(state);

try {
[color=blue]JsonUtil.sendJson[/color](vo);
} catch (IOException e) {
if (log.isErrorEnabled()) {
log.error(e);
}
}

return null;
}

[color=brown]sendJson方法的调用[/color]
package jp.co.syspro.common.util;

import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import org.apache.struts2.ServletActionContext;

public class JsonUtil {
/**
* jsonデータ戻る
*
* @param obj
* データ
* @throws IOException
*/
public static void sendJson(Object obj) throws IOException {
JSONArray json = JSONArray.fromObject(obj);
HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncoding("UTF-8");
response.getWriter().write(json.toString());
}

}

[color=brown]GetAddressTelNameInfoVO [/color]
public class GetAddressTelNameInfoVO implements Serializable {
// TODO
private static final long serialVersionUID = 1L;
/**
* 都道府県データ
*/
private String state;
/**
* 市区郡データ
*/
private String city;
/**
* 区町村データ
*/
private String address;
/**
* 電話番号重複チェック結果
*/
private String telCheckResult;
/**
* 資料収集者社員名
*/
private String name;

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public String getCity() {
return city;
}

public void setCity(String city) {
this.city = city;
}

public String getState() {
return state;
}

public void setState(String state) {
this.state = state;
}

public String getTelCheckResult() {
return telCheckResult;
}

public void setTelCheckResult(String telCheckResult) {
this.telCheckResult = telCheckResult;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}

追加一个联动
/****************************************************************
* 機 能: 業種大分類を変わったら、業種中分類を変わる処理 下拉框
* 戻り値: 業種中分類
****************************************************************/
jsp画面: 用的是s2标签
<td height="35" bgcolor="#DAEDFE"> 
<s:select id="gyosyu1" name="shiryouInputVO.gyosyu1" list="custGyoBClassList" listKey="id" listValue="name" headerKey="-99" headerValue=" " οnchange="changeMClass();" required="true" />(大分類)
<s:select id="gyosyu" name="preKokyakuMBean.gyosyu" list="custGyoMClassList" listKey="id" listValue="name" required="true" />(中分類)</td>

[color=red]js:[/color]
function parseMessage_changeMClass(){
if(req.responseText == ''){
return false;
}
var result=eval(""+req.responseText+"");
var select_root=document.getElementById('gyosyu');
select_root.options.length=0;
for (var i = 0; i < result.length; i ++ ) {
select_root.options[i] = new Option(
result[i].name,
result[i].id);
}
var id=result[0].id;
var name=result[0].name;
$("#gyosyu").attr("value",id);
return true;
}
function callback_changeMClass(){

if(req.readyState == 4){
if(req.status == 200){
parseMessage_changeMClass();
}
}
}
function changeMClass(){
var gyosyu1 = $("#gyosyu1").val();
var url="changeCustGyoMClass.action?selectedBClass="+gyosyu1;
if(window.XMLHttpRequest){
req=new XMLHttpRequest();
}else if(window.ActiveXObject){
req=new ActiveXObject("Microsoft.XMLHTTP");
}
if(req){
req.open("GET",url,true);
req.onreadystatechange=callback_changeMClass;
req.send(null);
}

}
[color=olive] 后台 :action[/color]
/**
*
* @description 大分類を変わった時、中分類リスト内容を変わる
* @return JSONString
* @throws SQLException
* @throws NumberFormatException
* @note なし
*/
@SuppressWarnings("unchecked")
public String changeCustGyoMClassList() throws NumberFormatException,
SQLException {
// セッションを取得する
Map session = ActionContext.getContext().getSession();
HttpServletRequest request = ServletActionContext.getRequest();
String selectedBClass = (String) request.getParameter("selectedBClass");

String gyosyu1Name="";
//セッションから業種(大分類)リストオブジェクトを取得する
List<LabelValueBean> custGyoBClassAList = new ArrayList<LabelValueBean>();
custGyoBClassAList = (List<LabelValueBean>) session.get("custGyoBClassList");
for (LabelValueBean entity : custGyoBClassAList) {
String id = entity.getId();
if (id.equals(selectedBClass)) {
//選択した業種(大分類)名を取得
gyosyu1Name = entity.getName();
break;
}
}
if(!gyosyu1Name.equals("")){
// 業種:中分類リスト作成
custGyoMClassList = shiryouInputService.getTblCustGyoMClass(Integer.parseInt(selectedBClass));
}else{
LabelValueBean vo = custGyoBClassAList.get(0);
// 大分類ディフォルト選定値を設定する
selectedBClass = vo.getId();
// 中分類リストを取得する
custGyoMClassList = shiryouInputService.getTblCustGyoMClass(Integer.parseInt(selectedBClass));
}
session.put("custGyoMClassList", custGyoMClassList);
try {
[color=brown]JsonUtil.sendJson[/color](custGyoMClassList);
} catch (IOException e) {
if (log.isErrorEnabled()) {
log.error(e);
}
}
return null;
}

ajax json 根据邮政编码查地址相关推荐

  1. ajax json的参数,java ajax json参数

    java ajax json参数 [2021-02-02 08:55:23]  简介: php去除nbsp的方法:首先创建一个PHP代码示例文件:然后通过"preg_replace(&quo ...

  2. struts2 ajax html,Struts2+Jquery+Ajax+Json

    现在使用Json来封装并且传递数据的情形是越来越多了,可怎么样在Struts2中来使用Jquery+Ajax+Json来协同工作呢?在网上查了下就那几个例子被转过来转过去的,还有很多例子根本行不通,这 ...

  3. linux json 写sql注入,sql注入之AJAX(SQL Injection (AJAX/JSON/jQuery))

    sql注入之AJAX(SQL Injection (AJAX/JSON/jQuery)) 0x1 网上搜下没有关于ajax的sql注入文章,这里简单写一下抛砖引玉. 原理自己百度,这里简单说下: 通过 ...

  4. 微信小程序之根据经纬度反查地址

    最近做微信小程序项目中遇到根据后台接口获取城市某个区域内的信息,后台接口要求传入城市的区域名称,例如上海市杨浦区,小程序官方地址提供的API只能获取到用户当前的经纬度,如何通过经纬度查询到用户的当前位 ...

  5. python怎么查找代码位置查询_邮编查地址示例代码

    #!/usr/bin/python # encoding:utf-8 import urllib2, json, urllib # 1.邮编查地址 data = {} data["appke ...

  6. AJAX+JSON【练习】实现简易的点赞功能

    AJAX+JSON[练习]实现简易的点赞功能 1.前言 2.AJAX实现前后端数据传递 3.JSON与Java对象之间的转换 4.数据库相关配置 5.jQuery的JS文件,lib中的Jar包 6.代 ...

  7. laravel ajax返回json,Laravel validate error处理,ajax,json示例

    如下所示: public function updateLevelTestRecords(Request $request) { $rules = [ 'uid' => 'required|in ...

  8. ajax+php+jq+面向对象,php+jquery+ajax+json的一个最简单实例

    html页面: $(function(){ $("#send").click(function(){ var cont = $("input").seriali ...

  9. AJAX+json+jquery实现预加载瀑布流布局

    AJAX+json+jquery实现预加载瀑布流布局 转载于:https://www.cnblogs.com/zhujiabin/p/4860954.html

最新文章

  1. 基础环境搭建--原始mavenWeb项目
  2. 为什么Bugs没有被修复?
  3. 《走遍中国》珍藏版(四)
  4. 通过计算机网络进行的商务活动包括,电子商务练习题及答案
  5. 安卓投屏软件_有哪些好用又免费的手机投屏到电脑的软件?
  6. 在线CSV转XML/JSON工具
  7. 云计算机室怎么样映射d 盘,云服务器磁盘如何映射到本地磁盘
  8. 国家java认证考试报名入口,值得一读!
  9. Django入门教程
  10. ApacheCN 翻译/校对/笔记整理活动进度公告 2019.9.20
  11. jupyter notebook代码无法运行
  12. ReferenceError: request is not defined
  13. distinct效率更高还是group by效率更高?
  14. Visual Studio 2015产品密钥
  15. python包离线安装教程_Linux下离线安装Python项目的依赖包
  16. 一年之计在于春,一日之计在于晨
  17. 心电图学习笔记(1)
  18. 百度清风算法打击下载违规网站说明
  19. 领域建模——架构设计的第一步(上)
  20. 原来是这样的稳压二极管

热门文章

  1. 计算机原理概述,计算机组成原理-概述
  2. Andriod networking
  3. Unsupervised Monocular Depth and Ego-motion Learning with Structure and Semantics 之论文详解
  4. 如何在 Linux 下执行 bin 文件
  5. StreamEye使用说明
  6. 解决RealPlayer11崩溃的问题
  7. 利用ENVI实现landsat 5地表温度反演
  8. matlab输出二进制,Matlab二进制类型数据相关操作
  9. python爬虫之路【2】fiddle手机抓包
  10. 远离国产!windows唯一推荐杀毒软件-Avast