-
Create New Project
-
Select Spring Framework and check the Spring MVC and JavaEE Web application
-
Find web.xml and change
<url-pattern>*.form</url-pattern>
to
<url-pattern>/</url-pattern>
- 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>
-
Go to Run > Edit Configuration and click on the + sign.
-
Scroll down to the bottom and add Local Tomcat Server
-
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.
-
Click on edit represented by a pencil sign.
-
Select the Problems tab on left and click on fix and select Add xyz to the artifact
-
Click apply and done.
Adding static resources like js, css, img etc;
-
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.
-
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 />
- 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>
<link href="${pageContext.request.contextPath}/resources/css/main.css" rel="stylesheet" >