<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mvc</groupId>
<artifactId>springmvc</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>springmvc Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.13</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>springmvc</finalName>
</build>
</project>
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!-- configure dispatcher servlet -->
<servlet>
<servlet-name>abc</servlet-name>
<servlet-
class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>abc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
package springmvc.controller;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HomeController {
@RequestMapping("/home")
public String home(Model model)
{
model.addAttribute("name","Ajay Sahoo");
model.addAttribute("id",1431);
List<String> friends=new ArrayList<String>();
friends.add("Akshay");
friends.add("Abhay");
friends.add("Ajay");
model.addAttribute("fr",friends);
return "index";
}
@RequestMapping("/about")
public ModelAndView about()
{
ModelAndView modelAndView=new ModelAndView();
modelAndView.addObject("name","Rahul Dravid");
modelAndView.addObject("regdno",101);
LocalDateTime now=LocalDateTime.now();
modelAndView.addObject("time",now);
modelAndView.setViewName("about");
return modelAndView;
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>This is spring mvc</h2>
<%
String s1=(String)request.getAttribute("name");
Integer id=(Integer)request.getAttribute("id");
List<String> list=(List<String>)request.getAttribute("fr");
%>
<h2>The name is<%=s1 %></h2>
<h2>The employee id is<%=id %></h2>
<%
for(String s:list)
{
out.print(s);
}
%>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.time.LocalDateTime" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>Learn Spring MVC Tutorial</h2>
<%
String s=(String)request.getAttribute("name");
Integer regdno=(Integer)request.getAttribute("regdno");
LocalDateTime time=(LocalDateTime)request.getAttribute("time");
%>
<h2>The Name is<%=s %></h2>
<h2>The regdno is<%=regdno %></h2>
<h2>The Date and Time is<%=time.toString() %></h2>
</body>
</html>
Now we will run then we will get the following output;
Right click on project->Run As->Run on Server
Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.
We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc