利用ssh(servlet+spring+hibernate)+ajax实现城区联动

  1. 需求:选中城市,会显示出对应城市的区的名字
  2. 在jsp页面,用到的是 < select>< option>标签,每个< option>有value属性,意思是指选中哪项,此select的value就是这个值.
    jsp页面:先获取所有的城市,通过el表达式显示在页面,区的显示就利用ajax实现.(通过调用.ajax({}) 函数)
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="js/jquery-1.8.3.js"></script>
<script type="text/javascript">
 $(function(){
     $(".dis").change(function(){
           $.ajax({

                url:"${pageContext.request.contextPath}/DisServlet?id="+$("#cityid").val(),
                async:false,
                 success: function(data) {

                     //这里如果服务器返回的是json,这里解析json,然后通过for循环等,jq中的append方法进行html元素追加
                     // json是js对象  json[i]是一个对象
                     var json = JSON.parse(data);

                     $("#dis2").empty();
                     for (var i = 0; i < json.length; i++) {
                         $("#dis2").append(
                                    "<option>"+json[i].dis_name+"</option>");
                    }

                } 
         })

     })
})
</script>
</head>

<body>
    <div>
        <select id="cityid" class="dis">
        <!-- <option></option> -->
            <c:forEach items="${list}" var="city">
                <option value="${city.city_id }">${city.city_name }</option>
            </c:forEach>
        </select>
        <select id="dis2">
        </select>
    </div>


</body>
</html>
  1. 注意上面的$(“#dis2”).empty();这里是把查询的数据给清空.

  2. for (var i = 0; i < json.length; i++) {

    (” #dis2”).append(“< option>”+json[i].dis_name+””);}  
    append()追加的时候不会把之前根据城市查询到的区给清空,为了清空,使用
    (“#dis2”).empty();append()是追加到dis2的select标签

web.xml中配置:

<!--指定spring xml存在的路径,根据路径初始化ioc容器-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

pom.xml中配置:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.lm</groupId>
    <artifactId>test_city</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
        <!-- json -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.8</version>
        </dependency>
        <!-- jstl -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/aopalliance/aopalliance -->
        <dependency>
            <groupId>aopalliance</groupId>
            <artifactId>aopalliance</artifactId>
            <version>1.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.10</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.3.8.RELEASE</version>
        </dependency>

        <!-- 1spring的基础jar包 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.8.RELEASE</version>
        </dependency>
        <!--spring对orm框架的支持包 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>4.3.8.RELEASE</version>
        </dependency>
        <!-- hibernate所使用的包 -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.1.0.Final</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <!-- mysql的connector包 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.24</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/javax/javaee-api -->
        <!-- 一些servlet等的基本类的包 -->
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>
        <!--JSON -->
        <!-- https://mvnrepository.com/artifact/net.sf.json-lib/json-lib -->

    </dependencies>

</project>

dao层注意实现接口,实现类中的事务处理使用属性HibernateTemplate属性,例如:
这里写图片描述

但是在spring.xml中,标签 < bean>中的class使用的是实现类的包名加实现类名 .但是在service层类里定义dao的属性的时候,要使用接口定义属性,这就是接口的用途.例如:这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
**smart-web2** 是一套的OA系统;包含了流程设计器,表单设计器,权限管理,简单报表管理等功能; 系统后端基于SpringMVC+Spring+Hibernate框架,前端页面采用JQuery+Bootstrap等主流技术; 流程引擎基于Snaker工作流;表单设计器基于雷劈网WEB表单设计器。 系统主要功能有: >1.系统管理 >>系统管理包含有:基础信息管理、系统权限管理、版本管理、子系统管理。 > >2.流程管理 >>流程管理包含有:流程设计器、流程实例管理、流程页面模版管理等功能。 > >3.表单管理 >>表单管理包含有:表单设计器、表管理、表单帮助信息管理等。 > >4.我的办公 >>我的待办、我的已办; > >5.简单报表管理 >>简单报表管理包含:简单报表的设计、报表管理等。 使用说明 ======= ------- ---数据库MySQL5.6以上 <br/> ---下载后把data目录下的smart-web2.zip解压;然后解压出来的脚本文件(“smart-web2.sql”)导入到mysql数据库中;注:建库时,字符集编码为:utf8(utf8_general_ci)<br/> ---修改配置文件“jdbc.properties”,改成对应数据库的用户名和密码 <br/> ---“sysconfig.properties”系统配置文件;需要修改“root.dir”属性,设置为系统上传文件时用来存放的根目录 <br/> ----系统管理员用户名为:admin;密码为:123456 <br/> ----linux类系统需要修改mysql的配置文件,改为数据库表名不区分大小写(lower_case_table_names=1) <br /> 环境要求 ------------ 1.jdk要求1.7及以上;<br /> 2.tomcat6或tomcat7; <br /> 3.eclipse版本4.4以上;<br /> 4.浏览器要求:IE8及以上(最理想的是IE10及以上),火狐,chrome等。<br />

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值