SSH框架網上商城項目第10戰之搭建商品類根本模塊。本站提示廣大學習愛好者:(SSH框架網上商城項目第10戰之搭建商品類根本模塊)文章只能為提供參考,不一定能成為您想要的結果。以下是SSH框架網上商城項目第10戰之搭建商品類根本模塊正文
後面我們完成了與商品種別相干的營業邏輯,接上去我們開端做詳細商品部門。
1. 數據庫建表並映照Model
起首我們在數據庫中新建一張表,然後應用逆向工程將表映照成Model類,表以下:
/*=============================*/ /* Table: 商品表構造 */ /*=============================*/ create table product ( /* 商品編號,主動增加 */ id int primary key not null auto_increment, /* 商品稱號 */ name varchar(20), /* 商品價錢 */ price decimal(8,2), /* 商品圖片 */ pic varchar(200), /* 商品簡略引見 */ remark longtext, /* 商品具體引見 */ xremark longtext, /* 商品臨盆日期 */ date timestamp default CURRENT_TIMESTAMP, /* 能否為推舉商品,推舉商品才有能夠顯示在商城首頁 */ commend bool, /* 能否為有用商品,有用商品才有能夠顯示在商城首頁 */ open bool, /* 商品地點的種別編號*/ cid int, constraint cid_FK foreign key(cid) references category(id) );
應用逆向工程映照為Model類就不贅述了,後面有提到若何應用逆向工程生成Model。
2. 完成商品類的Service層和Action的架構
2.1 商品類的Service層架構
與後面category一樣,product也得有個service來操作與商品相干的營業邏輯,所以我們得寫一個ProductService和ProductServiceImpl的架構出來,詳細以下:
//ProductService接口繼續BaseService<Product>
public interface ProductService extends BaseService<Product> {
}
//ProductServiceImpl完成類繼續BaseServiceImpl<Product>,並完成下面的ProductService接口
@Service("productService")
public class ProductServiceImpl extends BaseServiceImpl<Product> implements ProductService {
}
2.2 商品類的Action架構
起首得完美一下BaseAction中關於Service層的注解
@Controller("baseAction")
@Scope("prototype")
public class BaseAction<T> extends ActionSupport implements RequestAware,SessionAware,ApplicationAware,ModelDriven<T> {
@Resource
protected ProductService productService;
//其他代碼省略,照樣本來的代碼……
}
然後我們寫一個ProductAction繼續該辦法:
public class ProductAction extends BaseAction<Product> {
}
至此,關於商品的後台架構就根本搭建好了,接上去就是完美外面的詳細功效和營業邏輯了。
3. 完成前台的根本構造
前台的根本構造和商品類的一樣,我們看一下曾經完成的商品類的前台都有哪些文件:
我們先依據其商品類的前台文件,拷貝一份到product文件夾中,然後我們再做響應的修正。先來剖析一下賤程:起首index.jsp到aindex.jsp顯示左邊菜單欄,當點擊種別治理時,進入category/query.jsp頁面右邊顯示一切商品種別信息,搜刮和刪除功效均在此頁面,不須要彈出新的窗口,添加彈出save.jsp窗口,更新彈出update.jsp窗口。當點擊商品治理的時刻,進入product/query.jsp頁面右邊顯示一切商品信息,搜刮和刪除功效均在此頁面完成,添加和更新分離彈出save.jsp和update.jsp。接上去我們把各個頁面的框架搭建好,然後往響應的部門填器械便可。
起首在aindex.jsp中添加以下代碼:
接上去,我們完成query.jsp的框架:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<%@ include file="/public/head.jspf" %>
<style type="text/css">
body {
margin: 1px;
}
.searchbox {
margin: -3;
}
</style>
<script type="text/javascript">
$(function(){
$('#dg').datagrid({
//url地址改成要求productAction中的queryJoinCategory辦法
url:'product_queryJoinCategory.action',
loadMsg:'Loading......',
queryParams:{name:''},//這裡參數改成name,參數值為空,表現我們要顯示一切商品,後台是依據商品name屬性查詢的
//width:300,
fitColumns:true,
striped:true,
nowrap:true,
singleSelect:false,
pagination:true,
pageSize:5,
pageList:[5,10,15,20],
idField:'id',//指定id為標識字段,在刪除,更新的時刻有效,假如設置裝備擺設此字段,在翻頁時,換頁不會影響選中的項
//toolbar界說添加、刪除、更新按鈕和搜刮框
toolbar: [{
iconCls: 'icon-add',
text:'添加商品',
handler: function(){
//添加觸發代碼
}
},'-',{
iconCls: 'icon-edit',
text:'更新商品',
handler: function(){
//添加觸發代碼
}
},'-',{
iconCls: 'icon-remove',
text:'刪除商品',
handler: function(){
//添加觸發代碼
}
},'-',{ //查詢按鈕不是LinkButton,它有語法,然則也支撐解析HTML標簽
text:"<input id='ss' name='serach' />"
}],
rowStyler: function(index,row){
console.info("index" + index + "," + row)
if(index % 2 == 0) {
return 'background-color:#fff;';
} else {
return 'background-color:#c4e1e1;';
}
},
frozenColumns:[[
{field:'checkbox',checkbox:true},
{field:'id',title:'商品編號',width:100}
]],
columns:[[
{field:'name',title:'商品稱號',width:100},
{field:'price',title:'商品價錢',width:100},
{field:'remark',title:'簡略描寫',width:100},
{field:'xremark',title:'具體描寫',width:100},
{field:'date',title:'上架時光',width:100},
{field:'commend',title:'推舉商品',width:100,
formatter: function(value,row,index){
if(value) {
return "<input type='checkbox' checked='checked' disabled='true'";
} else {
return "<input type='checkbox' disabled='true'";
}
}
},
{field:'open',title:'有用商品',width:100,
formatter: function(value,row,index){
if(value) {
return "<input type='checkbox' checked='checked' disabled='true'";
} else {
return "<input type='checkbox' disabled='true'";
}
}
},
{field:'category.type',title:'所屬商品種別',width:200, //category.type是商品種別
formatter: function(value,row,index){
if(row.category != null && row.category.type != null) {
return row.category.type; //假如商品種別不為空,前往商品種別
} else {
return "此商品臨時未分類";
}
}
}
]]
});
//把通俗的文本框轉化為查詢搜刮文本框
$('#ss').searchbox({
//觸發查詢事宜
searcher:function(value,name){ //value表現輸出的值
//添加觸發代碼
},
prompt:'請輸出搜刮症結字'
});
});
</script>
</head>
<body>
<table id="dg"></table>
</body>
</html>
接上去我們完成productAction中的queryJoinCategory辦法,在這之前,先要完成service部門,我們都是先從底層漸漸往上開辟的:
//ProductService接口
public interface ProductService extends BaseService<Product> {
//查詢商品信息,級聯種別
public List<Product> queryJoinCategory(String type, int page, int size); //應用商品的稱號查詢
//依據症結字查詢總記載數
public Long getCount(String type);
}
@SuppressWarnings("unchecked")
@Service("productService")
public class ProductServiceImpl extends BaseServiceImpl<Product> implements ProductService {
@Override
public List<Product> queryJoinCategory(String name, int page, int size) {
String hql = "from Product p left join fetch p.category where p.name like :name";
return getSession().createQuery(hql)
.setString("name", "%" + name + "%")
.setFirstResult((page-1) * size) //從第幾個開端顯示
.setMaxResults(size) //顯示幾個
.list();
}
@Override
public Long getCount(String name) {
String hql = "select count(p) from Product p where p.name like :name";
return (Long) getSession().createQuery(hql)
.setString("name", "%" + name + "%")
.uniqueResult(); //前往一筆記錄:總記載數
}
}
上面可以完成productAction中的queryJoinCategory辦法了:
@Controller("productAction")
@Scope("prototype")
public class ProductAction extends BaseAction<Product> {
public String queryJoinCategory() {
System.out.println("name:" + model.getName());
System.out.println("page:" + page);
System.out.println("rows:" + rows);
//用來存儲分頁的數據
pageMap = new HashMap<String, Object>();
//依據症結字和分頁的參數查詢響應的數據
List<Product> productList = productService.queryJoinCategory(model.getName(), page, rows);
pageMap.put("rows", productList); //存儲為JSON格局
//依據症結字查詢總記載數
Long total = productService.getCount(model.getName());
// System.out.println(total);
pageMap.put("total", total); //存儲為JSON格局
return "jsonMap";
}
}
接上去在struts.xml中停止設置裝備擺設,跟之前的商品類一樣的流程,到這裡可以看出,開辟好了一個,上面一個就快了:
<action name="product_*" class="productAction" method="{1}">
<result name="jsonMap" type="json">
<param name="root">pageMap</param>
<param name="excludeProperties">
<!-- rows[0].category.account -->
<!-- 把一切account過濾失落,不然會湧現懶加載成績,該部門上面截圖 -->
</param>
</result>
</action>
如許後台法式寫好了,然後開啟tomcat,測試一下,當我們點擊左邊菜單欄的商品治理時,會彈出左邊以下窗口:
如許我們就完成了商品治理窗口的框架了。
原文地址:http://blog.csdn.net/eson_15/article/details/51354932
以上就是本文的全體內容,願望對年夜家的進修有所贊助,也願望年夜家多多支撐。