{"id":1649,"date":"2016-09-19T03:22:34","date_gmt":"2016-09-19T03:22:34","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=1649"},"modified":"2019-04-08T12:51:43","modified_gmt":"2019-04-08T12:51:43","slug":"basic-spring-project","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/basic-spring-project\/","title":{"rendered":"Spring Project Setup &#8211; XML Configuration"},"content":{"rendered":"<p><strong>Creating a Preconfigured MVC Project<\/strong><br \/>\n<strong>web.xml<\/strong><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;\r\n&lt;web-app version=&quot;2.5&quot; xmlns=&quot;http:\/\/java.sun.com\/xml\/ns\/javaee&quot;\r\n\txmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot;\r\n\txsi:schemaLocation=&quot;http:\/\/java.sun.com\/xml\/ns\/javaee http:\/\/java.sun.com\/xml\/ns\/javaee\/web-app_2_5.xsd&quot;&gt;\r\n\r\n\t&lt;!-- The definition of the Root Spring Container shared by all Servlets and Filters --&gt;\r\n\t&lt;context-param&gt;\r\n\t\t&lt;param-name&gt;contextConfigLocation&lt;\/param-name&gt;\r\n\t\t&lt;param-value&gt;\/WEB-INF\/spring\/root-context.xml&lt;\/param-value&gt;\r\n\t&lt;\/context-param&gt;\r\n\t\r\n\t&lt;!-- Creates the Spring Container shared by all Servlets and Filters --&gt;\r\n\t&lt;listener&gt;\r\n\t\t&lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;\/listener-class&gt;\r\n\t&lt;\/listener&gt;\r\n\r\n\t&lt;!-- Processes application requests --&gt;\r\n\t&lt;servlet&gt;\r\n\t\t&lt;servlet-name&gt;appServlet&lt;\/servlet-name&gt;\r\n\t\t&lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;\/servlet-class&gt;\r\n\t\t&lt;init-param&gt;\r\n\t\t\t&lt;param-name&gt;contextConfigLocation&lt;\/param-name&gt;\r\n\t\t\t&lt;param-value&gt;\/WEB-INF\/spring\/appServlet\/servlet-context.xml&lt;\/param-value&gt;\r\n\t\t&lt;\/init-param&gt;\r\n\t\t&lt;load-on-startup&gt;1&lt;\/load-on-startup&gt;\r\n\t&lt;\/servlet&gt;\r\n\t\t\r\n\t&lt;servlet-mapping&gt;\r\n\t\t&lt;servlet-name&gt;appServlet&lt;\/servlet-name&gt;\r\n\t\t&lt;url-pattern&gt;\/&lt;\/url-pattern&gt;\r\n\t&lt;\/servlet-mapping&gt;\r\n\r\n&lt;\/web-app&gt;\r\n<\/pre>\n<ol>\n<li>DispatcherServlet is the first servlet which gets called when you run Preconfigured MVC Project<\/li>\n<li><em>servlet-context.xml<\/em> in init param of DispatcherServlet get run for other initialization of internal beans <\/li>\n<li>Apart from <em>servlet-context.xml<\/em> the other xml file that gets called is <em>root-context.xml<\/em> in a separate context<\/li>\n<\/ol>\n<p><strong>Test.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.mugil.spring;\r\n\r\nimport java.util.Locale;\r\nimport org.springframework.stereotype.Controller;\r\nimport org.springframework.ui.Model;\r\nimport org.springframework.web.bind.annotation.RequestMapping;\r\nimport org.springframework.web.bind.annotation.RequestMethod;\r\n\r\n@Controller\r\npublic class Test\r\n{\t\r\n\t@RequestMapping(value = &quot;\/&quot;, method = RequestMethod.GET)\r\n\tpublic String testMethod(Locale locale, Model model) \r\n\t{\t\r\n\t\tmodel.addAttribute(&quot;testMsg&quot;, &quot;Hi There&quot; );\t\t\r\n\t\treturn &quot;home&quot;;\r\n\t}\r\n}\r\n<\/pre>\n<p><strong>home.jsp<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n&lt;%@ taglib uri=&quot;http:\/\/java.sun.com\/jsp\/jstl\/core&quot; prefix=&quot;c&quot; %&gt;\r\n&lt;%@ page session=&quot;false&quot; %&gt;\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n\t&lt;title&gt;Home&lt;\/title&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n&lt;h1&gt;\r\n\t${testMsg}  \r\n&lt;\/h1&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p><strong>Output(http:\/\/localhost:8089\/spring\/)<\/strong><\/p>\n<pre>\r\nHi There\r\n<\/pre>\n<p><strong>servlet-context.xml<\/strong><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;\r\n&lt;beans:beans xmlns=&quot;http:\/\/www.springframework.org\/schema\/mvc&quot;\r\n\txmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot;\r\n\txmlns:beans=&quot;http:\/\/www.springframework.org\/schema\/beans&quot;\r\n\txmlns:context=&quot;http:\/\/www.springframework.org\/schema\/context&quot;\r\n\txsi:schemaLocation=&quot;http:\/\/www.springframework.org\/schema\/mvc http:\/\/www.springframework.org\/schema\/mvc\/spring-mvc.xsd\r\n\t\thttp:\/\/www.springframework.org\/schema\/beans http:\/\/www.springframework.org\/schema\/beans\/spring-beans.xsd\r\n\t\thttp:\/\/www.springframework.org\/schema\/context http:\/\/www.springframework.org\/schema\/context\/spring-context.xsd&quot;&gt;\r\n\t&lt;annotation-driven \/&gt;\r\n\t&lt;resources mapping=&quot;\/resources\/**&quot; location=&quot;\/resources\/&quot; \/&gt;\r\n\t&lt;beans:bean class=&quot;org.springframework.web.servlet.view.InternalResourceViewResolver&quot;&gt;\r\n\t\t&lt;beans:property name=&quot;prefix&quot; value=&quot;\/WEB-INF\/views\/&quot; \/&gt;\r\n\t\t&lt;beans:property name=&quot;suffix&quot; value=&quot;.jsp&quot; \/&gt;\r\n\t&lt;\/beans:bean&gt;\r\n\r\n\t&lt;context:component-scan base-package=&quot;com.mugil.spring&quot; \/&gt;\r\n&lt;\/beans:beans&gt;\r\n<\/pre>\n<ol>\n<li><strong>context:component-scan base-package<\/strong> defines the base location in which the controller to be found<\/li>\n<li><strong>beans:property name=&#8221;prefix&#8221;<\/strong> defines the folder in which jsp should be found<\/li>\n<li><strong>beans:property name=&#8221;suffix&#8221;<\/strong> defines the valid prefix<\/li>\n<li>Based on the return type of <strong>testMethod<\/strong> method the jsp page name is recognized. In our case it is <strong>home.jsp<\/strong><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Creating a Preconfigured MVC Project web.xml &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt; &lt;web-app version=&quot;2.5&quot; xmlns=&quot;http:\/\/java.sun.com\/xml\/ns\/javaee&quot; xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot; xsi:schemaLocation=&quot;http:\/\/java.sun.com\/xml\/ns\/javaee http:\/\/java.sun.com\/xml\/ns\/javaee\/web-app_2_5.xsd&quot;&gt; &lt;!&#8211; The definition of the Root Spring Container shared by all Servlets and Filters &#8211;&gt; &lt;context-param&gt; &lt;param-name&gt;contextConfigLocation&lt;\/param-name&gt; &lt;param-value&gt;\/WEB-INF\/spring\/root-context.xml&lt;\/param-value&gt; &lt;\/context-param&gt; &lt;!&#8211; Creates the Spring Container shared by all Servlets and Filters &#8211;&gt; &lt;listener&gt; &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;\/listener-class&gt; &lt;\/listener&gt; &lt;!&#8211; Processes application requests &#8211;&gt; &lt;servlet&gt;&hellip; <a href=\"https:\/\/codethataint.com\/blog\/basic-spring-project\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[273],"tags":[],"class_list":["post-1649","post","type-post","status-publish","format-standard","hentry","category-spring-mvc"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1649","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=1649"}],"version-history":[{"count":5,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1649\/revisions"}],"predecessor-version":[{"id":1987,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1649\/revisions\/1987"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=1649"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=1649"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=1649"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}