Skip to content
Snippets Groups Projects
Commit 1a6572d1 authored by Antoine Rey's avatar Antoine Rey
Browse files

Replace web.xml by PetclinicInitializer

parent 835f53da
No related branches found
No related tags found
No related merge requests found
......@@ -96,7 +96,7 @@ File -> Import -> Maven -> Existing Maven project
<td>
<a href="/src/main/webapp/WEB-INF/jsp/owners/ownersList.jsp">ownersList.jsp</a>
<a href="/src/main/webapp/WEB-INF/jsp/vets/vetList.jsp">vetList.jsp</a>
<a href="/src/main/webapp/WEB-INF/web.xml">web.xml</a>
<a href="/src/main/java/org/springframework/samples/petclinic/PetclinicInitializer.java">PetclinicInitializer.java</a>
<a href="/src/main/resources/dandelion/datatables/datatables.properties">datatables.properties</a>
</td>
</tr>
......@@ -135,7 +135,7 @@ File -> Import -> Maven -> Existing Maven project
<td>
<a href="/src/main/resources/spring/business-config.xml">business-config.xml</a>
<a href="/src/test/java/org/springframework/samples/petclinic/service/ClinicServiceJdbcTests.java">ClinicServiceJdbcTests.java</a>
<a href="/src/main/webapp/WEB-INF/web.xml">web.xml</a>
<a href="/src/main/java/org/springframework/samples/petclinic/PetclinicInitializer.java">PetclinicInitializer.java</a>
</td>
</tr>
<tr>
......
/*
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.samples.petclinic;
import com.github.dandelion.core.web.DandelionFilter;
import com.github.dandelion.core.web.DandelionServlet;
import com.github.dandelion.datatables.core.web.filter.DatatablesFilter;
import org.springframework.util.Assert;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.XmlWebApplicationContext;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer;
import javax.servlet.*;
import java.util.EnumSet;
/**
* In Servlet 3.0+ environments, this class replaces the traditional {@code web.xml}-based approach in order to configure the
* {@link ServletContext} programmatically.
* <p/>
* Create the Spring "<strong>root</strong>" application context.<br/>
* Register a {@link DispatcherServlet} and a {@link DandelionServlet} in the servlet context.<br/>
* For both servlets, register a {@link CharacterEncodingFilter}, a {@link DandelionFilter} an a {@link DatatablesFilter}.
* <p/>
*
* @author Antoine Rey
*/
public class PetclinicInitializer extends AbstractDispatcherServletInitializer {
/**
* Spring profile used to choose the persistence layer implementation.
* <p/>
* When using Spring jpa, use: jpa
* When using Spring JDBC, use: jdbc
* When using Spring Data JPA, use: spring-data-jpa
*/
private static final String SPRING_PROFILE = "jpa";
private static final String DANDELION_SERVLET = "dandelionServlet";
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
super.onStartup(servletContext);
registerDandelionServlet(servletContext);
}
@Override
protected WebApplicationContext createRootApplicationContext() {
XmlWebApplicationContext rootAppContext = new XmlWebApplicationContext();
rootAppContext.setConfigLocations("classpath:spring/business-config.xml", "classpath:spring/tools-config.xml");
rootAppContext.getEnvironment().setActiveProfiles(SPRING_PROFILE);
return rootAppContext;
}
@Override
protected WebApplicationContext createServletApplicationContext() {
XmlWebApplicationContext webAppContext = new XmlWebApplicationContext();
webAppContext.setConfigLocation("classpath:spring/mvc-core-config.xml");
return webAppContext;
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
@Override
protected Filter[] getServletFilters() {
// Used to provide the ability to enter Chinese characters inside the Owner Form
CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter("UTF-8", true);
// Dandelion filter definition and mapping -->
DandelionFilter dandelionFilter = new DandelionFilter();
// Dandelion-Datatables filter, used for basic export -->
DatatablesFilter datatablesFilter = new DatatablesFilter();
return new Filter[]{characterEncodingFilter, dandelionFilter, datatablesFilter};
}
@Override
protected FilterRegistration.Dynamic registerServletFilter(ServletContext servletContext, Filter filter) {
FilterRegistration.Dynamic registration = super.registerServletFilter(servletContext, filter);
registration.addMappingForServletNames(EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.INCLUDE), false, DANDELION_SERVLET);
return registration;
}
private void registerDandelionServlet(ServletContext servletContext) {
DandelionServlet dandelionServlet = new DandelionServlet();
ServletRegistration.Dynamic registration = servletContext.addServlet(DANDELION_SERVLET, dandelionServlet);
registration.setLoadOnStartup(2);
registration.addMapping("/dandelion-assets/*");
}
}
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0" metadata-complete="true">
<display-name>Spring PetClinic</display-name>
<description>Spring PetClinic sample application</description>
<!-- When using Spring jpa, use the following: -->
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>jpa</param-value>
</context-param>
<!-- When using Spring JDBC, use the following: -->
<!-- <context-param>
<param-name>spring.profiles.active</param-name>
<param-value>jdbc</param-value>
</context-param> -->
<!-- the CallMonitoringAspect counts invocations on classes with @Repository on them. Classes in spring-data-jpa don't have that annotation -->
<!-- When using Spring Data JPA, uncomment the following: -->
<!--
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>spring-data-jpa</param-value>
</context-param>
-->
<!--
- Location of the XML file that defines the root application context.
- Applied by ContextLoaderListener.
-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/business-config.xml, classpath:spring/tools-config.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--
- Servlet that dispatches request to registered handlers (Controller implementations).
-->
<servlet>
<servlet-name>petclinic</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/mvc-core-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>petclinic</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- Dandelion servlet definition and mapping -->
<servlet>
<servlet-name>dandelionServlet</servlet-name>
<servlet-class>com.github.dandelion.core.web.DandelionServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dandelionServlet</servlet-name>
<url-pattern>/dandelion-assets/*</url-pattern>
</servlet-mapping>
<!-- used to provide the ability to enter Chinese characters inside the Owner Form -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Dandelion filter definition and mapping -->
<filter>
<filter-name>dandelionFilter</filter-name>
<filter-class>com.github.dandelion.core.web.DandelionFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>dandelionFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Dandelion-Datatables filter, used for basic export -->
<filter>
<filter-name>datatables</filter-name>
<filter-class>com.github.dandelion.datatables.core.web.filter.DatatablesFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>datatables</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- No need for welcome-file declaration here.
See inside spring/mvc-core-config.xml :
<mvc:view-controller path="/" view-name="welcome" />
-->
</web-app>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment