In this Example
1. Model Ã
Simple Beans
2.
Controllers
·
FrontController à DispatcherServlet
·
User Controller Ã
Controller (Interface)
3.
View
·
JSP’s,
·
InternalresourceViewResolver
class
4.
Xml’s
·
Web.xml Ã
to configure FrontController
·
Spring.xml Ã
Configure the Usercontroller (controller-servlet.xml)
Controller Interface
·
It is the Basic Controller
·
It contains public ModelandView
handleRequest(req,res)
·
While returing ModelandView(“page” Map) we must place all the data to Map
object
InternalresourceViewResolver Class
·
Used for result page mappings
·
It has two properties, we have to configure
these in spring.xml
·
Prefix Ã
for Folder / Directory where pages are placed
·
Suffix Ã
extension of result pages (.jsp, .html etc)
ModelandView Class
·
It contains the model data like id,name,bean data
·
View data like success, error, other pages
·
It has parameter constructor ModelandView(“page”
Map)
·
Page contains result page name
·
Map contains all bean data return to resultpage
HandlerMapping class
·
It is a mediator between FrontController
and User specific Controller
·
After getting request Dispacherservlet
asks HandlerMapping , which Specific Controller to be
called
·
HandlerMapping gives the Specific Controller to FrontController
Exmaple
Code
Input.jsp
<form action="./hello.ds">
Name : <input type="text"
name="name"><br>
<button type="submit">Submit</button>
</form>
Success.jsp
<h1>SUCCESS</h1>
${msg}
HelloServlet.java
package controller;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class HelloServlet
implements Controller{
@Override
public ModelAndView
handleRequest(HttpServletRequest
req,
HttpServletResponse
res) throws Exception {
// TODO Auto-generated
method stub
String msg = req.getParameter("name");
Map map = new HashMap();
map.put("msg", msg);
return new ModelAndView("success",map);
}
}
Web.xml
<?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" 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>MVC_1helloworld</display-name>
<welcome-file-list>
<welcome-file>input.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>*.ds</url-pattern>
</servlet-mapping>
</web-app>
hello-servlet.xml
<!DOCTYPE beans
PUBLIC "-//SPRING//DTD
BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<!-- 1. Handler Class Conguration -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
<!-- 1. Controller
Class Conguration -->
<bean name="/hello.ds" class="controller.HelloServlet"></bean>
<!-- 1. ViewandResolve Class Conguration
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix"
value="/" ></property>
<property name="suffix"
value=".jsp"></property>
</bean>
</beans>
===============================================
EXCEPTION RESOLVING NOTES
==================================
in web.xml
/ ==> means for all URLs
*.ds ==> means URL's with .ds extension
/hello ==> mean URL with hello as action
in hello-servlet.xml
<bean name="/hello" class="controller.HelloServlet"></bean>
name="/hello" ==> must be same as ACTION Name in <form action="hello">
xml name should be actionname-servlet.xml
==> all XML's web.xml and hello-servlet.xml must be Under WEB-INF/ Folder
------------------------XSD---------------------------
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
-------------------------------------------------------------------
===============================================
EXCEPTION RESOLVING NOTES
==================================
in web.xml
/ ==> means for all URLs
*.ds ==> means URL's with .ds extension
/hello ==> mean URL with hello as action
in hello-servlet.xml
<bean name="/hello" class="controller.HelloServlet"></bean>
name="/hello" ==> must be same as ACTION Name in <form action="hello">
xml name should be actionname-servlet.xml
==> all XML's web.xml and hello-servlet.xml must be Under WEB-INF/ Folder
------------------------XSD---------------------------
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
-------------------------------------------------------------------