Spring MVC Interview Questions With Answers - HowToDoInJava
Spring MVC Interview Questions With Answers - HowToDoInJava
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>
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>
<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" />
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...
</beans>
@Controller
@RequestMapping("/employee-module")
public class EmployeeController
{
@Autowired
EmployeeManager manager;
@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"/>
</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
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...
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) {
if (multipartFile != null) {
fileName = multipartFile.getOriginalFilename();
}
map.addAttribute("files", fileName);
return "file_upload_success";
}
}
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...
Validator
Validator
rejectIfEmptyOrWhitespace( rejectIfEmpty() ValidationUtils
@Component
public class EmployeeValidator implements Validator
{
public boolean supports(Class clazz) {
return EmployeeVO.class.isAssignableFrom(clazz);
}
@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...
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="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</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 {
@Override
public void setServletConfig(final ServletConfig servletConfig) {
this.config = servletConfig;
@Override
public void setServletContext(final ServletContext servletContext) {
this.context = servletContext;
}
//other code
}
JdbcTemplate
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