Register   Login   About   Study   Enterprise   Share
AI / Internet Technology University (AITU)
Fast Login - available after registration







|

Top Links: >> 80. Technology >> Internet Technology Summit Program >> 4. Web Apps Frameworks >> 4.4. Spring with Apache Maven and Data Service Frameworks
Current Topic: 4.4.1.Spring MVC Framework
You have a privilege to create a quiz (QnA) related to this subject and obtain creativity score...
4.4.1.Spring MVC Framework
Excellent time to recollect again: what is MVC!

Model-view-controller (MVC) architecture separates the different aspects of the application:
- The Model represents business logic and data
- The View is responsible for data presentation usually generated as HTML output that the client's browser can interpret
- The Controller takes care of input from a browser (user requests are passed to the Model for processing) and of the processed results (the results of processing with business logic are passed to the View for presentation as HTML)
We have learned that Java Servlet usually plays a role of the Controller.

MvcModel2

A conceptual Dispatcher Controller on the illustration above is implemented in the Spring framework with the DispatcherServlet class.
The Spring Web model-view-controller (MVC) framework is designed around a DispatcherServlet class that handles all the HTTP requests and responses. Spring Web MVC DispatcherServlet class is part of the framework.

The DispatcherServlet class serves as the first target for any request from a browser. According to the standards for Java-based web applications, this mapping is assigned in the web.xml file.
Usually the DispatcherServlet class is assigned in the web.xml file as a common servlet target. Programming specific controllers starts in Spring with the @Controller annotation. The mapping between this controller and the URL pattern is also provided with the annotation, such as @RequestMapping("url").
The Web Application Concept Map (Spring flavor) describes the basic steps of creating a web application with the Spring framework.

WebAppStepsMavenSpring

This is the example of the web.xml file.

xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
ITS Web Apps with Spring MVC

ItsCommonController

org.springframework.web.servlet.DispatcherServlet



ItsCommonController
/




Any request from a browser will be handled initially by the DispatherServlet class of the Spring frameworks. This is defined by mapping any url-pattern with the Project Name (/) to the servlet-name ItsCommonController and mapping this name to the servlet-class org.springframework.web.servlet.DispatcherServlet.

Spring framework expects us to define the View folder (prefix) and the View file extension (suffix). This definition must be done in the XML file with the file name that we specified in the servlet mapping above, ItsCommonCointroller-servlet.xml.

Let us keep the View files in the jsp directory with the extension .jsp.
The same file will also define the package name for specific controllers in the project.
Check the content of the ItsCommonCointroller-servlet.xml file below.

xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">



class="org.springframework.web.servlet.view.InternalResourceViewResolver">

/jsp/


.jsp



Was it clear so far?


Specific URLs will be handled by specific controllers. One example of such a specific controller is provided below.


