java-different user login can see different menus (background can be implemented to the user menu management) 1 (55)

Source: Internet
Author: User

You can see different menus after you have implemented different user logins. (The User menu can be managed in the background.) First step: Analyze data structures

1 : User Table

Table Name: Users

Column Name

Type

Description

Id

Varchar (32)

Primary key

Name

Varchar (30)

2: Menu Table

Step two: Write a few rows of data in the table for basic analysis

Insert into users values (' U001 ', ' Jack ', ' 1234 ');

Insert into users values (' U002 ', ' Zhang San ', ' 4321 ');

Insert into users values (' U003 ', ' Tom ', ' 1111 ');

Insert into roles values (' R001 ', ' admin ', ');

Insert into roles values (' R002 ', ' teacher ', ');

INSERT into Roleuser values (' U001 ', ' R001 ');

INSERT into Roleuser values (' U002 ', ' R002 ');

Insert into menus values (' M001 ', ' System Management ', '/sys.jsp ');

Insert into menus values (' M002 ', ' User Management ', '/user.jsp ');

Insert into menus values (' M003 ', ' role management ', '/role.jsp ');

INSERT into Rolemenu values (' M001 ', ' R001 ');

INSERT into Rolemenu values (' M002 ', ' R001 ');

INSERT into Rolemenu values (' M003 ', ' R001 ');

INSERT into Rolemenu values (' M003 ', ' R002 ');

/* Query for someone who has a role */

Select U.name,r.name

From the users u inner join Roleuser ru on U.id=ru.uid

Inner JOIN roles R on Ru.rid=r.id;

/* A role has a menu */

Select R.name,m.name

From roles R inner joins Rolemenu RM on R.id=rm.rid

Inner join menus m on rm.mid=m.id;

/* Query Someone to have a menu */

Select U.name,m.name

From the users u inner join Roleuser ru on U.id=ru.uid

Inner JOIN roles R on Ru.rid=r.id

INNER JOIN Rolemenu RM on R.id=rm.rid

Inner join menus m on rm.mid=m.id;

Step Three: Create JavaBean (Domain model) and tool classes

Public class User {

Private String name;

Private String ID;

Private String pwd;

Tool classes to connect to the database:

Step three: Design the interface

Fourth step: Implement User Login

Subcontracting: Provide user login, exit Modify

Cn.hx.user.

Userservlet

Service

UserService

Dao

Userdao

Menu

Cn.hx.menu

Menuservlet

Menuservice

Menudao

Role

For the main page, only Servvlet has no service and calls other service implementations.

Cn.hx.main

Mainservlet

(No service)

Fifth step: Develop login page

<form name="name" action="<c:url value='/userservlet?cmd=login '/> " method="POST">

Name:<input type="text" name="name"/><br/>

pwd: <input type="text" name="pwd"/><br/>

<input type="Submit"/>

</form>

Sixth step: Develop the entire USRE package

Cn.hx.user.

Userservlet

Service

UserService

Dao

Userdao

The seventh step: Real Menu Query function

A menu is a menu of feature packs.

main function, with its own servvlet- mainservvlet

Eighth step: Using the filter to verify the/jsp/*

This is only the first step, just to verify that the user has no access credentials. Certification .

Filter Validation class:

Package cn.hx.filter;

import java.io.IOException;

import Javax.servlet.Filter;

import Javax.servlet.FilterChain;

import javax.servlet.FilterConfig;

import javax.servlet.ServletException;

import javax.servlet.ServletRequest;

import javax.servlet.ServletResponse;

import javax.servlet.http.HttpServletRequest;

import Org.apache.commons.dbutils.QueryRunner;

import Org.apache.commons.dbutils.handlers.ScalarHandler;

import Cn.hx.domain.User;

import cn.hx.utils.DataSourceUtils;

Public class AuthFilter implements Filter {

Public void init (filterconfig filterconfig) throws servletexception {

TODO auto-generated Method stub

}

Public void DoFilter (servletrequest request, servletresponse response,

Filterchain chain) throws IOException, servletexception {

Get URI

HttpServletRequest req = (httpservletrequest) request;

String uri = Req.getrequesturi ();//Http://localhost: 8080/day20/jsps/role.jsp->day20 /JSPs/role.jsp

URI = Uri.replace (Req.getcontextpath (), "");

Compose SQL

String sql = "Select COUNT (1)" +

"From menus m INNER joins Rolemenu RM on M.id=rm.mid" +

"INNER JOIN roles R on R.id=rm.rid" +

"INNER JOIN roleuser ru on R.id=ru.rid" +

"WHERE ru.uid=?" and url=? ";

Take the user's ID

User user = (user) req.getsession (). getattribute ("user");

Try {

Queryrunner run = new queryrunner (datasourceutils. Getdatassource());

Object o = run.query (sql,new Scalarhandler (), User.getid (), URI);

int size = Integer. parseint (O.tostring ());

if (size==0) {

System. err. println ("You have no permissions ....");

}Else{

Chain.dofilter (req, response);

}

}catch(Exception e) {

}

}

Public void Destroy () {

TODO auto-generated Method stub

}

}

Flow chart

Certification process

Login process:

The process of displaying the menu:

java-different user login can see different menus (background can be implemented to the user menu management) 1 (55)

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.