案例36-商品添加页面类别ajax显示
1 add.jsp代码
<%@ page language="java" pageEncoding="UTF-8"%>
<HTML>
<HEAD>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<LINK href="${pageContext.request.contextPath}/css/Style1.css" type="text/css" rel="stylesheet">
<script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-1.11.3.min.js"></script>
<script type="text/javascript">
$(function(){
//页面加载完毕后异步获得分类数据
$.post(
"${pageContext.request.contextPath }/admin?method=findAllCategory",
function(data){
//[{"cid":"xxx","cname":"xxx"},{},{}]
//拼接多个<option value=""></option>
var content="";
for(var i=;i<data.length;i++){
content += "<option value='"+data[i].cid+"'>"+data[i].cname+"</option>";
}
$("#cid").html(content);
},
"json"
);
});
</script>
</HEAD> <body>
<!-- -->
<form id="userAction_save_do" name="Form1" action="${pageContext.request.contextPath}/adminProduct_save.action" method="post" enctype="multipart/form-data">
<table cellSpacing="1" cellPadding="5" width="100%" align="center" bgColor="#eeeeee" style="border: 1px solid #8ba7e3" border="0">
<tr>
<td class="ta_01" align="center" bgColor="#afd1f3" colSpan="4"
height="26">
<strong><STRONG>添加商品</STRONG>
</strong>
</td>
</tr> <tr>
<td width="18%" align="center" bgColor="#f5fafe" class="ta_01">
商品名称:
</td>
<td class="ta_01" bgColor="#ffffff">
<input type="text" name="pname" value="" id="userAction_save_do_logonName" class="bg"/>
</td>
<td width="18%" align="center" bgColor="#f5fafe" class="ta_01">
是否热门:
</td>
<td class="ta_01" bgColor="#ffffff">
<select name="is_hot">
<option value="1">是</option>
<option value="0">否</option>
</select>
</td>
</tr>
<tr>
<td width="18%" align="center" bgColor="#f5fafe" class="ta_01">
市场价格:
</td>
<td class="ta_01" bgColor="#ffffff">
<input type="text" name="market_price" value="" id="userAction_save_do_logonName" class="bg"/>
</td>
<td width="18%" align="center" bgColor="#f5fafe" class="ta_01">
商城价格:
</td>
<td class="ta_01" bgColor="#ffffff">
<input type="text" name="shop_price" value="" id="userAction_save_do_logonName" class="bg"/>
</td>
</tr>
<tr>
<td width="18%" align="center" bgColor="#f5fafe" class="ta_01">
商品图片:
</td>
<td class="ta_01" bgColor="#ffffff" colspan="3">
<input type="file" name="upload" />
</td>
</tr>
<tr>
<td width="18%" align="center" bgColor="#f5fafe" class="ta_01">
所属分类:
</td>
<td class="ta_01" bgColor="#ffffff" colspan="3">
<select id="cid" name="cid"> </select>
</td>
</tr>
<tr>
<td width="18%" align="center" bgColor="#f5fafe" class="ta_01">
商品描述:
</td>
<td class="ta_01" bgColor="#ffffff" colspan="3">
<textarea name="pdesc" rows="5" cols="30"></textarea>
</td>
</tr>
<tr>
<td class="ta_01" style="WIDTH: 100%" align="center"
bgColor="#f5fafe" colSpan="4">
<button type="submit" id="userAction_save_do_submit" value="确定" class="button_ok">
确定
</button> <FONT face="宋体"> </FONT>
<button type="reset" value="重置" class="button_cancel">重置</button> <FONT face="宋体"> </FONT>
<INPUT class="button_ok" type="button" onclick="history.go(-1)" value="返回"/>
<span id="Label1"></span>
</td>
</tr>
</table>
</form>
</body>
</HTML>
2 AdminServlet代码
package www.test.web.servlet; import java.io.IOException;
import java.util.List; import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.google.gson.Gson; import www.test.domain.Category;
import www.test.service.AdminService; public class AdminServlet extends BaseServlet { // 1 页面加载完毕后异步获得分类数据
public void findAllCategory(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //提供一个List<Category>转成json字符串
AdminService service = new AdminService();
List<Category> categoryList = service.findAllCategory(); //将List<Category>转换成json格式
Gson gson = new Gson();
String json = gson.toJson(categoryList); //解决乱码并写出
response.setContentType("text/html;charset=UTF-8");
response.getWriter().write(json); }
}
3 AdminService代码
package www.test.service; import java.sql.SQLException;
import java.util.List; import www.test.dao.AdminDao;
import www.test.domain.Category; public class AdminService { // 获取分类
public List<Category> findAllCategory() {
AdminDao dao = new AdminDao();
try {
return dao.findAllCategory();
} catch (SQLException e) {
e.printStackTrace();
return null;
}
} }
4 AdminDao代码
package www.test.dao; import java.sql.SQLException;
import java.util.List; import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler; import www.test.domain.Category;
import www.test.utils.C3P0Utils; public class AdminDao { // 获取分类
public List<Category> findAllCategory() throws SQLException {
QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource());
String sql = "select * from category";
return qr.query(sql, new BeanListHandler<Category>(Category.class));
} }
案例36-商品添加页面类别ajax显示的更多相关文章
- 案例19-页面使用ajax显示类别菜单
1 版本一 版本只能在首页显示类别,当切换到了其它页面就不会显示 1 web层IndexServlet代码 package www.test.web.servlet; import java.io.I ...
- JAVAEE——宜立方商城09:Activemq整合spring的应用场景、添加商品同步索引库、商品详情页面动态展示与使用缓存
1. 学习计划 1.Activemq整合spring的应用场景 2.添加商品同步索引库 3.商品详情页面动态展示 4.展示详情页面使用缓存 2. Activemq整合spring 2.1. 使用方法 ...
- 页面进行ajax时 显示一个中间浮动loading
先发效果图,加载东西的时候如果没有设计或者其它提示会降低用户体验,所以写了个简单的loading弹层. 适用于触屏和pc页面. /* 页面进行ajax时 显示一个中间浮动loading @auther ...
- layui页面操作,点击一个添加页面,跳转有确定,然后点击确定后将选择的几个数据返回前一个页面获取值,然后ajax请求后台
custUserIndex.html [添加页面代码] <!DOCTYPE html> <html> <head> <meta charset="u ...
- jsp页面通过ajax取值/展示数据及分页显示
jsp页面通过ajax从后台获取数据,在页面展示,并实现分页效果代码: [JavaScript部分代码] 1 <script> function getComposition(pageno ...
- JS 返回上一页并刷新,但不用重新加载整个页面(ajax实现)
需求 有三个页面A.B.C,点击A=>B,点击B=>C,在C中添加内容,点击确定返回到B,此时B页面需重新加载新的内容.再次点击B的返回按钮,希望返回到A而不是C. ===== 2017/ ...
- 第04项目:淘淘商城(SpringMVC+Spring+Mybatis)【第九天】(商品详情页面实现)
https://pan.baidu.com/s/1bptYGAb#list/path=%2F&parentPath=%2Fsharelink389619878-229862621083040 ...
- ajax“显示弹窗详情”和“删除”功能练习
1.查看详细信息,以弹窗的形式显示,使用ajax 2.批量删除 “查询”功能可以参考前面的文章,这里只讲解ajax“显示弹窗详情”和“删除”功能 第一:在body中的代码 <title>a ...
- vue登录功能和将商品添加至购物车实现
2.1: 学子商城--用户登录 用户登录商城用户操作行为,操作用户输入用户名和密码 点击登录按钮,一种情况登录成功 一种情况登录失败 "用户名或密码有误请检查" 2.2:如何实现 ...
随机推荐
- small cell 在安防领域的应用探讨
在安防领域,最核心的问题是:如何有效区分“内部人员”与“外部人员”.所谓“有效”包含两点意思,一是安全,尽可能地过滤出“外部人员”.二是效率,即尽可能无干扰地或较小干扰地使“内部人员”通过.所有的安全 ...
- Dapper 简单封装
using System; using System.Collections.Generic; using System.Text; using Dapper; using System.Data; ...
- Win7 WPF程序无法接受外部拖拽
最近在WPF项目中遇到一个问题.虽然选择了AllowDrop = True,但是还是无法支持从外部拖拽文件到程序,倒是内部拖拽(如从一个列表拖拽到树)和从程序拖拽到外部可以. 解决过程 1.考虑是程序 ...
- HTML4.0 需要掌握的主要内容
常用标签: <html></html> 创建一个HTML文档 <head></head> 设置文档标题和其它在网页中不显示的信息 <title&g ...
- [转载] java中关于OOM的场景及解决方法
1.OOM for Heap=>例如:java.lang.OutOfMemoryError: Java heapspace[分析] 此OOM是由于JVM中heap的最大值不满足需要,将设置hea ...
- 苹果开发者账号提示“Unable to verify mobile phone number”的解决方案
在注册苹果开发者账号时,会提示:"Unable to verify mobile phone number.".顾名思义,没有有效的手机号码. 解决方案: 进入到Your Appl ...
- Visual odometry and zed's IMU fusion on RTAB-Map
"When using /camera/odom, you don't need to use visual_odometry node. rtabmap should be subscri ...
- php代码审计5审计命令执行漏洞
命令执行漏洞:通过易受攻击的应用程序在主机操作系统上执行任意命令,用户提供的数据(表单,cookie,http头等)未过滤 挖掘思路:用户能够控制函数输入,存在可执行代码的危险函数 命令执行和代码执行 ...
- servlet 核心技术
servlet 核心技术 servlet 生命周期 在 servlet 生命周期中,servlet 容器完成加载 servlet 类和实例化一个 servlet 实例,并通过3个方法来完成生命周期中的 ...
- 14. 异步加载Js的方式有哪些?
我们都知道渲染引擎遇到 script 标签会停下来,等到执行完脚本,继续向下渲染,如下: <script type="text/javascript" src=". ...