Teammates: Explanation of the way of form handling

Created on 29 Apr 2017  路  12Comments  路  Source: TEAMMATES/teammates

In the "instructorCourseEnroll.jsp" file, there is a form and the action button directs the users here: action="${data.instructorCourseEnrollSaveLink}.
1

I was wondering where this "method" (instructorCourseEnrollSaveLink) is located because I want to check how the method's data is handled. I did try to search it in GitHub but I could not find any relevant detail.
2

Most helpful comment

@VamsiSangam your answer is very detailed and it helped me a lot! thank you very much for that. Thank you also @AnaghHegde for your help. If I have another question on that topic, I will let you know.

All 12 comments

Hi @nikiforosbotis ! The method you are looking for is the execute() method in teammates.ui.controller.InstructorCourseEnrollAction.java.

You can learn a lot about the architecture and design of the TEAMMATES application in design.md. Although even I'm not yet fully aware of the complete architecture of the application, it is mentioned in the docs that -

[logic] - [ui::view] - [ui::controller] represent an application of Model-View-Controller pattern.

In MVC pattern, the requests are handled by the controller. So if you want to search for a method which is handling a request, then you can blindly start looking for it in the teammates.ui.controller package. :smiley:

Hi @nikiforosbotis

I was wondering where this "method" (instructorCourseEnrollSaveLink) is located because I want to check how the method's data is handled.

I am no perfect even i had a similar problem but like @VamsiSangam said then those methods will make use of the backend java files. For more information take a look at the design.md as @VamsiSangam said. But for your question may be design.md-user-inoked-requests will help you esp those 6 points after the diagram.

@VamsiSangam at first, thank you for your answer! I understand your point but I would like to ask you:
1)Do you mean the teammates.ui.controller.InstructorCourseEnrollPageAction.java file? because I cannot find the exact filename that you sent me.
2)So there is no such method with the exact name: "instructorCourseEnrollSaveLink" right? I mean I cannot understand how the action: ${data.instructorCourseEnrollSaveLink} is "transformed" to the execute() method, as you are saying.

Thank you!

@nikiforosbotis

Do you mean the teammates.ui.controller.InstructorCourseEnrollPageAction.java file?

This file is there you should check again. It's there in my directory.

@AnaghHegde Thanks for your help.

The only two files with a similar name (but not the same) are these, as shown in the TEAMMATES directory:
I guess I can find the functionality I want by checking out these files.

1

@nikiforosbotis Did you get answer to your question ?. I mean is everything clear now ?.

1)Do you mean the teammates.ui.controller.InstructorCourseEnrollPageAction.java file? because I cannot find the exact filename that you sent me.

Very sorry, my bad! I meant teammates.ui.controller.InstructorCourseEnrollSaveAction.java.

I mean I cannot understand how the action: ${data.instructorCourseEnrollSaveLink} is "transformed" to the execute() method, as you are saying.

I am going to give a little detailed explanation, please bear with me. So when you send a request to /page/instructorCourseEnrollPage, according to the web.xml code -

<servlet>
        <description>Servlet that handles all incoming requests</description>
        <servlet-name>ControllerServlet</servlet-name>
        <servlet-class>teammates.ui.controller.ControllerServlet</servlet-class>
</servlet>

The ControllerServlet class handles every request. There, the doPost() method is called, where we have -

ActionResult actionResult = c.executeAndPostProcess();
actionResult.send(req, resp);

Here, the executeAndPostProcess() causes the execute() method in InstructorCourseEnrollPageAction to run. If you have noticed in the execute() method, you create an object named pageData which is of the type InstructorCourseEnrollPageData. InstructorCourseEnrollPageData has a method getInstructorCourseEnrollSaveLink(), it is this method which constructs the end output. Eg. -> /page/instructorCourseEnrollSave?courseid=teammates.instructor.uni-demo&user=teammates.instructor%40university.edu

The execute() method returns a ShowPageResult object -

return createShowPageResult(Const.ViewURIs.INSTRUCTOR_COURSE_ENROLL, pageData);

So if you go back to the code above -

ActionResult actionResult = c.executeAndPostProcess(); // actionResult is a ShowPageResult object
actionResult.send(req, resp); // this calls send() method in ShowPageResult class

So in ShowPageResult's send() method, you have a super magical line -

req.setAttribute("data", data);

