0% found this document useful (1 vote)
359 views

Spring MVC Interview Questions With Answers - HowToDoInJava

The document discusses Spring MVC concepts and configuration. It explains how to configure the DispatcherServlet in web.xml to handle requests, and how to define controllers with the @Controller annotation and map them to requests with the @RequestMapping annotation. It also covers configuring the ViewResolver to render JSP views, integrating Spring with other frameworks like Hibernate, and using the @ResponseBody annotation to return JSON responses from controller methods.

Uploaded by

navdeepgsp
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
359 views

Spring MVC Interview Questions With Answers - HowToDoInJava

The document discusses Spring MVC concepts and configuration. It explains how to configure the DispatcherServlet in web.xml to handle requests, and how to define controllers with the @Controller annotation and map them to requests with the @RequestMapping annotation. It also covers configuring the ViewResolver to render JSP views, integrating Spring with other frameworks like Hibernate, and using the @ResponseBody annotation to return JSON responses from controller methods.

Uploaded by

navdeepgsp
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Spring MVC Interview Questions with Answers - HowToDoInJava https://howtodoinjava.com/interview-questions/spring-mvc-interview-que...

1 of 29 11/3/2017 5:19 PM
Spring MVC Interview Questions with Answers - HowToDoInJava https://howtodoinjava.com/interview-questions/spring-mvc-interview-que...

<web-app>
<display-name>Archetype Created Web Application</display-name>

<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>

2 of 29 11/3/2017 5:19 PM
Spring MVC Interview Questions with Answers - HowToDoInJava https://howtodoinjava.com/interview-questions/spring-mvc-interview-que...

<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

</web-app>

<servlet_name>-servlet.xml

<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>

<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</init-param>

<load-on-startup>1</load-on-startup>
</servlet>

<!-- Spring MVC support -->

3 of 29 11/3/2017 5:19 PM
Spring MVC Interview Questions with Answers - HowToDoInJava https://howtodoinjava.com/interview-questions/spring-mvc-interview-que...

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>

<!-- Tag libs support for view layer -->

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
<scope>runtime</scope>
</dependency>

web.xml

<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<beans>
<!-- Scan all classes in this path for spring specific annotations -->
<context:component-scan base-package="com.howtodoinjava.demo" />

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />


<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

4 of 29 11/3/2017 5:19 PM
Spring MVC Interview Questions with Answers - HowToDoInJava https://howtodoinjava.com/interview-questions/spring-mvc-interview-que...

<!-- Vierw resolver configuration -->


<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>

</beans>

@Controller
@RequestMapping("/employee-module")
public class EmployeeController
{
@Autowired
EmployeeManager manager;

@RequestMapping(value = "/getAllEmployees", method = RequestMethod.GET)


public String getAllEmployees(Model model)
{
model.addAttribute("employees", manager.getAllEmployees());
return "employeesListDisplay";
}
}

<!-- Jackson JSON Processor -->


<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.1</version>
</dependency>

@ResponseBody

5 of 29 11/3/2017 5:19 PM
Spring MVC Interview Questions with Answers - HowToDoInJava https://howtodoinjava.com/interview-questions/spring-mvc-interview-que...

@Controller
public class EmployeeRESTController
{
@RequestMapping(value = "/employees")
public @ResponseBody EmployeeListVO getAllEmployees()
{
EmployeeListVO employees = new EmployeeListVO();
//Add employees
return employees;
}
}

@RestController @Controller
@ResponseBody

@RestController
public class EmployeeRESTController
{
@RequestMapping(value = "/employees")
public EmployeeListVO getAllEmployees()
{
EmployeeListVO employees = new EmployeeListVO();
//Add employees
return employees;
}
}

web.xml

<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>

6 of 29 11/3/2017 5:19 PM
Spring MVC Interview Questions with Answers - HowToDoInJava https://howtodoinjava.com/interview-questions/spring-mvc-interview-que...

<param-value>
WEB-INF/spring-dao-hibernate.xml,
WEB-INF/spring-services.xml,
WEB-INF/spring-security.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<beans>
<import resource="spring-dao-hibernate.xml"/>
<import resource="spring-services.xml"/>
<import resource="spring-security.xml"/>

