Task 6(b): Implementing Client and Admin Modules - Controller Logic and Views with Server-Side View Rendering
By the end of this lab activity, you will:
- Develop and complete the
ClientControllerandAdminControllerclasses to handle data fetching, input validation, data storage, and preparation of ViewModel data for rendering in JSP views. - Use static lists to simulate a database, storing form data for both Client and Admin modules.
- Create ViewModel classes to structure data for JSP views.
- Design and complete JSP pages for the Client and Admin modules, integrating dynamic data from the ViewModel.
In this lab, you will expand on your work from Week 5(b) by building out core functionality for the Client and Admin modules. You will finalize the controller methods for these two modules, simulate database interactions using static lists, and build corresponding JSP views for server-side HTML rendering.
You need to handle form data submission, validate inputs, store records in memory, and prepare data for dynamic rendering in JSP pages using ViewModel classes.
- Ensure the project structure includes the required directories for controllers, models, services, and views.
- Set up the initial
ClientControllerandAdminControllerclasses if not already done.
Objectives:
- Capture form data for client registration and profile management.
- Validate inputs (e.g., name, email, password strength (optional), and BMI-related fields).
- Save validated data to a static list (e.g.,
List<Client>).
Steps:
- Define methods in
ClientController:showRegisterForm()- Displays the registration page (register.jsp).showLoginForm()- Displays the login page (login.jsp).processRegisterForm()- Handles form submission for client registration.showProfile()- Displays the client profile page (profile.jsp).
Implementation:
- In
showRegisterForm(), forward the user toregister.jsp. - In
showLoginForm(), forward the user tologin.jsp. - In
processRegisterForm(), capture form data, validate inputs, and store client data in a static list if validation passes. If validation fails, display error messages onregister.jsp. - In
processLoginForm(), validate user credentials using the static list of registered clients. If valid, redirect toprofile.jsp; otherwise, display an error onlogin.jsp. - In
showProfile(), retrieve the client’s data from the static list and forward it toprofile.jsp.
JSP Integration:
- Use
model.addAttribute()in each method to pass data to the JSP pages for display.
Objectives:
- Manage client data, roles, and programs using server-rendered JSP views.
- Implement CRUD operations for managing clients and fitness programs.
Steps:
- Define methods in
AdminController:showClientList()- Displays a list of all registered clients (clientList.jsp).showAddProgramForm()- Displays a form to add a new fitness program (addProgram.jsp).processAddProgramForm()- Handles form submission for new programs.showManageRoles()- Displays a page for managing client roles (manageRoles.jsp).
Implementation:
- In
showClientList(), retrieve the list of clients from the static list and forward it toclientList.jsp. - In
showAddProgramForm(), forward directly toaddProgram.jsp. - In
processAddProgramForm(), handle form data to add a new fitness program, validate inputs, and store the program in a static list if validation is successful. - In
showManageRoles(), forward necessary data tomanageRoles.jspfor assigning roles.
JSP Integration:
- Use
model.addAttribute()to bind data for JSP views such as lists of clients and program details.
Objectives:
- Use ViewModel classes to organize data for JSP views.
- Keep business logic separate from presentation.
Steps:
- Create a
ClientViewModelclass to format data forprofile.jspandclientList.jsp. - Create an
AdminViewModelclass to format data foraddProgram.jspandmanageRoles.jsp. - Populate the ViewModel classes with data retrieved from static lists.
Objectives: Develop dynamic JSP pages that render data passed from controllers for Client and Admin modules.
Steps:
For Client:
register.jsp: Form for new clients to sign up with fields for name, email, password, and any other relevant details.login.jsp: Create a login form for clients with fields for email and password. Include input validation and display error messages for incorrect credentials.profile.jsp: Displays client details and includes editable fields for updating profile information.
For Admin:
clientList.jsp: Displays a list of all registered clients, with options to view and manage each client.addProgram.jsp: Form to add new fitness programs, including fields like program name, description, and duration.manageRoles.jsp: Interface to assign roles to clients, displaying a list of users and available roles.
Use JSTL tags (<c:forEach>, <c:if>) to loop through lists and handle conditional rendering in the JSP files.
- Test each JSP view to ensure dynamic content renders correctly from ViewModel classes.
- Test form submissions to verify data is validated and stored correctly.
- Verify role-based access by ensuring that Admin-specific pages (e.g.,
clientList.jspandmanageRoles.jsp) are accessible only to Admin users.
-
Controllers:
- Fully implemented controller classes for the modules, including
ClientController,AdminController. - Each controller should handle data fetching, validation, storage, and forwarding data to JSP views using ViewModel classes.
- Fully implemented controller classes for the modules, including
-
ViewModel Classes:
- Complete ViewModel classes for each module, including
ClientViewModel,AdminViewModel. - Each ViewModel class should prepare data in a structured format for JSP views, simplifying data access and presentation.
- Complete ViewModel classes for each module, including
-
JSP Views:
- Complete JSP pages for all modules, implementing dynamic data rendering and form handling for each module’s specific tasks.
- Client Module:
register.jsp,login.jsp,profile.jsp - Admin Module:
clientList.jsp,addProgram.jsp,manageRoles.jsp
- Client Module:
- Complete JSP pages for all modules, implementing dynamic data rendering and form handling for each module’s specific tasks.
Each JSP view should be set up to display data dynamically from the corresponding ViewModel class and support form submissions, where applicable. This structure provides a comprehensive framework for delivering a fully functional web application with each module implemented according to MVC principles, using Spring Web MVC and JSP for server-side rendering.
- A brief report describing each controller method, data flow, validation rules, and any issues encountered.
- Correct Implementation: All controllers and JSP views function as expected. [Only
ClientControllerandAdminController] - Input Validation: Appropriate validation is applied, with errors displayed on invalid input.
- Data Storage: Static lists correctly simulate a database.
- Code Structure: MVC structure is followed with ViewModels effectively used.
- Documentation: Clear descriptions and explanations are provided.
This lab reinforces your understanding of MVC architecture and gives you practical experience with JSP and server-side rendering in Java Web applications.