Last page code optimization (http://blog.csdn.net/u013082989/article/details/51025255), but the disadvantage is to write the HQL statement in the action layer, if the HQL statement is more complex, For example, conditional query, there will be some parameters of the judgment, is very error-prone. The following further optimizations for paging queries will become very simple.
Below we will complete a conditional query, for example, you can select departments, by name, according to the conditions of the account query, and paging.
A: or first look at the simple, JSP interface code further extraction.
(1) The Code for public page numbering has been extracted before, but each interface still has to write a piece of JavaScript code to specify the corresponding xxx.action
You can actually define a form directly,
Then get the form, submit the form, so you can write a form tag, specify the action
We want to complete the conditional query, put the content in the form tag, submit the form to the action layer to get the corresponding property value
Second: The following is the solution in the action layer write HQL statement problem, you can define a Queryhelper class, in the future through this class to stitch HQL statements and pass the parameter list, After processing it as a parameter passed to Pagebean processing (about Pagebean please see a blog)
(1) The need to splice from (must have the statement), where, the order BY statement, define the three properties, and need to get the corresponding parameter list
(2) Define a constructor that passes the arguments of the from statement (that is, the class name) when the action layer is new to an object
(3) Splicing where statement, the action layer needs to pass the condition and the parameter list, notice here the parameter is the variable parameter, because is indeterminate, the concrete look below comment.
When the attribute was just defined, the Whereclause and Orderbyclause are initialized to an empty string, so the first call to this method stitching is the from xxx where xxx.yyy=?, the next call is where xxx.yyy=? And xxx.zzz=?
And here is the type of the method is the Queryhelper type, return this, the object that returns the class, so that you can then call your own method to increase the condition, without a statement of a statement of the separate write (see below the action layer of the call)
(3) The main role of the following method is to determine whether to join the where statement, the reason for this is because the interface condition is multiple, the user may not all choose, then it needs to be judged in action, if (XXX) call stitching; so you can just call it all the way, You don't have to judge the IF in action
(4) Same as the same as the order, this is not listed here, this condition query does not need to sort.
How to get the HQL statement:
(5) The statement of the HQL statement and the argument list for the total number of records to be queried:
Three:
(1) In action, declares the property passed by the interface, implements the get, set method
(2) construct a Queryhelp object, such as: (This is where you can pass ".") You can call your own method),
To get the Pagebean object, pass the Queryhelp object as a parameter in the past.
(3) This can be further optimized, as each action requires the service to call the Getpagebean () method and then puts the resulting Pagebean object on top of the stack. So it can be extracted into the Queryhepler class, directly write a method to complete this function.
Such as:
So here you can get the paging information and put it on top of the stack. Complete.
Four: summary
At this point the page-related code optimization is almost the same, and then the paging operation will be very easy and not easy to error.
Java EE paging function final code optimization (ii)