... //Other configuration stuff

</beans>

<context:annotation-config>

<context:component-scan>

<context:component-scan>
<context:annotation-config>

7 of 29 11/3/2017 5:19 PM
Spring MVC Interview Questions with Answers - HowToDoInJava https://howtodoinjava.com/interview-questions/spring-mvc-interview-que...

@Component

@Component
public class EmployeeDAOImpl implements EmployeeDAO {
...
}

@Repository @Component

DataAccessException

@Service

@Component

@Controller @Component

@Controller @RequestMapping

ViewResolver

InternalResourceViewResolver

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>

/WEB-
INF/views/login.jsp

8 of 29 11/3/2017 5:19 PM
Spring MVC Interview Questions with Answers - HowToDoInJava https://howtodoinjava.com/interview-questions/spring-mvc-interview-que...

MultipartResolver

DispatcherServlet MultipartResolver

MultipartHttpServletRequest MultipartFiles

CommonsMultipartResolver

<!-- Apache Commons Upload -->


<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.2</version>
</dependency>

<!-- Apache Commons Upload -->


<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>

MultipartResolver

<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- one of the properties available; the maximum file size in bytes -->
<property name="maxUploadSize" value="100000"/>
</bean>

FileUploadForm

import org.springframework.web.multipart.MultipartFile;

9 of 29 11/3/2017 5:19 PM
Spring MVC Interview Questions with Answers - HowToDoInJava https://howtodoinjava.com/interview-questions/spring-mvc-interview-que...

public class FileUploadForm


{
private MultipartFile file;

public MultipartFile getFile() {


return file;
}

public void setFile(MultipartFile file) {


this.file = file;
}
}

FileUploadController

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import com.howtodoinjava.form.FileUploadForm;

@Controller
public class FileUploadController
{
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public String save(@ModelAttribute("uploadForm") FileUploadForm uploadForm, Model map) {

MultipartFile multipartFile = uploadForm.getFile();

String fileName = "default.txt";

if (multipartFile != null) {
fileName = multipartFile.getOriginalFilename();
}

//read and store the file as you like

map.addAttribute("files", fileName);
return "file_upload_success";
}
}

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>


<html>
<body>
<h2>Spring MVC file upload example</h2>
<form:form method="post" action="save.html" modelAttribute="uploadForm" enctype="multipart/form-
data">
Please select a file to upload : <input type="file" name="file" />
<input type="submit" value="upload" />

10 of 29 11/3/2017 5:19 PM
Spring MVC Interview Questions with Answers - HowToDoInJava https://howtodoinjava.com/interview-questions/spring-mvc-interview-que...

<span><form:errors path="file" cssClass="error" /></span>


</form:form>
</body>
</html>

