Skip to content

Instantly share code, notes, and snippets.

@somecuitears
Last active May 9, 2017 04:03
Show Gist options
  • Save somecuitears/e2ed62d58dc07142364268fc4f49978a to your computer and use it in GitHub Desktop.
Save somecuitears/e2ed62d58dc07142364268fc4f49978a to your computer and use it in GitHub Desktop.
Spring on Idea IntelliJ Ultimate

Create Basic Spring MVC Project

  1. Create New Project

  2. Select Spring Framework and check the Spring MVC and JavaEE Web application

  3. Find web.xml and change

<url-pattern>*.form</url-pattern>

to

<url-pattern>/</url-pattern>
  1. Then add the following into dispacher-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="
  http://www.springframework.org/schema/beans     
  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  http://www.springframework.org/schema/context 
  http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 <context:component-scan base-package="com.**BASE**" />
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>
  1. Go to Run > Edit Configuration and click on the + sign.

  2. Scroll down to the bottom and add Local Tomcat Server

  3. Provide the name of the server and under Deployment tab click + sign and add Aritfact. An war exploded artifact will be added to the list.

  4. Click on edit represented by a pencil sign.

  5. Select the Problems tab on left and click on fix and select Add xyz to the artifact

  6. Click apply and done.

Add Static Resources to the Project

Adding static resources like js, css, img etc;

  1. Create resources folder inside web or webapp folder. Static resources can be placed inside WEB-INF folder as well but it is not good practice to do so.

  2. Declares mvc:resources, to map “url path” to a physical file path location on dispatcher-servlet.xml

<mvc:resources mapping="/resources/**" location="/resources/theme1/"/>
<mvc:annotation-driven />
  1. include CSS or JS in a JSP page, you can use JSTL tag c:url or Spring tag spring:url
<head>
   <link href="<c:url value="/resources/css/main.css" />" rel="stylesheet">
   <script src="<c:url value="/resources/js/jquery.1.10.2.min.js" />"></script>
   <script src="<c:url value="/resources/js/main.js" />"></script>
</head>

or

 <head>
  <spring:url value="/resources/css/main.css" var="mainCss" />
  <spring:url value="/resources/js/jquery.1.10.2.min.js" var="jqueryJs" />
  <spring:url value="/resources/js/main.js" var="mainJs" />

  <link href="${mainCss}" rel="stylesheet" />
     <script src="${jqueryJs}"></script>
     <script src="${mainJs}"></script>
 </head>

Alternatively page context can be use instead of JSTL

<link href="${pageContext.request.contextPath}/resources/css/main.css" rel="stylesheet" >
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment