"SSH online Mall project actual combat 10" commodity class basic module construction

Source: Internet
Author: User

Before we completed the business logic related to the product category, we started to do the specific product section.

1. Database build table and map model

First we create a new table in the database, and then use reverse engineering to map the table to the model class, as shown in the following table:

/*=============================*//* table: Commodity table structure    *//*=============================*/create table product (   /* Product number, autogrow *                  /ID int primary key NOT NULL auto_increment,/   * Product Name *                /Name varchar */   * commodity price *               /Price Decimal (8,2),/   * Product Picture *                 /pic varchar ($),/   * Product Brief */   remark              Longtext,/   * Product Details */   Xremark             longtext,/   * Product Production date */date                timestamp default Current_ TIMESTAMP,/   * Whether it is a recommended product, recommended products can be displayed in the mall home * *   commend             bool,/   * is a valid commodity, effective products can be displayed in the mall home */   Open                bool,   /* Product category Number *   /cid                  int,   constraint CID_FK foreign key (CID) references category (ID));
The use of reverse engineering mapping for the model class will not be mentioned, the previous mention how to use reverse engineering to generate model.

2. Complete the service layer and action architecture for the product class
2.1 Service Layer architecture for commodity classes

As with the previous category, product has to have a service to manipulate the business logic associated with the commodity, so we have to write a productservice and Productserviceimpl architecture, as follows:

Productservice interface Inherits Baseservice<product>public interface Productservice extends baseservice<product> {} Productserviceimpl implements class inheritance Baseserviceimpl<product>, and implements the above Productservice interface @service ("Productservice") public Class Productserviceimpl extends baseserviceimpl<product> implements Productservice {}

2.2 Action architecture for commodity classes

First, we need to refine the annotations on service layer in Baseaction

@Controller ("Baseaction") @Scope ("prototype") public class Baseaction<t> extends Actionsupport implements requestaware,sessionaware,applicationaware,modeldriven<t> {@Resourceprotected Productservice ProductService ;        The other code is omitted, or the original code ...}
Then we write a productaction that inherits the method:

public class Productaction extends baseaction<product> {}
At this point, the background of the product structure is basically set up, the next step is to improve the inside of 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 the product category, so we look at what documents are available at the front desk of the finished product Category:


We first copy a copy to the product folder according to the foreground file of its product category, and then we make the corresponding modification. First to analyze the process: first index.jsp to aindex.jsp display the left menu bar, when you click on the category management, enter the category/ Query.jsp page to the right of all product category information, search and delete features are on this page, do not need to pop up a new window, add pop-up save.jsp window, update pop-up update.jsp window. When you click on the product management, go to the right of the product/query.jsp page to display all product information, search and delete functions are completed on this page, add and update pop-up save.jsp and update.jsp respectively. Next we set up the frames of each page and then fill in the corresponding sections.

First, add the following code to the 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" >

Next we complete the Queryjoincategory method in the Productaction, before this, the first to complete the service section, we are first from the bottom slowly developed:

Productservice interface public interface Productservice extends baseservice<product> {         //Query product information, cascading categories     public list<product> queryjoincategory (String type, int page, int size); Use the product's name query     //to query the total number of records by keyword     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 lef T join fetch p.category where p.name like:name ";        return getsession (). CreateQuery ( HQL)                 .setstring ("name", "%" + name + "%") & nbsp;               .setfirStresult ((page-1) * size)//From the beginning of the first display                 . Setmaxresults (size)//display several                 .list ();     }         @Override     public Long getcount (String Name) {        string hql = "SELECT count (P) from Product p where P.name like:name";  & nbsp      return (Long) getsession (). CreateQuery (HQL)              .setstring ("name", "%" + name + "%")             .uniqueresult (); Returns a record: Total record Count     }}
Below you can complete the Queryjoincategory method in Productaction:

@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);//data used to store paging Pagemap = new hashmap<string, object> ();//Query the corresponding data list by keyword and paging parameters <Product> productlist = Productservice.queryjoincategory (Model.getname (), page, rows);p agemap.put ("Rows", ProductList); stored in JSON format//query by keyword long total = Productservice.getcount (Model.getname ());//system.out.println Pagemap.put ("Total", total); Save as JSON format return "Jsonmap";}}
Next in the Struts.xml configuration, and the same as the previous product class process, to here can be seen, developed one, the following one is fast:

<action name= "product_*" class= "productaction" method= "{1}" ><result name= "Jsonmap" type= "JSON" >< param name= "root" >pagemap</param><param name= "excludeproperties" ><!--rows[0].category.account- -><!--to filter out all the account, otherwise lazy loading problem, the section below--></param></result></action>

So the background program is written, and then open Tomcat, test, when we click on the left side of the menu bar of product management, will pop up to the right of the following window:

So we have finished the framework of the Commodity Management window.


(Note: To the end to provide the entire project source code download!) Welcome to our collection or attention)

Related reading: http://blog.csdn.net/column/details/str2hiberspring.html

___________________________________________________________________________________________________________ __________________________________________

-----willing to share and progress together!

-----More articles please see: http://blog.csdn.net/eson_15

"SSH online Mall project actual combat 10" commodity class basic module construction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.