2010.03.02(2)——google map api 带有分页的标注
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>简单分页</title>
<script type="text/javascript" src="http://ditu.google.cn/maps?
file=api&v=2&key=ABQIAAAAMrMsJOafJ9iQiv3u_76UbBT2yXp_ZAY8_ufC3CFXhHIE1NvwkxQYvggBkgDawaMNCy_renoUB9_3FA&sensor=true_o
r_false"></script>
<script type="text/javascript" src="js/jquery-1.3.2.js"></script>
<script type="text/javascript">
//用于和后台的ajax请求
function fn5(start,number){
$.ajax({
type: "POST",
url: "student_page.do",
data: {start:start,number:number},
dataType: "json",
success: function(data){
$.each(data,function(i,o){
$("#tr"+(i+1)+" td:eq(0)").text(o["id"]);
$("#tr"+(i+1)+" td:eq(1)").text(o["name"]);
$("#tr"+(i+1)+" td:eq(2)").text(o["age"]);
});
}
});
}
$(function(){
if(GBrowserIsCompatible()){//判断浏览器兼容性
var map = new GMap2(document.getElementById("map_canvas"));
map.setCenter(new GLatLng(34.6156,112.4450),13);
map.addControl(new GLargeMapControl());
map.addControl(new GMapTypeControl());
map.enableScrollWheelZoom();
var str1 = "<div style=\"width:200px; height:220px;\"><table border=\"2\" bordercolor=\"red\"
cellspacing=\"0\" align=\"center\" ><tr><td colspan=\"3\" align=\"center\">人工岛工程</td></tr><tr><td>ID</td><td>名字
</td><td>年龄</td></tr><tr id=\"tr1\"><td></td><td></td><td></td></tr><tr id=\"tr2\"><td></td><td></td><td></td></tr><tr
id=\"tr3\"><td></td><td></td><td></td></tr><tr id=\"tr4\"><td></td><td></td><td></td></tr><tr align=\"center\"><td
colspan=\"3\"><input type=\"button\" value=\"上一页\" onclick=\"\" /> <input type=\"button\" value=\"下一页\"
onclick=\"\"/></td></tr></table></div>";
var str2 = "<select><option>北京</option><option>天津</option><option>河北</option></select>";
var marker = new GMarker(map.getCenter(),{title:"呵呵"});
map.addOverlay(marker);
GEvent.addListener(marker,"click",function(latLng){
var str3 = "经度: "+latLng.lng()+",纬度: "+latLng.lat();
marker.openInfoWindowTabsHtml([new GInfoWindowTab("标签一", str1),new GInfoWindowTab("标签二", str3)]
);
fn5(0,4);
});
}
$(window).unload(function(){
GUnload();
})
});
</script>
</head>
<body>
<div id="map_canvas" style="width:500px; height:500px;"></div>
</body>
</html>
web.xml
<?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">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
spring.xml
<?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: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-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-
2.5.xsd"
default-autowire="byName">
<!--
<aop:aspectj-autoproxy proxy-target-class="true"/>
-->
<!-- 数据库外部文件配置 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list><value>classpath:db.properties</value></list>
</property>
<property name="fileEncoding" value="utf-8" />
</bean>
<!-- 数据库外部文件配置 -->
<!-- 配置数据源 使用dbcp数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- 配置数据源 使用dbcp数据源 -->
<!-- Hibernate SessionFactory配置 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>pojo.Student</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.OracleDialect
</prop>
<prop key="show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.use_sql_comments">true</prop>
</props>
</property>
</bean>
<!-- Hibernate SessionFactory配置 -->
<!-- hibernateTemplate -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- hibernateTemplate -->
<!-- dao -->
<bean id="studentDao" class="dao.StudentDaoImp" >
<property name="hibernateTemplate">
<ref bean="hibernateTemplate" />
</property>
</bean>
<!-- dao -->
<!-- serice -->
<bean id="studentService" class="service.StudentServiceImp" >
<property name="studentDao" >
<ref bean="studentDao" />
</property>
</bean>
<!-- serice -->
<!-- spring MVC -->
<bean id="studentDelegate" class="control.StudentControl">
<property name="studentService"><ref bean="studentService"/></property>
</bean>
<bean id="studentController" class="org.springframework.web.servlet.mvc.multiaction.MultiActionController">
<property name="methodNameResolver" ref="propMethodNameResolver"/>
<property name="delegate" ref="studentDelegate"/>
</bean>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/student_*.do">studentController</prop>
</props>
</property>
</bean>
<bean id="propMethodNameResolver"
class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
<property name="mappings">
<props>
<prop key="/student_page.do">page</prop>
<prop key="/student_latLng.do">latLng</prop>
</props>
</property>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"><value>org.springframework.web.servlet.view.JstlView</value></property>
<property name="prefix"><value></value></property>
<property name="suffix"><value>.jsp</value></property>
<property name="contentType"><value>text/html;charset=utf-8</value></property>
</bean>
<!-- spring MVC -->
</beans>
pojo
package pojo;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="STUDENT")
public class Student {
@Id
@Column(name="student_id")
private Integer id;
private String name;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
dao
package dao;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import pojo.Student;
public class StudentDaoImp implements StudentDao{
private HibernateTemplate hibernateTemplate;
public HibernateTemplate getHibernateTemplate() {
return hibernateTemplate;
}
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
public List<Student> page(final int start,final int number){
return (List<Student>) this.hibernateTemplate.execute(new HibernateCallback(){
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery("from Student s order by s.id");
query.setFirstResult(start);
query.setMaxResults(number);
return query.list();
}
});
}
}
service
package service;
import java.util.List;
import pojo.Student;
import dao.StudentDao;
public class StudentServiceImp implements StudentService{
private StudentDao studentDao;
public StudentDao getStudentDao() {
return studentDao;
}
public void setStudentDao(StudentDao studentDao) {
this.studentDao = studentDao;
}
public List<Student> page(final int start,final int number){
return this.studentDao.page(start, number);
}
}
ctroller
package control;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import org.springframework.web.servlet.ModelAndView;
import pojo.Student;
import service.StudentService;
public class StudentControl {
private StudentService studentService;
public StudentService getStudentService() {
return studentService;
}
public void setStudentService(StudentService studentService) {
this.studentService = studentService;
}
public ModelAndView page(HttpServletRequest request,
HttpServletResponse response) throws Exception {
String start = request.getParameter("start");
String number = request.getParameter("number");
if(start==null) start = "0";
if(number==null) number = "4";
Integer s = 0;
Integer n = 0;
try{
s = Integer.parseInt(start);
n = Integer.parseInt(number);
}catch(Exception e){
s = 0;
n = 4;
}
List<Student> students = this.studentService.page(s,n);
PrintWriter out = response.getWriter();
JSONArray json = JSONArray.fromObject(students);
System.out.println(json.toString());
out.print(json.toString());
return null;
}
}