Before, we completed the business logic related to product categories, and then we started to do the specific product part.
1. Create tables in the database and map models
First, we create a new table in the database, and then use reverse engineering to map the table into a Model class, the table is as follows:
/*===========================================================================================================================================/ create table product ( /* Product number, automatic growth */ id int primary key not null auto_increment, /* Product name*/ name varchar(20), /* Product price*/ price decimal(8,2), /* Product picture*/ pic varchar(200), /* Product brief introduction*/ remark longtext, /* Product details*/ xremark longtext, /* Product production date*/ date timestamp default CURRENT_TIMESTAMP, /* Whether it is a recommended product, it may be displayed on the home page of the mall */ comment bool, /* Whether it is a valid product, it may be displayed on the home page of the mall */ open bool, /* The category number of the product is located */ cid int, constraint cid_FK foreign key(cid) references category(id) );
I won't go into details about using reverse engineering mapping to Model class. I mentioned earlier how to use reverse engineering to generate Model.
2. Complete the service layer and action architecture of the product category
2.1 Service layer architecture for product categories
Like the previous category, product also needs to have a service to operate product-related business logic, so we have to write a framework of ProductService and ProductServiceImpl, which is as follows:
//ProductService interface inherits BaseService<Product> public interface ProductService extends BaseService<Product> { } //ProductServiceImpl implements ProductServiceImpl<Product> class inherits BaseServiceImpl<Product> and implements the above ProductService interface @Service("productService") public class ProductServiceImpl extends BaseServiceImpl<Product> implements ProductService { } 2.2 Action structure of commodity categories
First, we need to improve the annotations on the Service layer in BaseAction
@Controller("baseAction") @Scope("prototype") public class BaseAction<T> extends ActionSupport implements RequestAware,SessionAware,ApplicationAware,ModelDriven<T> { @Resource protected ProductService productService; //Other code is omitted, or the original code... } Then we write a ProductAction to inherit the method:
public class ProductAction extends BaseAction<Product> { }At this point, the backend architecture of the product has been basically built, and the next step is to improve the specific functions and business logic.
3. Complete the basic structure of the front desk
The basic structure of the front desk is the same as that of the commodity category. Let’s take a look at what documents are available for the finished commodity category:
We first copy a copy into the product folder based on the front desk files of its product category, and then we make corresponding changes. Let’s analyze the process first: First, index.jsp to aindex.jsp display the menu bar on the left. When clicking on category management, enter the category/query.jsp page to display all product category information on the right. The search and deletion functions are on this page. There is no need to pop up a new window, add a pop-up save.jsp window, and update.jsp window. When clicking on Product Management, enter the product/query.jsp page to display all product information on the right side. The search and deletion functions are completed on this page. Adding and updating save.jsp and update.jsp respectively pop up. Next, we will build the framework of each page and then fill in the corresponding part.
First add the following code in aindex.jsp:
Next, we complete the query.jsp framework:
<%@ 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({ // Change the url address to queryJoinCategory method in the request productAction url:'product_queryJoinCategory.action', loadMsg:'Loading......', queryParams:{name:''},// Here the parameter is changed to name, the parameter value is empty, indicating that we want to display all products. The background is query based on the product name attribute //width:300, fitColumns:true, striped:true, nowrap:true, singleSelect:false, pagination:true, pageSize:5, pageList:[5,10,15,20], idField:'id',//Specify id as the identification field, which is useful when deleting and updating. If you configure this field, page change will not affect the selected items when turning the page //toolbar definition Add, delete, update buttons and search box toolbar: [{ iconCls: 'icon-add', text:'Add product', handler: function(){ //Add trigger code} },'-',{ iconCls: 'icon-edit', text:'Update product', handler: function(){ //Add trigger code} },'-',{ iconCls: 'icon-remove', text:'Delete product', handler: function(){ //Add trigger code} },'-',{ //The query button is not a LinkButton, it has syntax, but it also supports parsing HTML tag text:"<input id='ss' name='ss' name='ssrach' />" }], 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:'Product number',width:100} ]], columns:[[ {field:'name',title:'Product name',width:100}, {field:'price',title:'Product price',width:100}, {field:'remark',title:'simple description',width:100}, {field:'xremark',title:'Detailed description',width:100}, {field:'date',title:'Release time',width:100}, {field:'commend',title:'Recommended product',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:'valid product',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:'Product Category',width:200, //category.type is the product category formatter: function(value,row,index){ if(row.category != null && row.category.type != null) { return row.category.type; //If the product category is not empty, return the product category} else { return "This product is not classified for the time being"; } } } ]] }); //Convert the ordinary text box into a query search text box $('#ss').searchbox({ //Trigger query event searcher:function(value,name){ //value represents the input value//Add trigger code}, prompt:'Please enter the search keyword' }); }); </script> </head> <body> <table id="dg"></table> </body> </html> Next, we complete the queryJoinCategory method in productAction. Before this, we must complete the service part. We will develop it slowly from the bottom layer:
//ProductService interface public interface ProductService extends BaseService<Product> { //Query product information, cascade categories public List<Product> queryJoinCategory(String type, int page, int size); //Query using the product name//Query the total number of records based on keywords 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) //Show .setMaxResults(size) //Show several .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(); //Return a record: total number of records} } The queryJoinCategory method in productAction can be completed:
@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); // Used to store paged data pageMap = new HashMap<String, Object>(); //Query the corresponding data according to keywords and paging parameters List<Product> productList = productService.queryJoinCategory(model.getName(), page, rows); pageMap.put("rows", productList); //Storage in JSON format//Query the total number of records based on keywords Long total = productService.getCount(model.getName()); //Storage in JSON format return "jsonMap"; } } Next, configure it in struts.xml, and the same process as the previous product category. From here, you can see that after developing one, the following one will be soon:
<action name="product_*" method="{1}"> <result name="jsonMap" type="json"> <param name="root">pageMap</param> <param name="excludeProperties"> <!-- rows[0].category.account --> <!-- Filter out all accounts, otherwise lazy loading problems will occur. The following screenshot of this part--> </param> </result> </action>In this way, the background program is written, then open tomcat and test it. When we click on product management in the menu bar on the left, the following window on the right will pop up:
In this way, we have completed the framework of the product management window.
Original address: http://blog.csdn.net/eson_15/article/details/51354932
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.