Forum module management for OA Projects

Source: Internet
Author: User

1. Make notes for the Forum

2. The effect is as follows:





3. We started from the simplest way, that is, module management. It is similar to role management, but it only has the functions of move up and move down.

4. Analyze the number of requests first. Crud has previously written a total of 6 requests. Therefore, we need 6 methods and add up and down. Therefore, forumaction should have 8 methods.

5. The design entity forum does not consider the association with other tables. Its own attributes are as follows: (the UML diagram designed for the entire Forum is provided here)



Forum (the move up and move down functions are designed in this way. To create an int for position switching, move up and move down is to swap a row of records in the database table to the next location, however, you must consider how to assign the position value? Next)

package com.icss.oa.domain;public class Forum {private Long id;private String name;private String description;private int position;public Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getDescription() {return description;}public void setDescription(String description) {this.description = description;}public int getPosition() {return position;}public void setPosition(int position) {this.position = position;}}
6. Write the corresponding HBM File Based on the object class. Remember to add the ing in the hibernate file.

<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">


7. Design Methods in Action (remember to add forumservice to baseaction and add annotations to action)

Package COM. ICSS. oa. view. action; import Java. util. list; import Org. springframework. context. annotation. scope; import Org. springframework. stereotype. controller; import COM. ICSS. oa. base. baseaction; import COM. ICSS. oa. domain. forum; import COM. ICSS. oa. domain. role; import COM. opensymphony. xwork2.actioncontext; @ suppresswarnings ("serial") @ controller @ scope ("prototype") public class forummanageaction extends baseaction <Forum> {// list method Public String list () throws exception {list <Forum> forumlist = forumservice. findall (); actioncontext. getcontext (). put ("forumlist", forumlist); Return "list";} // Delete the Public String Delete () throws exception {forumservice. delete (model. GETID (); Return "tolist";} // Add the Public String addui () throws exception {return "saveui";} // Add the Public String add () method () throws exception {forumservice. save (model); Return "tolist";} // modify the PAGE method Public String editui () throws exception {// echo data forum Forum = forumservice. getbyid (model. GETID (); actioncontext. getcontext (). getvaluestack (). push (ForUM); Return "saveui";} // modify the public string edit () throws exception {// obtain the original object forum Forum = forumservice from the database. getbyid (model. GETID (); // modify the Forum attribute. setname (model. getname (); forum. setdescription (model. getdescription (); // save forumservice. save (ForUM); Return "tolist";} // The Public String moveup () throws exception {forumservice. moveup (model. GETID (); Return "tolist";} // move the Public String movedown () throws exception {forumservice. movedown (model. GETID (); Return "tolist ";}}
8. almost all methods are added with complete functions. Remember to add the JSP echo result in struts2.xml. The move down and move up functions are your own special methods, the implementation code in the forumserviceimpl file is as follows:

Note: ① The findall () and save () methods need to be rewritten here, because the query needs to be sorted by the position value.

② How to assign a value to the position, and use the database to generate the primary key policy. Each time a record is generated, the Forum id value is assigned to the position, because the IDs are not repeated, in addition, each auto-increment operation ensures that the position value is the largest.

③ Pay attention to the optimization of SQL statements. For example, the following two results are extracted from the previous record of the current position, which has a large difference in efficiency and should be used as few subqueries as possible.


Int position
Requirements:
Display in ascending order of position.
The value of position cannot be repeated. When adding a position, you must specify a position number. If the current maximum value is greater than 1.
Moving up or down refers to exchanging the position value with the Forum above or below.

Package COM. ICSS. oa. service. impl; import Java. util. list; import Org. springframework. stereotype. service; import COM. ICSS. oa. base. basedaoimpl; import COM. ICSS. oa. domain. forum; import COM. ICSS. oa. service. forumservice; @ service @ suppresswarnings ("unchecked") public class forumserviceimpl extends basedaoimpl <Forum> implements forumservice {@ overridepublic list <Forum> findall () {return getsession (). createquery (// "From Forum f order by F. position ASC ")//. list () ;}@ overridepublic void save (forum Forum) {// save it to the DB and generate the id value getsession (). save (ForUM); // specify the position value as the maximum // select max (F. position) from Forum Fforum. setposition (Forum. GETID (). intvalue (); // you do not need to call the update () method because it is a persistent state .} Public void moveup (long ID) {// get the two forumforum Forum = getbyid (ID) to be exchanged; // forumforum Other = (ForUM) getsession () for the current operation (). createquery (// my Forum "from Forum f Where F. position <? Order by F. position DESC ")//. setparameter (0, Forum. getposition ())//. setfirstresult (0 )//. setmaxresults (1 )//. uniqueresult (); // The Top cannot be moved up. If (Other = NULL) {return;} // You can exchange the position value int temp = forum. getposition (); forum. setposition (Other. getposition (); Other. setposition (temp); // update to database // The update () method does not need to be called because it is a persistent state .} Public void movedown (long ID) {// get the two forumforum Forum = getbyid (ID) to be exchanged; // forumforum Other = (ForUM) getsession () for the current operation (). createquery (// The Forum "from Forum f Where F. position>? Order by F. position ASC ")//. setparameter (0, Forum. getposition ())//. setfirstresult (0 )//. setmaxresults (1 )//. uniqueresult (); // The bottom cannot be moved down if (Other = NULL) {return;} // The value of the switch position int temp = Forum. getposition (); forum. setposition (Other. getposition (); Other. setposition (temp); // update to database // The update () method does not need to be called because it is a persistent state .}}

9. Now all the management functions of the Forum module are implemented.

10. Remember to write the JSP page, which is about modifying the tag and iterative value on the static page. The Code is as follows:

List. jsp

<% @ Page Language = "Java" Import = "Java. util. * "pageencoding =" UTF-8 "%> <% @ include file ="/WEB-INF/JSP/public/Common. jspf "%> <HTML> 

Saveui. jsp

<% @ Page Language = "Java" Import = "Java. util. * "pageencoding =" UTF-8 "%> <% @ include file ="/WEB-INF/JSP/public/Common. jspf "%> <HTML> 






Forum module management for OA Projects

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.