Created On : 09 Aug 2020
Last Updated on: 09 Aug 2020
===========================
Objective:-
Develop a simple Add Calculator app to demonstrate Spring MVC project
Desired Outcome:-
Links
(a) 'Demo1' Step-by-Step notes [Download]
(b) 'Demo1' Screenshots files [Download]
Step-by-Step.
# Step-by-Step
1.0 Install STS4/Eclipse (done)
1.1 On left corner, click on file-->new-->Search for Dynamic Web Project--> define project name and make sure that the default runtime environment is set to Apache tomcat
1.2 Once the project is created, right click on project --> click on configure-->convert to Maven project
1.3 Once the project is converted, you will be able to see pom.xml file directly inside the project directory
1.4 Open pom.xml and inside the file, right click and click on add dependency(Note:- if the indexing of maven is disable, you have to provide the actuators
manually in the file. for example, below is the list of actuators which have to be added for creating basic Spring MVC project)
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
make sure you add them inside project tag but outside build tag
1.5 Now, right click on project and then click on Maven and then click on update project. All the dependencies defined in pom.xml are basically jar files which gets downloaded on your local system. Internet is required for this step.
1.6 In the project directory, search for web content--> under that right click on WEB-INF, click on on new--> search for xml file, type the name demo1-servlet.xml . 'demo1' is 'servlet-name' name on web.xml
1.7 Since the servlet has fix line of boiler-plate code, so below file can be added:-
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<mvc:annotation-driven />
<context:component-scan
base-package="com.icg.demo" />
<mvc:default-servlet-handler />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
-->note* :- context for base package can be changed according to the package you are specifying for adding controller classes.
1.8 In the project directory, search for web content--> under that right click on WEB-INF, click on on new--> search for xml file, type the name web.xml
web.xml file below:-
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
<display-name>demo</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>demo</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>demo</servlet-name>
<url-pattern>/index.jsp</url-pattern>
<url-pattern>/add.jsp</url-pattern>
<url-pattern>/added.jsp</url-pattern>
<url-pattern>/add.html</url-pattern>
<url-pattern>/added.html</url-pattern>
</servlet-mapping>
</web-app>
Note:- Remember that whatever jsp files you are declaring, needs to be defined in the above file with both .jsp and .html pattern. The file can be edited as per the project requirement
1.9 Now right click WEB-INF, click on new-->folder--> name it jsp (you can change name but then you make changes in servlet file as well)
2.0 Now, right click on jsp folder and create the desired jsp file you want to create(Remember to configure in web.xml as well as stated above)
2.1 Under the project directory, click on java resources--> right click on src--> create --> new--> package--> com.icg.demo(you can define any package name but make it align in the format provided)
2.2 Now right click on new package new created---> create new --> java file---> name it AddController.java(you can define any name). Since, we are defining the controller class in this package , make sure it is configured in the dispatcher-servlet .xml file as well. Refer note* of step 1.7
sample file:-
package com.icg.demo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class AddController {
@RequestMapping(value="add", method=RequestMethod.GET)
public ModelAndView add(@ModelAttribute AddModel model) {
ModelAndView m1 = new ModelAndView("add");
return m1;
}
@RequestMapping(value="added", method=RequestMethod.POST)
public ModelAndView added(@ModelAttribute AddModel model) {
ModelAndView m1 = new ModelAndView("added");
System.out.println("check val=="+model.getXval());
int c=model.getXval()+model.getYval();
m1.addObject("val", c);
return m1;
}
}
'AddController.java' added. Error on 'AddModel' shall go once 'AddMOdel.java added in subsequent steps
2.3 Create new model class(POJO) inside the same package and give it a name, remember to generate getters and setters method. Define attributes/properties/elements of the class. Right click and click on Generate Getters and Setters.
sample POJO class:-
package com.icg.demo;
public class AddModel {
private int xval;
private int yval;
public int getXval() {
return xval;
}
public void setXval(int xval) {
this.xval = xval;
}
public int getYval() {
return yval;
}
public void setYval(int yval) {
this.yval = yval;
}
@Override
public String toString() {
return "AddModel [xval=" + xval + ", yval=" + yval + "]";
}
}
2.4 Add 'index.jsp' file under 'demo1-->WebContent'
2.5 Configuration check--> we need to make sure that JDK is added on build path and not JRE, otherwise, compiler will generate issues
click on window--> preferences-->under java--> click on Installed JREs--> if the path is set to JDK, that means it is fine, if not then make sure the JDK path is set
2.6 Now right click on project--> click on -->Run As--> Maven Clean. Wait for the build to be completed---> once completed, check if the BUILD SUCCESS message is there or nor---> if not , debug it
2.7 Now right click on project--> click on -->RUN--> Run on server--> run the build on tomcat
2.8 Always remember that index.jsp is the starting point once project run successfully.
No comments:
Post a Comment