![]() |
VOOZH | about |
Spring MVC is a powerful Web MVC framework for building web applications. It is designed around the Model-View-Controller (MVC) pattern, which separates the application into three main components Model, View and Controller.
- Eclipse (EE version)
- Spring JAR Files
- Apache Tomcat
Followings are the steps to implement the first model in Spring MVC:
File: web.xml:
Note: One should be well aware of what is Dispatcher Servlet in Spring as it is a crucial concept to understand prior adhering ahead.
And the name of the file must be in this format:
YourServletName-servlet.xml
For example: For this project, the name of the file must be
viewresolver-dispatcher-servlet.xml
So either you can create a Spring Configuration File or you can just create a simple XML file and add the below lines of code inside that file.
File: viewresolver-dispatcher-servlet.xml:
@Controller
public class DemoController {}
Note: Spring will automatically initialize the class having a @Controller annotation and register that class with the spring container.
@RequestMapping("/hello")"demo"// Annotation
@RequestMapping("/hello")
// Method
public String helloWorld()
{
return "";
}
Now in the return statement, we have to return some views (web pages), so whenever the endpoint '/hello' is invoked we can see our result on the web page. So let's create our first View.
File: demo.jsp:
Now go to the DemoController class and inside the helloWorld() method we have to return a value something like this.
return "demo";
File: DemoController.java:
Modify controller method:
File: DemoController.java:
${myNameValue}
Now the complete code for Demo.jsp is given below and you are done.
After that use the following URL to run your controller
http://localhost:8080/springmvc-view-resolver/demo.com/hello
Output: