"SSH online Mall project Combat 08" Query and delete the product category function implementation

Source: Internet
Author: User

In the previous section we finished using the DataGrid to display all the product information, and this section we started adding several features: Add, update, delete, and query. First we implement the foreground display, and then do the background to get the data.

1. Foreground implementation of ADD, update, delete, and query features

There is a toolbar property in the DataGrid control, which is to add toolbars, and we can add these buttons to the Toolbar property to implement the corresponding functions. First look at the Official document definition of toolbar:

We define the toolbar using an array, and add the following code to the query.jsp page:

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >

So we set up the Add, update, delete and query the foreground frame, now can be displayed in the foreground, the background no data come over, just pop up a prompt box, but the display function has been completed, look at the effect:


Next we will do the corresponding function one by one.

2. Implementation of the DataGrid category query

The implementation of the query is the simplest, enter the keyword in the search box, and then pass the keyword as a parameter to the action, and then the service from the database to take out the data, packaged into a JSON format to the foreground to display it, the process is the same as the previous display of all the product information is the same, We just need to add the code of the search section to the JSP above, and the rest does not have to be changed, the following code is added:

The Load method can load all rows showing the first page, it has a parameter, if specified, will go to take the queryparams, otherwise the default is passed the above queryparams specified parameters, we here to set the type of value, that is, the user input query keyword, It is then uploaded to the action, and the background is found in the database based on the value entered by the user and returned to the foreground. The results of the implementation are as follows:

So I completed the function of the search, relatively simple.

3. Implementation of the DataGrid category deletion

Now we have to implement the deletion function, from the above JSP can be seen, before the deletion to determine whether the user has selected a record, if not the user a hint, if there is a check, the pop-up window let the user confirm, if true, then perform the delete function. One detail to note is that if you want to delete more than one record at a time, the Singleselect property above is set to false.

First, we have added the code for the deletion of the above query.jsp, see below:

{iconcls: ' Icon-remove ', text: ' Delete category ', Handler:function () {///To determine if there is a row record selected, use Getselections to get all rows selected var rows = $ ("#dg"). DataGrid ("Getselections");//Returns the selected row, if no row is selected, returns an empty array if (Rows.length = = 0) {//Popup message $.messager.show ({// Syntax is similar to a static method in Java, the direct object calls the title: ' Error hint ', msg: ' Select at least one record ', Timeout:2000,showtype: ' Slide ',});} else {//Prompt for confirmation of deletion, if confirmation performs the deletion of the logical $.messager.confirm (' Delete Confirmation dialog ', ' Are you sure you want to delete this item?    ', function (r) {if (R) {//1.) gets the corresponding ID from the obtained record, stitching the value of the ID, and then sends the background 1,2,3,4 var ids = "";    for (var i = 0; i < rows.length; i + +) {ids + = Rows[i].id + ",";    } ids = Ids.substr (0, Ids.lastindexof (",")); 2.                                                Send Ajax request $.post ("Category_deletebyids.action", {ids:ids},function (result) {if (result = = "true") { Delete the record you just selected, otherwise it will affect the actions of the later update              &nbsp ;                               $ ("#dg"). DataGrid ("UnCheckall "); Refresh the current page, query when we use load, refresh the first page, reload is to refresh the current page $ ("#dg"). DataGrid ("Reload");//The default is the above Queryparams} else {$.messager    . Show ({title: ' Delete Exception ', msg: ' Delete failed, please check operation ', Timeout:2000,showtype: ' Slide ',}); }}, "text");}});}}
If the user chooses to delete, the first one will pop up a dialog box, when the user is determined to delete, we first want to get the user tick the product ID, the ID is stitched into a string, and then send the AJAX request in the background, the first parameter in $.post is sent to that action, The second parameter is the parameter that is sent, the third parameter is the callback function, that is, the method that executes the function after the deletion succeeds, the function parameter result is passed from the background, the fourth parameter is optional, is the type of the return data. We focus on the content in $.post, when the background returns a "true" means the deletion succeeds, then we call the DataGrid inside the Reload method to refresh the page, reload and the previous query with the load is the same, The difference is that reload stays on the current page after the refresh, and load displays the first page.

OK, the front page section is written, and then complete the corresponding method in the background, first add the Deletebyids method in Categoryservice, and implement the method in its implementation class Categoryservceimpl:

//categoryservice interface Pu Blic interface Categoryservice extends baseservice<category> {//query category information, Cascade Admin public list<category> Queryjoinaccount (String type, int page, int size); Use category name query//query total records based on keyword public Long getcount (string type);//delete multiple records according to IDs public void Deletebyids (string IDs);} Categoryserviceimpl Implementation Class @suppresswarnings ("Unchecked") @Service ("Categoryservice") public class Categoryserviceimpl extends baseserviceimpl<category> implements Categoryservice {    // Other methods omitted to write ... You can refer to the previous section content      @Override     public void Deletebyids (String IDs) {    & nbsp;   string hql = "Delete from Category C where c.id in (" + IDs + ")";         GetSession (). CreateQuery (HQL). Executeupdate ();    }} 
Write the service section and then start writing the action section. Because we want to get the IDs data from the foreground, we have to have a variable that implements the Get and set methods in the action to receive this data, and we want to pass the result to the foreground, in the previous section we do the Cascade query, The method used is that struts packages the result data of the query into a JSON format to the foreground, so a map is required and the map is converted to JSON format through the configuration in the configuration file. Here we upload to the foreground of the data is relatively simple, a stake in the deletion of success we pass a "true" can, so do not package into the JSON format, we pass the flow of the method to transmit, the truth and the same as before, Prime Minister we have to have a stream of objects to save this "true" byte, and then through the configuration, Pass this object to the foreground. These two objects are still written in Baseaction, as follows:

@Controller ("Baseaction") @Scope ("prototype") public class Baseaction<t> extends Actionsupport implements requestaware,sessionaware,applicationaware,modeldriven<t> {//Get the IDs to be deleted, have get and set methods//flow is used to return data to the foreground, This data is for struts to get, and then through the flow of the form to the foreground, so implement get method can protected String ids;protected inputstream InputStream;                Omitted below ...}
The methods in the corresponding categoryaction are as follows:

@Controller ("Categoryaction") @Scope ("prototype") public class Categoryaction extends Baseaction<category> { Public String Queryjoinaccount () {                ///slightly ...} Public String Deletebyids () {System.out.println (IDs); Categoryservice.deletebyids (IDs);//If the deletion succeeds it will go down and we will "true" Stream to foreground inputstream = new Bytearrayinputstream ("true". GetBytes ()); Save "true" bytes to stream inputstream return "stream";}}
Next look at the corresponding configuration in Struts.xml:

<struts><constant name= "Struts.devmode" value= "true"/><package name= "shop" extends= "Json-default" ><!--Jason-default inherited Struts-default--><global-results><result name= "Aindex" >/WEB-INF/main/ aindex.jsp</result></global-results><!--class corresponds to the ID value of the action configured in spring because it is given to spring management-->< Action name= "category_*" class= "categoryaction" method= "{1}" ><result name= "Jsonmap" type= "JSON" ><!--slightly-- ></result><result name= "stream" type= "stream" > <!--in the form of stream, type is stream--><param name= " InputName ">inputStream</param> <!--Imputstream data to be transmitted--</result></action><action Name= "account_*" class= "accountaction" method= "{1}" ><result name= "index" >/index.jsp</result></ action><!--the action to complete the system request forwarding, all requests are given to Execute--><action name= "send_*_*" class= "SendAction" >< Result name= "Send" >/web-inf/{1}/{2}.jsp</result></action></package></sTruts> 
So we're done with the delete operation, look at the effect:


Test success, we can also select a number of items to delete, so that the deletion function is done.


(Note: In the end I will 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 Combat 08" Query and delete the product category function implementation

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.