Validator
Validator
rejectIfEmptyOrWhitespace( rejectIfEmpty() ValidationUtils

@Component
public class EmployeeValidator implements Validator
{
public boolean supports(Class clazz) {
return EmployeeVO.class.isAssignableFrom(clazz);
}

public void validate(Object target, Errors errors)


{
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "firstName", "error.firstName", "First name
is required.");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "lastName", "error.lastName", "Last name is
required.");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "email", "error.email", "Email is
required.");
}
}

@Component EmployeeValidator

11 of 29 11/3/2017 5:19 PM
Spring MVC Interview Questions with Answers - HowToDoInJava https://howtodoinjava.com/interview-questions/spring-mvc-interview-que...

<context:component-scan base-package="com.howtodoinjava.demo" />

<bean id="employeeValidator" class="com.howtodoinjava.demo.validator.EmployeeValidator" />

HandlerInterceptor

HandlerInterceptor
preHandle() postHandle() afterCompletion()

HandlerInterceptor
HandlerInterceptorAdapter
HandlerInterceptor

HandlerExceptionResolver
DispatcherServlet
SimpleMappingExceptionResolver

AuthException
/WEB-INF/views/error
/authExceptionView.jsp

<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">

12 of 29 11/3/2017 5:19 PM
Spring MVC Interview Questions with Answers - HowToDoInJava https://howtodoinjava.com/interview-questions/spring-mvc-interview-que...

<props>
<prop key="com.howtodoinjava.demo.exception.AuthException">
error/authExceptionView
</prop>
</props>
</property>
<property name="defaultErrorView" value="error/genericView"/>
</bean>

LocaleResolver

<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">


<property name="defaultLocale" value="en" />
</bean>

<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>

<!-- Enable the interceptor -->


<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
<list>
<ref bean="localeChangeInterceptor" />
</list>
</property>
</bean>

13 of 29 11/3/2017 5:19 PM
Spring MVC Interview Questions with Answers - HowToDoInJava https://howtodoinjava.com/interview-questions/spring-mvc-interview-que...

messages.properties messages_zh_CN.properties

ServletContextAware ServletConfigAware

@Controller
@RequestMapping(value = "/magic")
public class SimpleController implements ServletContextAware, ServletConfigAware {

private ServletContext context;


private ServletConfig config;

@Override
public void setServletConfig(final ServletConfig servletConfig) {
this.config = servletConfig;

@Override
public void setServletContext(final ServletContext servletContext) {
this.context = servletContext;
}

//other code
}

JdbcTemplate

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">


<property name="jndiName" value="java:comp/env/jdbc/MySQLDB"/>
</bean>

14 of 29 11/3/2017 5:19 PM
Spring MVC Interview Questions with Answers - HowToDoInJava https://howtodoinjava.com/interview-questions/spring-mvc-interview-que...

15 of 29 11/3/2017 5:19 PM
Spring MVC Interview Questions with Answers - HowToDoInJava https://howtodoinjava.com/interview-questions/spring-mvc-interview-que...

16 of 29 11/3/2017 5:19 PM
Spring MVC Interview Questions with Answers - HowToDoInJava https://howtodoinjava.com/interview-questions/spring-mvc-interview-que...

17 of 29 11/3/2017 5:19 PM
Spring MVC Interview Questions with Answers - HowToDoInJava https://howtodoinjava.com/interview-questions/spring-mvc-interview-que...

18 of 29 11/3/2017 5:19 PM
Spring MVC Interview Questions with Answers - HowToDoInJava https://howtodoinjava.com/interview-questions/spring-mvc-interview-que...

19 of 29 11/3/2017 5:19 PM
Spring MVC Interview Questions with Answers - HowToDoInJava https://howtodoinjava.com/interview-questions/spring-mvc-interview-que...

20 of 29 11/3/2017 5:19 PM
Spring MVC Interview Questions with Answers - HowToDoInJava https://howtodoinjava.com/interview-questions/spring-mvc-interview-que...

21 of 29 11/3/2017 5:19 PM
Spring MVC Interview Questions with Answers - HowToDoInJava https://howtodoinjava.com/interview-questions/spring-mvc-interview-que...

22 of 29 11/3/2017 5:19 PM
Spring MVC Interview Questions with Answers - HowToDoInJava https://howtodoinjava.com/interview-questions/spring-mvc-interview-que...

<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

23 of 29 11/3/2017 5:19 PM
Spring MVC Interview Questions with Answers - HowToDoInJava https://howtodoinjava.com/interview-questions/spring-mvc-interview-que...

24 of 29 11/3/2017 5:19 PM
Spring MVC Interview Questions with Answers - HowToDoInJava https://howtodoinjava.com/interview-questions/spring-mvc-interview-que...

25 of 29 11/3/2017 5:19 PM
Spring MVC Interview Questions with Answers - HowToDoInJava https://howtodoinjava.com/interview-questions/spring-mvc-interview-que...

26 of 29 11/3/2017 5:19 PM
Spring MVC Interview Questions with Answers - HowToDoInJava https://howtodoinjava.com/interview-questions/spring-mvc-interview-que...

27 of 29 11/3/2017 5:19 PM
Spring MVC Interview Questions with Answers - HowToDoInJava https://howtodoinjava.com/interview-questions/spring-mvc-interview-que...

28 of 29 11/3/2017 5:19 PM
Spring MVC Interview Questions with Answers - HowToDoInJava https://howtodoinjava.com/interview-questions/spring-mvc-interview-que...

29 of 29 11/3/2017 5:19 PM

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy