Register Login
Internet / AI Technology University (ITU/AITU)





|

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.2.Spring MVC and Apache Maven with Eclipse
Sub-Topics: 4.4.2.1.Working with JSON
-- Scroll to check for more content below...
You have a privilege to create a quiz (QnA) related to this subject and obtain creativity score...
4.4.2.Spring MVC and Apache Maven with Eclipse

Let us create a Maven Web Project with Spring framework.
Follow the steps 1 – 4 on the illustration below.

SpringMaven

1. Open Eclipse and choose New – Maven Project
2. In the open window just press the NEXT control.
3. This is the important step of choosing a maven archetype, a project template. Select maven-archetype-webapp
4. Provide a Group ID the same as the Package name com.its.samples.spring and provide the Artifact ID as the project name 4.4.2.MavenSpring

Click on the Finish control and check a new project that will appear on the left side panel.
You will notice that the project has the red flag/error in the src – webapp – index.jsp area.
The reason is simple. We created a web project, but not supported the project by proper libraries.
The fix is easy. Follow the illustrated instructions below.
1. Do right mouse click on the project and select the Build Path – Configure Build Path option. Check the Libraries tab and ADD LIBRARY control.
2. Select the Server Runtime library
3. Choose the Tomcat Server (v7 or later)
4. Check one more thing: Eclipse often provides a default for the JRE System Library. If so, REMOVE this library
5. Then use the Add Library control and select JRE System Library
6. And make sure that your installed JDK (c:\jdk) is selected as the JRE System Library

SpringMaven2

As you finish these steps, the red flag/error will disappear from the project.
What do we do next?

We need to configure the project for Spring framework, make sure the pom.xml file includes necessary dependences and can automatically download Spring libraries.

Find the pom.xml file in the project and add the following dependency lines before the end of the dependencies section.

	



org.springframework
spring-core
4.2.1.RELEASE



org.springframework
spring-web
4.2.1.RELEASE



org.springframework
spring-webmvc
4.2.1.RELEASE



The project configuration is ready for the development steps.
Take another look at the Web App Development Steps with the Maven Spring Project.

WebAppStepsMavenSpring

We just accomplished the step 1: created and configured the Maven Spring project.
In the step 2 we create the web.xml file, where we point to the DispatcherServlet as the main target for the browser requests. This is our code for the web.xml file, we keep it without changes.


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
/




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

We will keep the View files in the jsp directory with the extension .jsp.
The same file will also define the package name as com.its.samples.spring for specific controllers in the project.
Check the content of the ItsCommonController-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? Highlight the text in question Or


In the step 3 we will modify the index.jsp file under the WebContent folder.
The page will include the link that points to the controller via a specified url-pattern.
Here is the code for the index.jsp page:




Internet Technology School


Example of the Maven Spring Project


The link below points to the controller and includes the RequestMapping pattern /getCatalog


Click here to get the ITS catalog of courses.



In the step 4, we create a Java class as a specific controller to response to the specific URL /getCatalog provided with the annotation @RequestMapping.

Open the Java Resources in the project and create the package com.its.samples.spring under the src/main/java – folder in the Java Resources.

Right mouse click on the package – NEW – CLASS – CourseCatalog

Here is the code for the CourseCatalog class.

@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 the class CourseCatalog, which will be called when the URL "/getCatalog" is sent by a browser.
The CourseCatalog class is red with errors… Until we add all necessary import lines. Do this with one click by using the short-key for adding the imports. Get focus on the source and press CTRL-Shift-O characters to do this magic. The class will get import lines and the red flags disappear.

Note that 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.
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.
The class returns the name of the View. In this example the name of the View is jsp/courses.jsp.
The View will expose the data collected by the specific controller CourseCatalog.

In the step 5, we create the View, the courses.jsp page in the jsp folder.




Internet Technology School: Spring MVC


${title}


${listOfCourses}






The project structure and the illustrations to steps 5 and 6 are presented below.

WebAppStepsMavenSpring5-6

Check the Console for server output.
In the step 7 start a Web Browser with the URL to the project:
Make sure that you properly enter the port number.
http://localhost:81/4.4.2.MavenSpring/
If your Tomcat configuration uses the port 80, there is no need to provide any port number. The port 80 is the default port for Web applications. The URL will look this way.
http://localhost/4.4.2.MavenSpring/

This URL only includes the project name. By default the web server will check if there is the index file. Our project has index.jsp file. So the Web server will find this file and display its content.

If there is no response or the response is HTTP Status 404 - not found, check the tomcat/webapps – directory. Check if the project was actually published, if it has a full content, including the index.jsp file, the WEB-INF directory with the classes directory inside, etc.

Eclipse may do one of its jokes on you. Take is easy. One simple cure is trying to use the Add – Remove options to remove the project, then add it again and re-publish.
If this does not help, this might be an indication that the Tomcat server configuration in Eclipse is corrupted. You can delete the Tomcat server configuration in Eclipse and Add the Tomcat server again.

Delete-Add-Tomcat-inEclipse

Do not forget to change the Deploy Path to your {Tomcat}\webapps location and to modify the port from 80 to 81, if you have some other application, for example Oracle Express Web App, running on the port 80. Then, save the configuration! Then ADD (Add and Remove) the project to the server and Publish it. Now START it again.

In the next section we provide more hints on Troubleshooting (Debugging) in Web Applications.

The resulting pages of the project are below.

4.4.2.results

There is initial index.jsp page with the link. After the click on the link, the request with the specific URL /getCatalog is directed to the specific controller CourseCatalog, which collects data and passes the data to the View, which is present in the /jsp/courses.jsp.

Assignments:
1. Create a new project Eclipse 4.4.2.1.MavenSpring.Your.Name
2. Follow the same steps provided above and recreate the same content.
3. Make sure it works and then modify the index.jsp, the CourseCatalog and the courses.jsp code.
4. Compress the new project sources (including web.xml and other sources in webapps) in a zip file: 4.4.2.1.Your.Name.zip and email to dean@ituniversity.us
5. Create 3 QnAs (use WordPad) related to this content, place in a single file 4.4.2.QnAs.Your.Name.txt and email to dean@ituniversity.us
Questions and Answers (QnA): QnA1 | QnA2 | QnA3
We offer a fun way to share your experience and be rewarded.
You create a puzzle - quiz with a good question and several answers, one correct and several wrong ones.
The question as well as answers can include text, links, video.
This is your creation, completely up to your taste, humor, and of course your knowledge.
If there is an existing quiz above, you need first to solve the puzzles and evaluate the quality (at the bottom of every quiz).
After finishing the existing quiz you will be able to create your own. The best puzzles will be rewarded!
We invite you to create your own questions and answers (QnA) to increase your rank and win the Top Creativity Prize!

Topic Graph | Check Your Progress | Propose QnA | Have a question or comments for open discussion?
	
<br/><!-- Spring dependencies -->
<br/><dependency>
<br/>	<groupId>org.springframework</groupId>
<br/>	<artifactId>spring-core</artifactId>
<br/>	<version>4.2.1.RELEASE</version>
<br/></dependency>
<br/>
<br/><dependency>
<br/>	<groupId>org.springframework</groupId>
<br/>	<artifactId>spring-web</artifactId>
<br/>	<version>4.2.1.RELEASE</version>
<br/></dependency>
<br/>
<br/><dependency>
<br/>	<groupId>org.springframework</groupId>
<br/>	<artifactId>spring-webmvc</artifactId>
<br/>	<version>4.2.1.RELEASE</version>
<br/></dependency>
<br/>


The project configuration is ready for the development steps.
Take another look at the Web App Development Steps with the Maven Spring Project.

WebAppStepsMavenSpring

We just accomplished the step 1: created and configured the Maven Spring project.
In the step 2 we create the web.xml file, where we point to the DispatcherServlet as the main target for the browser requests. This is our code for the web.xml file, we keep it without changes.

<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/>


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

We will keep the View files in the jsp directory with the extension .jsp.
The same file will also define the package name as com.its.samples.spring for specific controllers in the project.
Check the content of the ItsCommonController-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? Highlight the text in question

Or



In the step 3 we will modify the index.jsp file under the WebContent folder.
The page will include the link that points to the controller via a specified url-pattern.
Here is the code for the index.jsp page:

