use togglz in project
Feature Toggle in Java
In our project, we choose togglz as a framework for feature toggle.
import togglz in project
We need to add these dependencies in project file build.gradle
1
compile 'org.togglz:togglz-core:2.2.0.Final'
compile 'org.togglz:togglz-servlet:2.2.0.Final'
compile 'org.togglz:togglz-jsp:2.2.0.Final' //support jsp
compile 'org.togglz:togglz-console:2.2.0.Final' //support togglz console
compile 'org.togglz:togglz-spring-security:2.2.0.Final' //support user authentication
testCompile 'org.togglz:togglz-testing:2.2.0.Final' //support togglz test
testCompile 'org.togglz:togglz-junit:2.2.0.Final' //support togglz test
config togglz in project
Add a new class ToggledFeature
implements Feature
1 | public enum ToggledFeature implements Feature { |
Add a new class ToggledFeatureConfiguration
implements TogglzConfig
1 | public class ToggledFeatureConfiguration implements TogglzConfig{ |
Add a features.peoperties
file to set togglz status
1 | CONFIRM_PASSWORD=true ADDRESS=true AGREE_TERMS=true EMAIL_CONFIRMATION=true |
Then config togglz in web.xml
1 | <context-param> |
Now we can use <togglz:feature>
in JSP file
First, we need to add togglz tags to jsp file
1
<%@ taglib prefix="togglz" uri="http://togglz.org/taglib"%>
Then we can use togglz to hide elements
1
2
3
4
5<togglz:feature name="AGREE_TERMS">
<div>
</div>
</togglz:feature>
How to use togglz in Unit Test
1 | public class ToggledFeaturesTest { |
How to show toggle console in browser
First, we need to add toggle servlet in
web.xml
1
2
3
4
5
6
7
8<servlet>
<servlet-name>TogglzConsoleServlet</servlet-name>
<servlet-class>org.togglz.console.TogglzConsoleServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TogglzConsoleServlet</servlet-name>
<url-pattern>/togglz/*</url-pattern>
</servlet-mapping>Then login with admin role and open http://localhost:8080/togglz/
- Now we can use this page to toggle feature