OA project permission design ②

Source: Internet
Author: User

1. Then, today we came to the design of how to successfully implement the permission assignment function. First, let's take a look at the steps of these features, such:

First, you can see the permission setting button on the user's list page and click it to go to the permission setting page.


Go to the permission settings page and view the permission data, for example:


Several of these requests are analyzed: click Set permission to enter the permission page, which is similar to the modification in role action, however, it should be noted that the ID is transferred from setting permissions to assigning permissions. after entering the page, we need to echo the permission data and display all the permission data on the assigned permissions page, this is all to be prepared. The following code adds two methods in roleaction:

Package COM. ICSS. oa. view. action; import Java. util. list; import javax. annotation. resource; import Org. springframework. context. annotation. scope; import Org. springframework. stereotype. controller; import COM. ICSS. oa. base. baseaction; import COM. ICSS. oa. domain. privilege; import COM. ICSS. oa. domain. role; import COM. ICSS. oa. service. privilegeservice; import COM. ICSS. oa. service. roleservice; import COM. opensymphony. xwork2.actioncontext; import COM. opensymphony. xwork2.actionsupport; import COM. opensymphony. xwork2.modeldriven; @ suppresswarnings ("serial") @ controller @ scope ("prototype") public class roleaction extends baseaction <role> {private long [] privilegeids; public long [] getprivilegeids () {return privilegeids;} public void setprivilegeids (long [] privilegeids) {This. privilegeids = privilegeids;} // list method Public String list () throws exception {list <role> rolelist = roleservice. findall (); actioncontext. getcontext (). put ("rolelist", rolelist); Return "list" ;}// Delete method Public String Delete () throws exception {roleservice. delete (model. GETID (); Return "tolist" ;}// Add the Public String addui () throws exception {return "addui" ;}// Add the Public String add () method () throws exception {// set the page parameter value // role = new role (); // role. setname (role. getname (); // role. setdescription (role. getdescription (); // save it to the database roleservice. save (model); Return "tolist";} // modify the PAGE method Public String editui () throws exception {// obtain a message about the role object based on the ID and display role role1 = roleservice. getbyid (model. GETID (); // display data on the Edit page // This. name = role. getname (); // This. description = role. getdescription (); actioncontext. getcontext (). getvaluestack (). push (role1); Return "editui";} // modify the public string edit () throws exception {// set the value of role role2 = roleservice. getbyid (model. GETID (); role2.setname (model. getname (); role2.setdescription (model. getdescription (); // update to the roleservice in the database. update (role2); Return "tolist";} // set the permission PAGE method Public String setprivilegeui () throws exception {// prepare the echo data // prepare the displayed data role = roleservice. getbyid (model. GETID (); actioncontext. getcontext (). put ("role", role); List <privilege> privilegelist = privilegeservice. findall (); actioncontext. getcontext (). put ("privilegelist", privilegelist); Return "setprivilegeui";} // set the permission method Public String setprivilege () throws exception {// retrieve the source object role = roleservice from the database. getbyid (model. GETID (); // set the attribute role to be modified. setprivileges (model. getprivileges (); // update to roleservice in the database. update (role); Return "tolist ";}}
The above Code does not display the data. First, all the permission data is displayed. The following is a new setprivilegeui. jsp page:

<% @ Page Language = "Java" Import = "Java. util. * "pageencoding =" UTF-8 "%> <HTML> We need to pay attention to this JSP page. We will use jquery to display the tree structure in the future. Here we only display the data temporarily, using the checkboxlist in the struts2 label, note that the corresponding results are added to the XML file, and the page also dynamically shows who is assigning permissions. When the page is accessed, use the El expression to obtain the role name attribute.


The running effect is as follows: (there are still many features not implemented, such as ECHO, tree structure, relationship between the upper and lower levels, full selection, and so on)



2. During the running process, I found a lazy loading exception. This is a very common file. I checked a lot of information on the Internet and shared it with you.

1. org. Apache. Jasper. jasperexception: javax. El. elexception: Error reading 'name' on typecn. itcast. OA. domain. Department _ $ _ export sist_1

..... Omitted ......

2. javax. El. elexception: Error reading 'name' ontype cn. itcast. OA. domain. Department _ $ _

Export sist_1

..... Omitted ......

3. org. hibernate. lazyinitializationexception: cocould not initialize proxy-No session

..... Omitted ......

 

In the preceding three error messages, the third is the critical error prompt lazyinitializationexception (the lazyinitializationexception is the lazy loading exception by default), which means that when reading data, the session has been closed.

Solution:

1. Set lazy loading to false. By default, Hibernate is lazy loading. Therefore, you need to set it not to lazy loading. Set the following in department. HBM. xml:

[HTML] view plaincopyprint?
  1. <Role-to-one name = "parent" class = "department" column = "parentid" lazy = "false"> </role-to-one>

Set the value of lazy to false. That is to say, after the parent department is loaded, all its sub-departments will be loaded, which leads to another problem: when the parent department has many sub-departments, all sub-departments are loaded, resulting in low performance. So can we load it only when we change it to use, but not when it is not used? (By default, it is still lazy to load, but you need to find the session when using it, and you can find the session to read data from the database)


2. Adopt interceptor

The above indicates a request, which must pass through action and interceptor. The box on the left is action, the box in the middle is result (PAGE), and the box on the right is filter interceptor.

Action indicates the result to be executed. In the action, the service method (service box) is called. Our common practice is to open and close transactions in the service, as well as opensession and close session, this is because we only use the lazy loading attribute in the result (PAGE) (the session has been closed at this time ). To solve this problem, you must postpone the session close step until the result is returned before closing the session. Here we use opensessioninviewfilter in spring (this is a filter.

The Code is as follows:

The configurations in department. HBM. xml remain unchanged.

Add this filter to the Web. xml file:

<! -- Configure opensessioninviewfilter of spring to solve the problem of lazy loading exceptions --> <filter-Name> opensessioninviewfilter </filter-Name> <filter-class> Org. springframework. orm. hibernate3.support. opensessioninviewfilter </filter-class> </filter> <filter-mapping> <filter-Name> opensessioninviewfilter </filter-Name> <URL-pattern> *. action </url-pattern> </filter-mapping>





OA project permission design ②

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.