In this tutorial i want to explain about how to do spring 4 MVC Java Configuration with example. Java configuration doesn’t require an appContext.xml or even a servlet-config.xml we can do all our configurations in java classes by using @Configuration and @EnableWebMVC annotations. Spring provided set of annotations for various Configuration elements like component-scan, interceptors, handlers, formatters, converts, etc. One more think in this example I also covered WebAppInitializer class using this we can run the web application without web.xml.

First setup project: There are multiple ways to setup a project in eclipse or spring STS all are previous methods you used will works fine. There is also option to use spring web project inside our IDE and more convenient way using spring STS 3.5+ and spring 4
Step 1: Open the spring STS and go to the package explorer right click on it select new-> other and go down to Spring and Spring Starter Project then click next provide the required details check the web and finally click finish


Provide the required details project name, type, packaging, java version, language, and boot version.

Note: Some times we get the issue with boot version if you face any problem please go to pom.xml file change the version to 1.0.2 manually


Now will go to learn how java configuration is different from xml configuration.

Create a WebConfig.java

Now right click on com.studywithdemo site select create a class and named as WebConfig.java go head and click finish. I have added Configuration and EnableWebMVC annotations here once check it. Most of the configuration is done using two annotations.

EnableWebMVC: The EnableWebMVC is only used for java configuration of spring MVC web apps

Component scan in xml: It is used to scan our base packages

package com.studywithdemo;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.studywithdemo")
public class WebConfig {
 
 @Bean
 public InternalResourceViewResolver getInternalResourceViewResolver() {
  InternalResourceViewResolver resolver = new InternalResourceViewResolver();
  resolver.setPrefix("/WEB-INF/");
  return resolver;
 }
 
}
Using WebAppInitializer.java instead of web.xml

The servlet 3+ specification negates the need for having web.xml.

package com.studywithdemo;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

public class WebAppInitializer implements WebApplicationInitializer {

 @Override
 public void onStartup(ServletContext servletContext) throws ServletException {
  WebApplicationContext context = getContext();
  servletContext.addListener(new ContextLoaderListener(context));
  ServletRegistration.Dynamic dispatcher = servletContext.addServlet("DispatcherServlet", 
   new DispatcherServlet(context));
  dispatcher.setLoadOnStartup(1);
  dispatcher.addMapping("*.html");
 }

 private AnnotationConfigWebApplicationContext getContext() {
  AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
  context.setConfigLocation("com.studywithdemo.WebConfig");
  return context;
 }

}
There is no changes in controller and view pages it works as usually like previous versions.

WishController.java
package com.studywithdemo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class WishController {

 @RequestMapping(value = "/wishes")
 public String sayHello(Model model) {
  model.addAttribute("wishes", "Thank You For Visiting StudywithDemo");
  
  return "wish.jsp";
 }
}
wish.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Hello</title>
</head>
<body>
 <h1>${wishes}</h1>
</body>
</html>
Note: please observe the WebConfig.java i also configured View Resolver in java class. 

0 comments:

Post a Comment

 
Top