论坛首页 Java企业应用论坛

发布一个基于Spring小工具postmark

浏览 9342 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (2) :: 隐藏帖 (0)
作者 正文
   发表时间:2012-07-28   最后修改:2012-07-31
(一) 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接受的请求参数
功能methodshortNamecssClassidname
得到所有的省级行政区option标签getAllLevelOnetrue或者false 默认为false任意不需要不需要
通过省级行政区ID得到市(县)option标签getLevelTwoByParentIdtrue或者false 默认为false任意省级行政区ID不需要
通过省级行政区名称或简称得到市(县)option标签getLevelTwoByParentNametrue或者false 默认为false任意不需要名称或简称

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) 效果图


   发表时间:2012-07-30  
三沙市都有了……更新够快的说
0 请登录后投票
   发表时间:2012-07-30  
peak 写道
三沙市都有了……更新够快的说

这个小屁玩意是我最近在做的嘛。 还热乎的呢。
0 请登录后投票
   发表时间:2012-07-30  
好久木见了,原来在做这个
0 请登录后投票
   发表时间:2012-07-30  
jinnianshilongnian 写道
好久木见了,原来在做这个

没啦。 我最近工作特别忙,没怎么上javaeye。

你没出新的文章呢? 也忙么?
0 请登录后投票
   发表时间:2012-07-30  
yingzhor 写道
jinnianshilongnian 写道
好久木见了,原来在做这个

没啦。 我最近工作特别忙,没怎么上javaeye。

你没出新的文章呢? 也忙么?

出了 跟着开涛学springMVC
0 请登录后投票
   发表时间:2012-07-30  
1、跟spring插件没关系吧
2、postmark.sql有了,数据呢?
0 请登录后投票
   发表时间:2012-07-30  
jinnianshilongnian 写道
yingzhor 写道
jinnianshilongnian 写道
好久木见了,原来在做这个

没啦。 我最近工作特别忙,没怎么上javaeye。

你没出新的文章呢? 也忙么?

出了 跟着开涛学springMVC


哈哈。 你的文章我一定看。等我有时间就看。
0 请登录后投票
   发表时间:2012-07-30  
javeaye 写道
1、跟spring插件没关系吧
2、postmark.sql有了,数据呢?


1) 这个叫法我觉得还是妥当的,我提供的功能是springframework.org没有提供的,而我实现的功能又要依赖spring。
称其为spring插件,我个人认为没什么不妥。

2) 正如你看到的数据在sql脚本里。 实际上我的postmark直接在启动时把数据加载到hsqldb嵌入式数据库里,然后用sql语言查询。 仅此而已。
0 请登录后投票
   发表时间:2012-07-30  
哎, 都是泡妞高手。
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics