{"id":179,"date":"2017-03-21T09:08:17","date_gmt":"2017-03-21T09:08:17","guid":{"rendered":"http:\/\/codeinsightacademy.com\/blog\/?p=179"},"modified":"2017-11-08T11:58:29","modified_gmt":"2017-11-08T11:58:29","slug":"spring-resources","status":"publish","type":"post","link":"https:\/\/codeinsightacademy.com\/blog\/java\/spring-resources\/","title":{"rendered":"Spring Resources"},"content":{"rendered":"<p><iframe loading=\"lazy\" src=\"https:\/\/www.youtube.com\/embed\/gN_V6nLJIHo\" width=\"560\" height=\"315\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n<p>Step 1. Create resource directory<\/p>\n<p><img src=\"http:\/\/54.251.176.223\/blog\/wp-content\/uploads\/2017\/03\/spring_resource.jpeg\" \/><\/p>\n<hr \/>\n<p>Step 2. Add tags in web.xml<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\r\n&lt;web-app version=\"3.0\" xmlns=\"http:\/\/java.sun.com\/xml\/ns\/javaee\" xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:schemaLocation=\"http:\/\/java.sun.com\/xml\/ns\/javaee http:\/\/java.sun.com\/xml\/ns\/javaee\/web-app_3_0.xsd\"&gt;\r\n    &lt;context-param&gt;\r\n        &lt;param-name&gt;contextConfigLocation&lt;\/param-name&gt;\r\n        &lt;param-value&gt;\/WEB-INF\/applicationContext.xml&lt;\/param-value&gt;\r\n    &lt;\/context-param&gt;\r\n    &lt;listener&gt;\r\n        &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;\/listener-class&gt;\r\n    &lt;\/listener&gt;\r\n    &lt;servlet&gt;\r\n        &lt;servlet-name&gt;dispatcher&lt;\/servlet-name&gt;\r\n        &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;\/servlet-class&gt;\r\n        &lt;load-on-startup&gt;2&lt;\/load-on-startup&gt;\r\n    &lt;\/servlet&gt;\r\n    &lt;servlet-mapping&gt;\r\n        &lt;servlet-name&gt;dispatcher&lt;\/servlet-name&gt;\r\n        &lt;url-pattern&gt;*.htm&lt;\/url-pattern&gt;\r\n        &lt;url-pattern&gt;*.css&lt;\/url-pattern&gt;\r\n        &lt;url-pattern&gt;*.js&lt;\/url-pattern&gt;\r\n    &lt;\/servlet-mapping&gt;\r\n    &lt;session-config&gt;\r\n        &lt;session-timeout&gt;\r\n            30\r\n        &lt;\/session-timeout&gt;\r\n    &lt;\/session-config&gt;\r\n    &lt;welcome-file-list&gt;\r\n        &lt;welcome-file&gt;redirect.jsp&lt;\/welcome-file&gt;\r\n    &lt;\/welcome-file-list&gt;\r\n&lt;\/web-app&gt;<\/pre>\n<p>&nbsp;<\/p>\n<hr \/>\n<p>Step 3. Configure dispatcher-servlet.xml<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;?xml version='1.0' encoding='UTF-8' ?&gt;\r\n&lt;!-- was: &lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt; --&gt;\r\n&lt;beans xmlns=\"http:\/\/www.springframework.org\/schema\/beans\"\r\n       xmlns:mvc=\"http:\/\/www.springframework.org\/schema\/mvc\"\r\n       xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\r\n       xmlns:p=\"http:\/\/www.springframework.org\/schema\/p\"\r\n       xmlns:aop=\"http:\/\/www.springframework.org\/schema\/aop\"\r\n       xmlns:tx=\"http:\/\/www.springframework.org\/schema\/tx\"\r\n       xmlns:context=\"http:\/\/www.springframework.org\/schema\/context\"\r\n       xsi:schemaLocation=\"http:\/\/www.springframework.org\/schema\/beans http:\/\/www.springframework.org\/schema\/beans\/spring-beans-4.0.xsd\r\n       http:\/\/www.springframework.org\/schema\/aop http:\/\/www.springframework.org\/schema\/aop\/spring-aop-4.0.xsd\r\n       http:\/\/www.springframework.org\/schema\/tx http:\/\/www.springframework.org\/schema\/tx\/spring-tx-4.0.xsd\r\n       http:\/\/www.springframework.org\/schema\/context http:\/\/www.springframework.org\/schema\/context\/spring-context-4.0.xsd\r\n       http:\/\/www.springframework.org\/schema\/mvc http:\/\/www.springframework.org\/schema\/mvc\/spring-mvc-4.0.xsd\"&gt;\r\n\r\n    &lt;context:component-scan base-package=\"codeinsightacademy.com.blog.controller\"\/&gt;\r\n\r\n    &lt;!--&lt;mvc:resources mapping=\"\/resources\/**\" location=\"\/resources\/\" cache-period=\"10000\" \/&gt;--&gt;\r\n    &lt;mvc:annotation-driven \/&gt;\r\n    \u00a0\u00a0\u00a0 &lt;mvc:resources mapping=\"\/resources\/**\" location=\"\/resources\/red\/\"\/&gt;\r\n\r\n    &lt;bean id=\"viewResolver\" class=\"org.springframework.web.servlet.view.InternalResourceViewResolver\"\r\n          p:prefix=\"\/WEB-INF\/jsp\/\"\r\n          p:suffix=\".jsp\" \/&gt;\r\n&lt;\/beans&gt;<\/pre>\n<p>&nbsp;<\/p>\n<hr \/>\n<p>Step 4. link or include in jsp page<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;head&gt;\r\n    &lt;meta charset=\"UTF-8\"&gt;\r\n    &lt;title&gt;Welcome to Spring Web MVC project&lt;\/title&gt;\r\n    &lt;link href=\"&lt;c:url value=\"\/resources\/css\/default.css\" \/&gt;\" rel=\"stylesheet\"&gt;\r\n          &lt;link href=\"&lt;c:url value=\"\/resources\/js\/default.css\" \/&gt;\" rel=\"stylesheet\"&gt;\r\n&lt;\/head&gt;<\/pre>\n<p>&nbsp;<\/p>\n<hr \/>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;img width=\"100px\" height=\"50px\" src=\"&lt;c:url value=\"\/resources\/images\/logo.gif\"\/&gt;\"\/&gt;<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Step 1. Create resource directory Step 2. Add tags in web.xml &lt;?xml version=&#8221;1.0&#8243; encoding=&#8221;UTF-8&#8243;?&gt; &lt;web-app version=&#8221;3.0&#8243; xmlns=&#8221;http:\/\/java.sun.com\/xml\/ns\/javaee&#8221; xmlns:xsi=&#8221;http:\/\/www.w3.org\/2001\/XMLSchema-instance&#8221; xsi:schemaLocation=&#8221;http:\/\/java.sun.com\/xml\/ns\/javaee http:\/\/java.sun.com\/xml\/ns\/javaee\/web-app_3_0.xsd&#8221;&gt; &lt;context-param&gt; &lt;param-name&gt;contextConfigLocation&lt;\/param-name&gt; &lt;param-value&gt;\/WEB-INF\/applicationContext.xml&lt;\/param-value&gt; &lt;\/context-param&gt; &lt;listener&gt; &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;\/listener-class&gt; &lt;\/listener&gt; &lt;servlet&gt; &lt;servlet-name&gt;dispatcher&lt;\/servlet-name&gt; &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;\/servlet-class&gt; &lt;load-on-startup&gt;2&lt;\/load-on-startup&gt; &lt;\/servlet&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;dispatcher&lt;\/servlet-name&gt; &lt;url-pattern&gt;*.htm&lt;\/url-pattern&gt; &lt;url-pattern&gt;*.css&lt;\/url-pattern&gt; &lt;url-pattern&gt;*.js&lt;\/url-pattern&gt; &lt;\/servlet-mapping&gt; &lt;session-config&gt; &lt;session-timeout&gt; 30 &lt;\/session-timeout&gt; &lt;\/session-config&gt; &lt;welcome-file-list&gt; &lt;welcome-file&gt;redirect.jsp&lt;\/welcome-file&gt; &lt;\/welcome-file-list&gt; &lt;\/web-app&gt; &nbsp; Step 3. Configure dispatcher-servlet.xml &nbsp; &lt;?xml version=&#8217;1.0&#8242; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[],"_links":{"self":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/179"}],"collection":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/comments?post=179"}],"version-history":[{"count":9,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/179\/revisions"}],"predecessor-version":[{"id":462,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/179\/revisions\/462"}],"wp:attachment":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/media?parent=179"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/categories?post=179"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/tags?post=179"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}