InternalResourceViewResolver Spring MVC Example

By Arvind Rai, June 29, 2019
Spring InternalResourceViewResolver is used to access views as JSPs, HTML, and XHTML etc. It extends UrlBasedViewResolver that has two property i.e prefix and suffix. UrlBasedViewResolver decides the URL that will be served to our views using the values defined in prefix and suffix. The views which are in security of InternalResourceViewResolver can be fetched by the URL with given prefix and suffix. We will provide here XML and JavaConfig to define InternalResourceViewResolver. In our demo we are using XML configuration.

Configure InternalResourceViewResolver using XML and JavaConfig

Find the XML file to configure InternalResourceViewResolver using XML configuration.
dispatcher-servlet.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:context="http://www.springframework.org/schema/context" 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">
   <context:component-scan base-package="com.concretepage.controller" />
   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	  <property name="prefix" value="/WEB-INF/pages/"/>
	  <property name="suffix" value=".jsp"/> 
   </bean>
</beans> 
Find JavaConfig to configure InternalResourceViewResolver.
AppConfig.java
@Configuration
@ComponentScan("com.concretepage")
@EnableWebMvc
public class AppConfig {
    @Bean  
    public InternalResourceViewResolver viewResolver() {  
	InternalResourceViewResolver resolver = new InternalResourceViewResolver();  
        resolver.setPrefix("/WEB-INF/pages/");  
        resolver.setSuffix(".jsp");
        return resolver;  
    }	
} 

Create Controller

Create the controller which will use our InternalResourceViewResolver.
LoginController.java
package com.concretepage.controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class LoginController {
	@RequestMapping(value="login", method = RequestMethod.GET)
	public String login(){
		return "login";
	}
	@RequestMapping(value="pages/userCheck", method = RequestMethod.POST)
	public String userCheck(ModelMap model, HttpServletRequest request) {
		String name=request.getParameter("name");
		String pwd=request.getParameter("pwd");
		if("concretepage".equalsIgnoreCase(name)&&"concretepage".equalsIgnoreCase(pwd)){
			model.addAttribute("message", "Successfully logged in.");
		}else{
			model.addAttribute("message", "Username or password is wrong.");
		}
		return "success";
	}
} 

web.xml

Find the web.xml file.
web.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">
	<display-name>Spring MVC Application</display-name>
	<servlet>
		<servlet-name>dispatcher</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>dispatcher</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
</web-app> 

Create View

Create the views which are being kept inside WEB-INF.
login.jsp
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head><title>Login</title></head>
<body>
  <form:form action="pages/userCheck" method="post">
   Enter User Name:<input type="text" name="name"> <br/>
   Enter Password :<input type="password" name="pwd"/><br/>
   <input type="submit">
  </form:form>
</body>
</html> 
success.jsp
<html>
<head><title>Login Success</title></head>
<body>
   Login Message : ${message}
</body>
</html> 

Maven File

Find the maven file to build the project.
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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.concretepage.app</groupId>
  <artifactId>spring-mvc</artifactId>
  <version>1</version>
  <packaging>war</packaging>      
  <name>SpringMVC</name>
  <dependencies>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-web</artifactId>
		<version>3.1.0.RELEASE</version>
		<type>jar</type>
		<scope>compile</scope>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-webmvc</artifactId>
		<version>3.1.0.RELEASE</version>
		<type>jar</type>
		<scope>compile</scope>
	</dependency>
	<dependency>
		<groupId>jstl</groupId>
		<artifactId>jstl</artifactId>
		<version>1.2</version>
		<type>jar</type>
		<scope>compile</scope>
	</dependency>
	<dependency>
		<groupId>taglibs</groupId>
		<artifactId>standard</artifactId>
		<version>1.1.2</version>
		<type>jar</type>
		<scope>compile</scope>
	</dependency>
	<dependency>
		<groupId>javax.servlet</groupId>
		<artifactId>servlet-api</artifactId>
		<version>3.0-alpha-1</version>
		<type>jar</type>
		<scope>compile</scope>
	</dependency>
 </dependencies>
</project> 

Project Structure in Eclipse

Find the project structure in eclipse.
InternalResourceViewResolver Spring MVC Example

Output

To run the example find the URL http://localhost:8080/spring-mvc-1/login and we will get print screen as follows.
InternalResourceViewResolver Spring MVC Example

Download Complete Source Code

POSTED BY
ARVIND RAI
ARVIND RAI







©2024 concretepage.com | Privacy Policy | Contact Us