
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>Spring4HelloWorld</display-name> <display-name>Spring4MVC</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>spring4</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring4</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping> </web-app>
<html> <head> <title>dineshonjava.com | Hello Spring 4 World</title> </head> <body> <center> <h2>dineshonjava.com | Hello Spring 4 World</h2> <h4><a href="hello.html">Click Here</a></h4> </center> </body> </html>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
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-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan base-package="com.doj.spring4.controller" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/view/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
HelloSpring4Controller.javapackage com.doj.spring4.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/**
* @author Dinesh Rajput
*
*/
@Controller
public class HelloSpring4Controller {
@RequestMapping("/hello")
public ModelAndView sayHello() {
String message = "Welcome to Spring 4.0 !!! dineshonjava.com";
return new ModelAndView("hello", "message", message);
}
}
This is a simple controller class for our application, to make a class work as controller we need to add a '@Controller' annotation. To match the appropriate url with controller method add a '@RequestMapping' annotation there enclosed within double cotes. <html>
<head>
<title>dineshonjava.com | Hello Spring 4 World</title>
</head>
<body>
<center>
<h2>dineshonjava.com | Hello Spring 4 World</h2>
<h4>${message}</h4>
</center>
</body>
</html>


Labels: Spring4