0% found this document useful (0 votes)
2 views25 pages

Spring4 Spring MVC

The document discusses the concept of 'convention over configuration' in Spring Web MVC, highlighting how established naming conventions can reduce configuration needs for handler mappings, view resolvers, and ModelAndView instances. It emphasizes the benefits for rapid prototyping and maintaining consistency in codebases. Additionally, it provides examples of configuration files and controller classes to illustrate the implementation of this approach.

Uploaded by

Suresh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
2 views25 pages

Spring4 Spring MVC

The document discusses the concept of 'convention over configuration' in Spring Web MVC, highlighting how established naming conventions can reduce configuration needs for handler mappings, view resolvers, and ModelAndView instances. It emphasizes the benefits for rapid prototyping and maintaining consistency in codebases. Additionally, it provides examples of configuration files and controller classes to illustrate the implementation of this approach.

Uploaded by

Suresh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 25

Convention over configuration

For a lot of projects, sticking to established conventions and having reasonable


defaults is just what they (the projects) need... this theme of convention-over-
configuration now has explicit support in Spring Web MVC.

What this means is that if you establish a set of naming conventions and suchlike,
you can substantially cut down on the amount of configuration that is required to
set up handler mappings, view resolvers, ModelAndView instances, etc.

This is a great boon with regards to rapid prototyping, and can also lend a degree of
(always good-to-have) consistency across a codebase should you choose to move
forward with it into production.

This convention over configuration support address the three core areas of MVC -
namely, the models, views, and controllers.
Convention
Configuration: Method 1
Configuration: Method 2
…….

<servlet>
<servlet-name>SpringController</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>SpringController</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
……..
mvc-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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
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.lnt"/>

<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
Note: The spring configuration file has to be placed
</beans> under WEB-INF folder
The model presents a placeholder to hold the information you want to display on the
view
If you are using the old JSP 1.2
descriptor, defined by DTD ,for example
web.xml , The EL is disabled or ignored
by default, you have to enable it manually,
so that it will outputs the value store in the
“msg” model.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"


pageEncoding="ISO-8859-1" isELIgnored="false"%>
WebAppDemo

http://localhost:8080/WebAppDemo/hello2
web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >

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

<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

</web-app>
web.xml
JSP 1.2

If you are using the old JSP 1.2 descriptor, defined by DTD ,for example
web.xml

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application


2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app> //...
</web-app>

The EL is disabled or ignored by default, you have to enable it manually, so that it will
outputs the value store in the “message” model.

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


<html>
<head> <%@ page isELIgnored="false" %> </head>
<body> ${message} </body> <
/html>
web.xml
JSP 2.0

If you are using the standard JSP 2.0 descriptor, defined by w3c schema ,for
example

web.xml
<web-app id="WebApp_ID" version="2.4" 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"> /
/...
</web-app>

The EL is enabled by default in standard JSP 2.0


HelloWorld.java : Controller class
@RequestMapping(value="/hello1", method=RequestMethod.GET)
public String sayHello(Model model){
String message="Hello World! Welcome to Spring Framework";
model.addAttribute("message",message);
return "helloworld";
}
………

@RequestParam annotation which indicates that a method parameter should be


bound to a web request parameter.

Optional Elements
Modifier and Type Optional Element and Description
String defaultValue The default value to use as a fallback when the
request parameter value is not provided or empty.
boolean Required Whether the parameter is required.
String Value The name of the request parameter to bind to.
HelloWorld.java : Controller
@RequestMapping( value="/hello2", method=RequestMethod.GET)
public ModelAndView sayHello(@RequestParam(value = "name", required = false,
defaultValue = "World") String name){
String message="Hello World! Welcome to Spring Framework";
ModelAndView modelAndView= new ModelAndView();
modelAndView.setViewName("hello");
modelAndView.addObject("message", message);
modelAndView.addObject("name", name); This method returns ModelAndView object
return modelAndView;
http://localhost:8080/WebAppDemo/hello2?name=Srini
}
This class is a holder for both Model and View in the web MVC framework.

Note that Model and View are entirely distinct. This class merely holds both to make it
possible for a controller to return both model and view in a single return value.
@RequestMapping(value="/hello1", method=RequestMethod.GET)
public String sayHello(Model model){
String message="Hello World! Welcome to Spring Framework";
model.addAttribute("message",message);
return "helloworld"; This method returns String object which is name of
} the jsp file
ModelAndView class
@RequestMapping(method=RequestMethod.GET) Attribute Value
public ModelAndView printHello(){
String message="Hello World! Welcome to Spring MVC";
return new ModelAndView("hello","message",message);
}
}
View name Attribute name
<?xml version="1.0" encoding="UTF-8"?> mvc-dispatcher-servlet.xml
<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.deloitte"/>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
helloworld.jsp and hello.jsp
<%@ page language="java"
<%@ page language="java"
contentType="text/html; charset=ISO-
contentType="text/html; charset=ISO-
8859-1"
8859-1" pageEncoding="ISO-8859-1"
pageEncoding="ISO-8859-1"
isELIgnored="false"%>
isELIgnored="false"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD
<!DOCTYPE html PUBLIC "-//W3C//DTD
HTML 4.01 Transitional//EN"
HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<html>
<head>
<head>
<meta http-equiv="Content-Type"
<meta http-equiv="Content-Type"
content="text/html; charset=ISO-8859-1">
content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<title>Insert title here</title>
</head>
</head>
<body>
<body>
<h2>
<h2> ${message}</h2>
${message}<br>
</body>
${name}
</html>
</h2>
</body>
</html>
Thank You!

You might also like