Spring Framework

Spring Projects

Spring Project 1

Example of Spring MVC Implementation
Previous Home Next

Introduction: In Spring MVC implementation the controller is an application component. Which is responsible for processing the request. It does not controlled the workflow of request processing as done by struts controller.

Workflow of request is controlled by front controller which is implemented by servlet name "DispatcherServlet: is provides a single entry point in to the application.

Technology use to run this source code:

  1. Spring2.5 jar file
  2. Eclips Id
  3. Tomcat Server
  4. Source Code:-

Source code

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>frontController</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>frontController</servlet-name>
<url-pattern>*.bean</url-pattern>
</servlet-mapping>
</web-app>

 

frontcontroller.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<bean name="/first.bean" class="org.r4r.SecondControlleer"></bean>
</beans>

myview.java

package org.r4r;

import java.io.PrintWriter;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.View;

public class MyView implements View {
public String getContentType() {
return "text/html";
}

@SuppressWarnings("unchecked")
public void render(Map model, HttpServletRequest request,
HttpServletResponse response) throws Exception {
PrintWriter out=response.getWriter();
out.println("This view is generated by spring view....");
out.println("First Name:-\t"+model.get("firstName"));
out.println("Last Name:-\t"+model.get("lastName"));
out.close();
}

}

FirstController.java

package org.r4r;

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.AbstractController;

public class FirstController extends AbstractController {

@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
Map<String, String> map=new HashMap<String, String>();
map.put("firstName", "Mukund");
map.put("lastName", "Singh");
ModelAndView mav=new ModelAndView(new MyView(),map);
return mav;
}
}

index.jsp

<a href="first.bean">My First Spring MVC</a><br/>

output

Previous Home Next

Tolal:0 Click:

Show All Comments

Did not find what you were looking for leave your name and message. We will revert within 24 hours
Name:
eMail:
Comment / Feedback: