Spring Framework

Spring Projects

Spring Project 1

adplus-dvertising
Example of Spring MVC ViewResolver
Previous Home Next

Introduction: In the Spring framework provide the facility to view the result in front of User through the "InternalResourceViewResolver" class. The Spring framework provide this class to present the information which processing after the controller of the Spring MVC pattern.

Technology use to run this source code:

  1. Spring2.5 jar file
  2. Eclips Id
  3. Tomcat Server

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 id="viewResolver"
class=" org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean name="/first.bean" class="org.r4r.FirstController">
<property name="message" value="My Name Is MUKUND"></property>
</bean>

</beans>

SecondController.java

package org.r4r;
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 {
private String message;

@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
// TODO Auto-generated method stub
return new ModelAndView("welcomePage","welcomeMessage",message);
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}
}

index.jsp

<a href="first.bean">First Controller</a>

welcomePage.jsp

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Welcome Page</title>
</head>
<body>
${welcomeMessage}
</body>
</html>

output

Previous Home Next