- 我们在写数据库的列名的时候注意不要与数据库中的关键字重复,如desc等等,可以加上表名前缀来避免,例如:stu_price
- 在jsp页面中我们如果想要获取某个button,在导入原生的js的库即可,不需要再写很多document.getElementById()
<button class="am-btn am-btn-default" type="button" id="add">添加</button>
例如我们想要获取这个button
<script>
$("#add").click(function(){
//可以获取该按钮的表单,加入该按钮的表单id="add_form"
//使用该方法即可提交
$("#add_form").submit();
})
</script>
- 如果我们在servlet中向数据库中写入数据乱码的话,在配置文件db.properties中
//在url后加入useUnicode=true&characterEncoding=utf-8即可
jdbc:mysql://localhost:3306/mystore?useUnicode=true&characterEncoding=utf-8&useSSL=false&rewriteBatchedStatements=true&serverTimezone=UTC
- 集合的反转
List<Goods> allGoods = goodsService.getAllGoods();
//对集合进行反转
Collections.reverse(allGoods);
集合反转可以用到:在servlet中向数据库中存储商品,并且展示到页面上的时候。
不反转的情况下:我们插入的商品会展示到最后一列
反转后:我们每插入一个商品,都插入到第一行。
- 在编辑商品的时候,若需要获取商品的id来更新该商品,但是该页面中并没有id的标签,我们可以使用一个小技巧,在页面中可以写入一个id的标签,但是使用style让他不显示:
<input type="text" neme = "id" value="${goods.id }" style="display: none">
这样就可以传入id,但同时这个标签也不会显示在页面中
- 我们在编辑商品列表的时候,需要回显数据,但是当需要回显下拉列表的时候,代码如下:
<!--
首先导入原生的js库,用于标记标签
-->
<script src="${ctx }/admin/js/jquery.min.js"></script>
<script>
$(function (){
//让value的值等于商品is_hot的option成为选中状态
$("#isHotSel option[value=${goods.is_hot}]").prop("selected",true);
$("#category_select option[value=${goods.cid}]").prop("selected",true);
})
</script>
- 我们在商品管理的时候,创建了六个servlet,添加商品,更新商品等等,所以需要创建一个通用的servlet来更加方便管理。
- 首先是jsp页面,只有三个a标签,添加,删除,更新
//需要再后边加上?action=add来向servlet发送一个参数
<a href='${pageContext.request.contextPath }/TestServlet?action=add'>添加</a>
<a href='${pageContext.request.contextPath }/TestServlet?action=del'>删除</a>
<a href='${pageContext.request.contextPath }/TestServlet?action=update'>更新</a>
- 再写一个通用servlet
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//首先获取jsp页面的请求参数,来判断是哪个操作
String action = request.getParameter("action");
//使用if语句来判断是那个操作
if("add".equals(action)) {
path = add(request,response);
}else if("del".equals(action)) {
path = del(request,response);
}else if("update".equals(action)) {
path = update(request,response);
}
//然后再进行跳转,这里就不写跳转的界面了,懂得逻辑就好
request.getRequestDispatcher(path).forward(request, response);
}
//需要在这个servlet下边写三个方法,添加删除更新
//这里只写添加,另外两个省略
public String add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
System.out.println("添加");
return "add.jsp";
}
上边的通用servlet的if语句太多,较难管理,于是我们可以使用反射进行改进:
//首先接收jsp页面发来的参数
String action = request.getParamete("action");
try {
//获取该对象的字节码
Class clazz = this.getClass();
Method method = clazz.getMethod(action,, HttpServletRequest.class,HttpServletResponse.class);
//判断有没有传入的方法
if(method != null){
String path = (String)method.invoke(this,request,response);
//判断path是否为空
if(path != null){
//不为空,则可以跳转该方法(更新或删除或添加)
request.getRequestDispatcher(path).forward(request, response);
}
}
} catch (Exception e) {
e.printStackTrace();
}
/* 需要注意,反射调用的方法必须为public,否则会报No Such Method的错误 */
//需要在这个servlet下边写三个方法,添加删除更新
//这里只写添加,另外两个省略
public String add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
System.out.println("添加");
return "add.jsp";
}