尚学堂shopping的经验

来源:互联网 发布:linux sem wait 编辑:程序博客网 时间:2024/04/30 04:44

本人在看尚学堂的视频时写下的经验, 故分享分享:

1. 一般,我们要写项目的路径时,我们的项目可能拿到其他的机器里运行, 故应该改为如下,

http://localhost:8080/项目的名称

=request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() +

request.getContextPath()

 

2.request.get的方法传的编码是:8859_1, 可用以下语句更改:

String keyword = new String(request.getParameter("keyword").getBytes("8859_1"), "GBK");

 

3.当提示类型不匹配时(类型名相同), 很可能是包引入错误.

 

 

4在调用TV20.js文件里的treeview_box_0_none = "images/4_clos.gif"(路径)  ;url是相对于调用它的jsp文件的相对路径.

 

 

5.面向接口:    例如: shopping中做productsDAO, 为方便用 不同的数据库(SQL, Oracle), 在建立DAO层时,采用面向接口的方法, 建立productDAO.java interface. 然后由不用的数据库DAO层继

(productMySQLDAO.java productOracleDAO.java ) .这样, 就不用避免了重写productDAO.java中数据与数据库代码的组织.  在做DAO层时, 推荐该方法.这也是有名的工厂模式

 

6.单例模式:   1,写静态方法.

                     2, 在该类构造方法中new成私有的, 使得别人无法new它的对象, 然后提供一静态方法为接口供访问(即返回该类的对象). 例如:shoppingproductMgr类中就采用该方法, 其对外提供的接口为: getInstance();

 

7.在发现数据库中数据显示为乱码时, 因成数据的生成, 传送, 到数据库的存储间, 一步步的寻找编码问题.

 

8.MySQL, 数据库编码的问题可由两种方法解决:

              , 开始->程序->……My server Instance config Wizard重新做设置设定;

              , MySQL安装处的my.ini文件里:

              [client]

port=3306

 

[mysql]

 

default-character-set=GBK   此处为客户端的编码(即是显示给你看的窗口处), 可改为GBK(只能显示中文和英文)

#Path to the database root

datadir="C:/Program Files/MySQL/MySQL Server 5.0/Data/"

 

# The default character set that will be used when a new schema or table is

# created and no character set is defined

default-character-set=utf8

该处为数据库内部的编码, 强烈不建议改动, UTF8是国际编码, 可显示各个国家的编码.!

 

: 数据库编码的改动不能影响之前存储的数据.

 

9. 关于分页:

              网页中显示记录经常用到分页, 在分页技术中:

              , 数据库语法: 分页: select * from article limit pageNo, pageSize;

              , 在写关于 下一页, 上一页功能时,

可将pageSize 设置为该方法:private static final int PAGE_SIZE = 3;

在接受PageNo, 还应该判断是否小于1 ,若小于1, 应该重新赋值为1;

 

 

10.get方法提交

<a href = "productsearch.jsp?action=<%=action%>&keyword=<%=keyword%>&lownormalprice=<%=lownormalprice%>&highnormalprice=<%=highnormalprice%>&lowmemberprice=<%=lowmemberprice%>&highmemberprice=<%=highmemberprice%>&startDate=<%=startDate%>&endDate=<%=endDate%>&categoryid=<%=categoryid%>&pageno=<%=pageNo + 1%>">下一页</a>

 

在做String strStartDate = request.getParameter("startDate");接收时, 系统会把它当作startDate = null, 于是,strStartDate = null

 

11.关于页面的跳转

    一.response.sendRedirect().该方法会实现页面跳转,但会丢失数据, 即不能传送数据。(pose, get方法的数据)

    二.<jsp:forward page=”complexsearchresult.jsp“></jsp:forward> 这个便可以传送数据了。

 

12.session的典型运用保存.

       例如: shopping视频41 buy.jsp,

一.   Cart cart = (session)session.getAttribute(“cart”);

session.setAttribute(“cart”, cart);

                     .<jsp:useBean id = “cart” type=”com.shopping.Cart” scop = “session”></jsp:useBean>

              13.如何在input中利用name 传递 数值:

              例如: shopping 45视频cart.jsp,     

input中填补后传递给另一个页面时, 若需要同时传递id value, 可用name传递id, 写法如下:

<input type = text size=4 name = “<%= “p” + ci.getProductId() %>” value = “<% ci.getCount %>                     // 加上 p 是为了让它变成字符后, 然后才可以传递

接受可用:

String strProductId =  request. getParameter( “p” + ci.getProductId());

然后直接把strProductId Integer.parseInt化即可拿到该值.

 

原创粉丝点击