We can handle static content for example: js,css,images in Spring MVC by using mvc:resources element.
mvc:resources element is used to point to the location of static content with a specific public URL pattern.
For example define the below line in your application-servlet.xml file. This line will serve all requests for static content coming in with a public URL pattern like /assets/** by searching in the /assets/ directory under the root folder in our application.:
<mvc:resources mapping="/assets/**" location="/assets/" />
Here you can find the complete example:
LoginController.java:
package com.evon;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class LoginController {
@RequestMapping(value="/")
public String login() {
return "login";
}
}
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<servlet>
<servlet-name>demo</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>demo</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Now define the below configuration in your demo-servlet.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="com.evon" />
<context:annotation-config />
<mvc:annotation-driven/>
<!-- the mvc resources tag will do resource management -->
<mvc:resources mapping="/assets/**" location="/assets/" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver" />
</beans>
Now, we can access a CSS file as I'm accessing style.css in the following jsp page:
login.jsp:
<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Flippadoo Form Tester</title>
<link href="/assets/styles/style.css" rel="stylesheet" type="text/css">
</head>
<body>
<div class="wrapper">
<div class="formContainer">
<form name="LoginForm" action="authenticateUser">
<div class="header"></div>
<h2>Flippadoo Login</h2>
<dl>
<dt>userid</dt>
<dd><input type="text"name="j_userName"></dd>
<dt>password</dt>
<dd><input type="password" name="j_userPassword"></dd>
</dl>
<div class="buttonBlock"><input type="submit"name="button1"value="login" class="submitBTN"></div>
</form>
</div>
</div>
</body>
</html>
Hope this will help you :-)
0 Comment(s)