@Controller
@RequestMapping("/getCatalog")
public class CourseCatalog{
@RequestMapping(method = RequestMethod.GET)
public String listCourses(ModelMap model) {
// business logic to retrieve the list of courses
model.addAttribute("title", "Internet Technology School Course Catalog");
model.addAttribute("listOfCourses", "Java
Databases
Mobile Apps
Big Data");
return "courses"; // this passes control to the View: a JSP page named courses.jsp
}
}

In the code above, we created a new class CourseCatalog, which will be called by the DispatcherServlet when the URL "/getCatalog" will be sent by a browser.

In the model object we collect key ? values that will be used in the View. We collected the title and the list of courses itself.
Note, that we might have several lines of business logic to actually retrieve the list of courses from a data source. Instead, we just hardcoded this object as a long string with line separators.

This is an example of the courses.jsp - the View that exposes the data collected by the specific controller CourseCatalog.




Internet Technology School: Spring MVC


${title}


${listOfCourses}





Assignments:
1. Create 3 QnAs on the subject and email all three together in the file 4.4.1.SpringMVC.QnA.Your.Name.txt (use WordPad, not MS Word) to dean@ituniversity.us

| Check Your Progress | Propose QnA | Have a question or comments for open discussion?
<br/><web-app id="WebApp_ID" version="2.4"
<br/>    xmlns="http://java.sun.com/xml/ns/j2ee" 
<br/>    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<br/>    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
<br/>    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<br/>   <display-name>ITS Web Apps with Spring MVC</display-name>
<br/>   <servlet>
<br/>      <servlet-name>ItsCommonController</servlet-name>
<br/>      <servlet-class>
<br/>         org.springframework.web.servlet.DispatcherServlet
<br/>      </servlet-class>
<br/>   </servlet>
<br/>   <servlet-mapping>
<br/>      <servlet-name>ItsCommonController</servlet-name>
<br/>      <url-pattern>/</url-pattern>
<br/>   </servlet-mapping>
<br/></web-app>
<br/>


Any request from a browser will be handled initially by the DispatherServlet class of the Spring frameworks. This is defined by mapping any url-pattern with the Project Name (/) to the servlet-name ItsCommonController and mapping this name to the servlet-class org.springframework.web.servlet.DispatcherServlet.

Spring framework expects us to define the View folder (prefix) and the View file extension (suffix). This definition must be done in the XML file with the file name that we specified in the servlet mapping above, ItsCommonCointroller-servlet.xml.

Let us keep the View files in the jsp directory with the extension .jsp.
The same file will also define the package name for specific controllers in the project.
Check the content of the ItsCommonCointroller-servlet.xml file below.
<br/><beans xmlns="http://www.springframework.org/schema/beans"
<br/>	xmlns:context="http://www.springframework.org/schema/context"
<br/>	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<br/>	xsi:schemaLocation="
<br/>http://www.springframework.org/schema/beans
<br/>http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
<br/>http://www.springframework.org/schema/context
<br/>http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<br/> 
<br/>	<context:component-scan base-package="com.its.samples.spring" />
<br/> 
<br/>	<bean
<br/>		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<br/>		<property name="prefix">
<br/>			<value>/jsp/</value>
<br/>		</property>
<br/>		<property name="suffix">
<br/>			<value>.jsp</value>
<br/>		</property>
<br/>	</bean>
<br/></beans>
<br/>






Was it clear so far?



Specific URLs will be handled by specific controllers. One example of such a specific controller is provided below.

<br/>@Controller
<br/>@RequestMapping("/getCatalog")
<br/>public class CourseCatalog{
<br/>   @RequestMapping(method = RequestMethod.GET)
<br/>   public String listCourses(ModelMap model) {
<br/>      // business logic to retrieve the list of courses
<br/>      model.addAttribute("title", "Internet Technology School Course Catalog");
<br/>      model.addAttribute("listOfCourses", "Java
<br/>Databases
<br/>Mobile Apps
<br/>Big Data");
<br/>      return "courses"; // this passes control to the View: a JSP page named courses.jsp
<br/>   }
<br/>}
<br/>

In the code above, we created a new class CourseCatalog, which will be called by the DispatcherServlet when the URL "/getCatalog" will be sent by a browser.

In the model object we collect key ? values that will be used in the View. We collected the title and the list of courses itself.
Note, that we might have several lines of business logic to actually retrieve the list of courses from a data source. Instead, we just hardcoded this object as a long string with line separators.

This is an example of the courses.jsp - the View that exposes the data collected by the specific controller CourseCatalog.

<br/><html>
<br/>   <head>
<br/>   <title>Internet Technology School: Spring MVC</title>
<br/>   </head>
<br/>   <body>
<br/>   <h2>${title}</h2>
<br/>   <h4>${listOfCourses}</h4>
<br/>   </body>
<br/></html>
<br/>

Assignments:
1. Create 3 QnAs on the subject and email all three together in the file 4.4.1.SpringMVC.QnA.Your.Name.txt (use WordPad, not MS Word) to dean@ituniversity.us


| Check Your Progress | Propose QnA | Have a question or comments for open discussion?

Have a suggestion? - shoot an email
Looking for something special? - Talk to me
Read: IT of the future: AI and Semantic Cloud Architecture | Fixing Education
Do you want to move from theory to practice and become a magician? Learn and work with us at Internet Technology University (ITU) - JavaSchool.com.

Technology that we offer and How this works: English | Spanish | Russian | French

Internet Technology University | JavaSchool.com | Copyrights © Since 1997 | All Rights Reserved
Patents: US10956676, US7032006, US7774751, US7966093, US8051026, US8863234
Including conversational semantic decision support systems (CSDS) and bringing us closer to The message from 2040
Privacy Policy