Skip to content

Instantly share code, notes, and snippets.

@banterCZ
banterCZ / SampleTest.java
Created May 4, 2012 13:55
JUnit test interceptor using TestRule (TestWatcher and RuleChain)
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExternalResource;
import org.junit.rules.RuleChain;
import org.junit.rules.TestRule;
import org.junit.rules.TestWatcher;
@banterCZ
banterCZ / context:property-placeholder.xml
Created May 21, 2012 22:30
How to extract configuration from a war file.
<context:property-placeholder location="${myApp.config.location:classpath}myApp.properties" system-properties-mode="OVERRIDE"/>
@banterCZ
banterCZ / RCValidator.java
Created October 30, 2012 08:36
Validator of national identification number used in the Czech Republic.
import static java.util.Calendar.DAY_OF_MONTH;
import static java.util.Calendar.MONTH;
import static java.util.Calendar.YEAR;
import java.util.Calendar;
/**
*
* Validator of national identification number used in the Czech Republic.
*
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Minification sample</title>
<c:choose>
<c:when test="${environment == 'development'}">
<link rel="stylesheet" href="<c:url value="/gui/css/first.css" />" />
<link rel="stylesheet" href="<c:url value="/gui/css/second.css" />" />
@banterCZ
banterCZ / UserController.java
Created February 4, 2013 07:56
Spring REST and e-mail as a parameter
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class UserController {
@RequestMapping(value = "/users/{email:.+}", method = RequestMethod.GET)
@banterCZ
banterCZ / ViewScope.java
Created February 7, 2013 07:22
Classes needed for JSF ManagedBeans of ViewScope managed by Spring.
import javax.faces.context.FacesContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.config.Scope;
import org.springframework.web.context.request.FacesRequestAttributes;
import java.util.Map;
@banterCZ
banterCZ / FileNotFoundFilter.java
Created February 7, 2013 11:42
When you ask for url matching JSF servlet mapping, you get the FileNotFoundException and http code 500. This filter fixes it.
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
* This filter interprets {@link FileNotFoundException} as HTTP status code <code>404</code>.
*
* @author banterCZ
@banterCZ
banterCZ / MyController.java
Created February 11, 2013 12:45
Fix of java.lang.IndexOutOfBoundsException using Spring MVC.
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
/**
* @author banterCZ
*/
@Controller
public class MyController {
@banterCZ
banterCZ / JsfRedirectStrategy.java
Last active May 15, 2021 15:26
InvalidSessionStrategy when session expired and ajax request is done.
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.web.session.InvalidSessionStrategy;
import org.springframework.util.StringUtils;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@banterCZ
banterCZ / jsfDynamicInclude.xhtml
Created April 9, 2013 11:27
Using JSF, there is no nice way how to iterate over collection an include a template.
<ui:composition template="/WEB-INF/templates/layout.xhtml"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:c="http://java.sun.com/jstl/core">
<ui:define name="content">
<!-- does NOT work -->
<!-- JSTL tags are taghandlers and they are executed during view build time, while JSF UI components are executed during view render time -->