----------------解耦
- 路徑src下用loader(BeanFactory.class.getClassLoader().getResource("bean.xml").getPath();),web路徑使用getrealpath(String path = this.getServletContext().getRealPath("upload");)
- dom4j
-------------xml中配置,id為尋找,class為所要創建的實際地址
<?xml version="1.0" encoding="UTF-8"?>
<beans>
<!-- 配置AdminServiceImpl的清單 -->
<bean id="adminService" class="com.itheima.service.impl.AdminServiceImpl"></bean>
<!-- <bean id="adminService" class="com.itheima.service.impl.AdminServiceImpl2"></bean> -->
<bean id="adminDao" class="com.itheima.dao.impl.AdminDaoImplMySql"></bean>
<!-- <bean id="adminDao" class="com.itheima.dao.impl.AdminDaoImplOracle"></bean> -->
</beans>
----------------------使用
//用解耦合的方式進行編碼----解web層與service層的耦合
//使用工廠+反射+配置文件
AdminService service = (AdminService) BeanFactory.getBean("adminService");
-------------------------BeanFactory
public static Object getBean(String id){
//生產對象---根據清單生產----配置文件----將每一個bean對象的生產的細節配到配置文件中
//使用dom4j的xml解析技術
try {
//1、創建解析器
SAXReader reader = new SAXReader();
//2、解析文檔---bean.xml在src下
String path = BeanFactory.class.getClassLoader().getResource("bean.xml").getPath();
Document doc = reader.read(path);
//3、獲得元素---參數是xpath規則
Element element = (Element) doc.selectSingleNode("http://bean[@id='"+id+"']");
//<bean id="adminService" class="com.itheima.service.impl.AdminServiceImpl"></bean>
String className = element.attributeValue("class");
//com.itheima.service.impl.AdminServiceImpl
//使用反射創建對象
Class clazz = Class.forName(className);
Object object = clazz.newInstance();
return object;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
Paste_Image.png
Paste_Image.png
----------------后臺
- ajax異步加載需要拼接字符串 $("#shodDivOid").html("");$("#loading").css("display","block");
//ajax異步訪問數據
//根據訂單id查詢訂單項和商品信息
public void findOrderInfoByOid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//獲得oid
String oid = request.getParameter("oid");
//用解耦合的方式進行編碼----解web層與service層的耦合
//使用工廠+反射+配置文件
AdminService service = (AdminService) BeanFactory.getBean("adminService");
List<Map<String,Object>> mapList = service.findOrderInfoByOid(oid);
Gson gson = new Gson();
String json = gson.toJson(mapList);
System.out.println(json);
/*[
* {"shop_price":4499.0,"count":2,"pname":"聯想(Lenovo)小新V3000經典版","pimage":"products/1/c_0034.jpg","subtotal":8998.0},
* {"shop_price":2599.0,"count":1,"pname":"華為 Ascend Mate7","pimage":"products/1/c_0010.jpg","subtotal":2599.0}
*]*/
response.setContentType("text/html;charset=UTF-8");
response.getWriter().write(json);
}
---------------------------
function findOrderInfoByOid(oid){
//清理上一次顯示的內容覆蓋
$("#showDivTab").html("");
$("#shodDivOid").html("");
$("#loading").css("display","block");
//ajax異步訪問數據
$.post(
"${pageContext.request.contextPath }/admin?method=findOrderInfoByOid",
{"oid":oid},
function(data){
//隱藏加載圖片
$("#loading").css("display","none");
/*[
* {"shop_price":4499.0,"count":2,"pname":"聯想(Lenovo)小新V3000經典版","pimage":"products/1/c_0034.jpg","subtotal":8998.0},
* {"shop_price":2599.0,"count":1,"pname":"華為 Ascend Mate7","pimage":"products/1/c_0010.jpg","subtotal":2599.0}
*]*/
var content = "<tr id='showTableTitle'><th width='20%'>圖片</th><th width='25%'>商品</th><th width='20%'>價格</th><th width='15%'>數量</th><th width='20%'>小計</th></tr>";
for(var i=0;i<data.length;i++){
content+="<tr style='text-align: center;'>"+
"<td>"+
""+
"</td>"+
"<td><a target='_blank'>"+data[i].pname+"</a></td>"+
"<td>¥"+data[i].shop_price+"</td>"+
"<td>"+data[i].count+"</td>"+
"<td><span class='subtotal'>¥"+data[i].subtotal+"</span></td>"+
"</tr>";
} $("#showDivTab").html(content);
//訂單編號
$("#shodDivOid").html(oid);
},
"json" ); }
- 【{“xxx”:"aaa"},{}】
- 兩表聯查
Paste_Image.png
- ajax異步是jquery中一部分
- el整體是一個字符串不是變量“${order.oid}”,相對應的json字符串在自己拼時候雙引號只能反義符來確定而不是單引
- ctrl+h filesearch搜索文件
- 一個小點是注意檢查name是否對應
- jquery高版本低版本有時候方法不兼容,例如layer在1.8中如果僅有1.11可能會出現問題?
- jquery-script
- 多表單提交enctype則使用request就會失效,因此不能用parameter來獲取值,也就不能使用name,value格式上傳值,解決方法:重新寫一個servlet
- 存圖片放路徑最好不要寫工程名,而寫工程下某一文件夾(因為不容易改動)