Where the data object here is of type InstructorCourseEnrollPageData. That line is the reason you are able to write ${data} in your .jsp file. When you use setAttribute in your java code, you access it via ${} in JSP. Now, data is of the type InstructorCourseEnrollPageData, that is why you are able to write ${data.instructorCourseEnrollSaveLink} which actually calls that method I mentioned earlier, giving you the required URL with required parameters.


In a nutshell - "data" in ${data.instructorCourseEnrollSaveLink} is an object of type InstructorCourseEnrollPageData which has a property instructorCourseEnrollSaveLink. Writing ${data.instructorCourseEnrollSaveLink} invokes InstructorCourseEnrollPageData.getInstructorCourseEnrollSaveLink()

Hope this clears your question :smile:

@VamsiSangam That is well explained. I hope it clarifies @nikiforosbotis doubt. On a note @nikiforosbotis feel free to ask if you have still have any doubts. We are happy to help.

@VamsiSangam your answer is very detailed and it helped me a lot! thank you very much for that. Thank you also @AnaghHegde for your help. If I have another question on that topic, I will let you know.

Guys @VamsiSangam , @AnaghHegde I did check your very detailed answers and I understood many things considering how the TEAMMATES is working.

One more question please:

On the getInstructorCourseEnrollSaveLink(String courseId) method of the PageData.java file, there is a link for the INSTRUCTOR_COURSE_ENROLL_SAVE page that is translated to: "/page/instructorCourseEnrollSave"; according to the Const.java file.

This is confirmed by the page that is shown after the form is submitted:(/page/instructorCourseEnrollSave?courseid=CS123&user=test%40example.com):

instructorcourseenrollsave

However, I searched in GitHub for the page: page/instructorCourseEnrollSave and I did not find anything similar. What I did find was this jsp page: /jsp/instructorCourseEnrollResult.jsp which seems to be the same as the required. Is there a way which "transforms" the first page to the .jsp one? Otherwise, where is the page/instructorCourseEnrollSave located?

Do you get what I mean?

Thank you in advance.

Hi @nikiforosbotis ! So as things start with ControllerServlet.java's doPost() method, there we have -

Action c = new ActionFactory().getAction(req);

So, the purpose of the getAction() method is to return the proper Action subclass associated with the request. So this is the getAction() which gets called -

public Action getAction(HttpServletRequest req) {
     String url = req.getRequestURL().toString();
     log.info("URL received : [" + req.getMethod() + "] " + url);

     String uri = req.getRequestURI();
     if (uri.contains(";")) {
         uri = uri.split(";")[0];
     }
     Action c = getAction(uri);
     c.init(req);
     return c;

 }

Here, in the line Action c = getAction(uri);, we have passed the URL to get the proper Action subclass. Inside the getAction(uri) method there's a line -

Class<? extends Action> controllerClass = actionMappings.get(uri);

It is using a variable actionMappings to get the proper Action subclass associated with the requested URL. Now if you look at the top of ActionFactory.java file, you will find the declaration of actionMappings which is a HashMap<String, Class<? extends Action>> and just below it is a huuuuuugely important code in the static block which initializes the actionMappings variable. So there you have a line -

map(INSTRUCTOR_COURSE_ENROLL_SAVE, InstructorCourseEnrollSaveAction.class);

This line associates /page/instructorCourseEnrollSave to InstructorCourseEnrollSaveAction.java. So that's the first half of your answer.

So due to the association above InstructorCourseEnrollSaveAction's execute() method gets called (similar to my previous answer as to how InstructorCourseEnrollPageAction's execute() gets called).

In InstructorCourseEnrollSaveAction's execute() method you have the line -

return createShowPageResult(Const.ViewURIs.INSTRUCTOR_COURSE_ENROLL_RESULT, pageData);

that line associates /jsp/instructorCourseEnrollResult.jsp with the page data (data which is displayed) and that's the view which is rendered.

So, basically /page/instructorCourseEnrollSave is associated with InstructorCourseEnrollSaveAction.java which returns the view /jsp/instructorCourseEnrollResult.jsp.

Think about it for a while. I'm sure you'll get it! Let me know if this answers your question. :smile:

@VamsiSangam Thank you very much for your detailed response. It really helped me a lot.

Was this page helpful?
0 / 5 - 0 ratings