<br/><html>
<br/><body>
<br/><h2>Internet Technology School</h2>
<br/><h3>Example of the Maven Spring Project</h3>
<br/><h4>The link below points to the controller and includes the RequestMapping pattern /getCatalog</h4>
<br/>Click <a href="/4.4.2.MavenSpring/getCatalog">here</a> to get the ITS catalog of courses.
<br/></body>
<br/></html>
<br/> 

In the step 4, we create a Java class as a specific controller to response to the specific URL /getCatalog provided with the annotation @RequestMapping.

Open the Java Resources in the project and create the package com.its.samples.spring under the src/main/java – folder in the Java Resources.

Right mouse click on the package – NEW – CLASS – CourseCatalog

Here is the code for the CourseCatalog class.
<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 the class CourseCatalog, which will be called when the URL "/getCatalog" is sent by a browser.
The CourseCatalog class is red with errors… Until we add all necessary import lines. Do this with one click by using the short-key for adding the imports. Get focus on the source and press CTRL-Shift-O characters to do this magic. The class will get import lines and the red flags disappear.

Note that 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.
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.
The class returns the name of the View. In this example the name of the View is jsp/courses.jsp.
The View will expose the data collected by the specific controller CourseCatalog.

In the step 5, we create the View, the courses.jsp page in the jsp folder.

<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/>


The project structure and the illustrations to steps 5 and 6 are presented below.

WebAppStepsMavenSpring5-6

Check the Console for server output.
In the step 7 start a Web Browser with the URL to the project:
Make sure that you properly enter the port number.
http://localhost:81/4.4.2.MavenSpring/
If your Tomcat configuration uses the port 80, there is no need to provide any port number. The port 80 is the default port for Web applications. The URL will look this way.
http://localhost/4.4.2.MavenSpring/

This URL only includes the project name. By default the web server will check if there is the index file. Our project has index.jsp file. So the Web server will find this file and display its content.

If there is no response or the response is HTTP Status 404 - not found, check the tomcat/webapps – directory. Check if the project was actually published, if it has a full content, including the index.jsp file, the WEB-INF directory with the classes directory inside, etc.

Eclipse may do one of its jokes on you. Take is easy. One simple cure is trying to use the Add – Remove options to remove the project, then add it again and re-publish.
If this does not help, this might be an indication that the Tomcat server configuration in Eclipse is corrupted. You can delete the Tomcat server configuration in Eclipse and Add the Tomcat server again.

Delete-Add-Tomcat-inEclipse

Do not forget to change the Deploy Path to your {Tomcat}\webapps location and to modify the port from 80 to 81, if you have some other application, for example Oracle Express Web App, running on the port 80. Then, save the configuration! Then ADD (Add and Remove) the project to the server and Publish it. Now START it again.

In the next section we provide more hints on Troubleshooting (Debugging) in Web Applications.

The resulting pages of the project are below.

4.4.2.results

There is initial index.jsp page with the link. After the click on the link, the request with the specific URL /getCatalog is directed to the specific controller CourseCatalog, which collects data and passes the data to the View, which is present in the /jsp/courses.jsp.

Assignments:
1. Create a new project Eclipse 4.4.2.1.MavenSpring.Your.Name
2. Follow the same steps provided above and recreate the same content.
3. Make sure it works and then modify the index.jsp, the CourseCatalog and the courses.jsp code.
4. Compress the new project sources (including web.xml and other sources in webapps) in a zip file: 4.4.2.1.Your.Name.zip and email to dean@ituniversity.us
5. Create 3 QnAs (use WordPad) related to this content, place in a single file 4.4.2.QnAs.Your.Name.txt and email to dean@ituniversity.us
Questions and Answers (QnA): QnA1 | QnA2 | QnA3

We offer a fun way to share your experience and be rewarded.
You create a puzzle - quiz with a good question and several answers, one correct and several wrong ones.
The question as well as answers can include text, links, video.
This is your creation, completely up to your taste, humor, and of course your knowledge.
If there is an existing quiz above, you need first to solve the puzzles and evaluate the quality (at the bottom of every quiz).
After finishing the existing quiz you will be able to create your own. The best puzzles will be rewarded!

We invite you to create your own questions and answers (QnA) to increase your rank and win the Top Creativity Prize!


Topic Graph | 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 AI
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