一些经常会忽视的问题:
1、在服务端返回json数据到客户端时,一定要记得设置返回数据的类型
response.setContentType("application/json;charset=utf-8");
2、路径写法:
1. 路径分类
1. 相对路径:通过相对路径不可以确定唯一资源
* 如:./index.html
* 不以/开头,以.开头路径
* 规则:找到当前资源和目标资源之间的相对位置关系
* ./:当前目录
* ../:后退一级目录
2. 绝对路径:通过绝对路径可以确定唯一资源
* 如:http://localhost/day15/responseDemo2 /day15/responseDemo2
* 以/开头的路径
* 规则:判断定义的路径是给谁用的?判断请求将来从哪儿发出
* 给客户端浏览器使用:需要加虚拟目录(项目的访问路径)
* 建议虚拟目录动态获取:request.getContextPath()
如重定向:
response.sendRedirect(request.getContextPath()+"/login.html");
* <a> , <form> 重定向...
* 给服务器使用:不需要加虚拟目录
* 转发路径
3、在使用ajax 异步交互时,对于表单数据,可以使用类似方法:
$(function () {
//1.给登录按钮绑定单击事件
$("#btn_sub").click(function () {
//2.发送ajax请求,提交表单数据
$.post("user/login",$("#loginForm").serialize(),function (data) {
//data : {flag:false,errorMsg:''}
if(data.flag){
//登录成功
location.href="index.html";
}else{
//登录失败
$("#errorMsg").html(data.errorMsg);
}
});
});
});
// 注意:
1)、这里$("#loginForm").serialize(),将js 对象转为jquery 对象 然后将表单数据封装为:
username=zhangsan&password=123 格式
2)location.href="index.html"; 可以使用这个方法在前端页面跳转到其他页面
4、在拼接字符串时,先看主体时单引还是双引号,若主体是单,则我们拼接用双,主体是双,我们拼接用单
例如:首先写''
然后再引号内部++ 效果'++'
然后在++,里面动态拼接,'+user.name+'
$.get("category/findAll",{},function (data) {
//[{cid:1,cname:国内游},{},{}]
var lis = '<li class="nav-active"><a href="index.html">首页</a></li>';
//遍历数组,拼接字符串(<li>)
for (var i = 0; i < data.length; i++) {
var li = '<li><a href="route_list.html">'+data[i].cname+'</a></li>';
lis += li;
}
//拼接收藏排行榜的li,<li><a href="favoriterank.html">收藏排行榜</a></li>
lis+= '<li><a href="favoriterank.html">收藏排行榜</a></li>';
//将lis字符串,设置到ul的html内容中
$("#category").html(lis);
});
5、mysql 实现分页,limit start,size
start:从哪条记录开始
size:查询几条数据
可以封装一个pageBean 对象,要查询到的数据封装到里面,再序列化为json 在页面取值。
例如:
import java.util.List;
/**
* 分页对象
*/
public class PageBean<T> {
private int totalCount;//总记录数 需要从数据库查询
private int totalPage;//总页数 根据总记录数和每页显示的条数计算,条数可从页面传
private int currentPage;//当前页码 从页面传递
private int pageSize;//每页显示的条数
private List<T> list;//每页显示的数据集合 从数据库查询
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
}
6、前端页面从一个html页面到另外一个html传递数据,可以如下:
1)在url 路径后面跟上要传递的数据:
var li = '<li><a href="route_list.html?cid='+data[i].cid+'">'+data[i].cname+'</a></li>';
2)在另外一个页面通过该方法,拿到该参数
$(function () {
var search = location.search;
//alert(search);//?id=5
// 切割字符串,拿到第二个值
var cid = search.split("=")[1];
});
或者 使用下面这方法
//根据传递过来的参数name获取对应的值
function getParameter(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)","i");
var r = location.search.substr(1).match(reg);
if (r!=null) return (r[2]); return null;
}
7、前端页面可以使用 window.decodeURIComponent(rname); 解码
后续再更新---