(一) postmark用来干什么?
很多网站注册时都要求填写出生地是什么地方对吧。
往往UI是这样的:一个下拉框选择省级行政区单位,你选择好了以后,用第二下拉框选择市(县)。
postmark就是来帮助大家完成这个工作的。
(二) 如何得到postmark?
iteye用户可以通过本文附件下载。
(三) postmark依赖什么?
1) 依赖spring,建议使用spring3.1.2.RELEASE。
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.1.2.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>3.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>3.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>3.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>3.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>3.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>3.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc-portlet</artifactId>
<version>3.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.11</version>
</dependency>
2) 依赖hsqldb,建议使用hsqldb-2.2.8
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.2.8</version>
</dependency>
(四) 核心API
package com.ztgame.postmark.api;
import java.util.List;
import com.ztgame.postmark.entity.AdministrativeRegion;
/**
* 核心API
*
* @author 应卓
*
*/
public interface PostmarkService {
/**
* 获取所有省级行政区列表
*
* @return 所有省级行政区列表
*/
public List<AdministrativeRegion> getAllLevelOneAdministrativeRegions();
/**
* 根据省级行政区ID获得所辖市县(区)的信息
*
* @param id 省级行政区id
* @return 所辖市县的信息
*/
public List<AdministrativeRegion> getLevelTwoAdministrativeRegionsByParentId(Integer id);
/**
* 根据省级行政区名称或简称获取所辖市县(区)的信息
*
* @param nameOrShortName 省级行政区名称或简称
* @return 获取所辖市县(区)的信息
*/
public List<AdministrativeRegion> getLevelTwoAdministrativeRegionsByParentName(String nameOrShortName);
}
核心API仅仅包含一个接口的三个方法,很简单。
(五) 配置postmark
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
<bean id="postmarkService" class="com.ztgame.postmark.impl.PostmarkServiceImpl">
<property name="jdbcTemplate">
<bean class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource">
<jdbc:embedded-database>
<jdbc:script encoding="UTF-8" location="classpath:com/ztgame/postmark/api/postmark.sql" />
</jdbc:embedded-database>
</property>
</bean>
</property>
</bean>
</beans>
这样postmark的核心API就可以使用了。
(六) 在Web项目中运用postmark
1) postmark通过一个javax.servlet.Filter的实现类来响应ajax请求
<bean id="postmarkSerivceFilter" class="com.ztgame.postmark.web.PostmarkServiceFilter">
<property name="encoding" value="utf-8" />
<property name="postmarkService" ref="postmarkService" />
</bean>
2) 在web项目部署描述符应当配置相应的url-pattern信息,如:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<filter-name>postmarkSerivceFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>postmarkSerivceFilter</filter-name>
<url-pattern>/postmark.cgi</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring-beans.xml
</param-value>
</context-param>
</web-app>
3) filter接受的请求参数
[table]
|功能|method|shortName|cssClass|id|name|
|得到所有的省级行政区option标签|getAllLevelOne|true或者false 默认为false|任意|不需要|不需要|
|通过省级行政区ID得到市(县)option标签|getLevelTwoByParentId|true或者false 默认为false|任意|省级行政区ID|不需要|
|通过省级行政区名称或简称得到市(县)option标签|getLevelTwoByParentName|true或者false 默认为false|任意|不需要|名称或简称|
[/table]
4) jsp example
<%@ page language="java" %>
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page import="java.util.*" %>
<%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<base href="<%=basePath%>" />
<link rel=stylesheet type="text/css" href="css/style.css"/>
<script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$.ajax({
url: "postmark.cgi",
type: "GET",
data: {
method: "getAllLevelOne",
shortName: "false"
},
dataType: "html",
error: function(){
alert("error");
},
success: function(html) {
$("#level1").html("<option value='-1' class=''>保密</option><option disabled='disabled'>----------------</option>" + html);
}
});
$("#level2").hide();
$("#level1").change(function() {
var parentId = $(this).val();
if (parentId == -1) {
$("#level2").hide();
} else {
$("#level2").show();
$.ajax({
url: "postmark.cgi",
type: "GET",
data: {
method: "getLevelTwoByParentId",
id: parentId,
shortName: "false"
},
dataType: "html",
error: function() {
alert("error");
},
success: function(html) {
$("#level2").html(html);
}
});
}
});
});
</script>
<title>index.jsp</title>
</head>
<body>
<label>你来自:</label><select name="level1" id="level1"></select><select name="level2" id="level2" ></select>
</body>
</html>
5) 效果图
[img]http://dl.iteye.com/upload/picture/pic/116085/16ee968d-b9cd-33f5-8eb4-eb460868ca20.png[/img]
[img]http://dl.iteye.com/upload/picture/pic/116087/e393264c-4093-3378-8881-2b53e9dd7948.png[/img]
[img]http://dl.iteye.com/upload/picture/pic/116089/ba4552a8-cd54-305d-9ba4-a2c3b70031df.png[/img]