Latest Added Tutorials
28-05-2016
Aspect-Oriented Programming (AOP) complements Object-Oriented Programming (OOP) by providing another way of thinking about program structure. The key unit of modularity in OOP is the class, whereas in AOP the unit of modularity is the aspect. Aspects enable the modularization of concerns such as transaction management that cut across multiple types and objects. (Such concerns are often termed crosscutting concerns in AOP literature.)
Lets start how to configure load time weaving in Spring framework.
applicationContext.xml Configuration
<bean id=...Continue Reading
Spring Configuration
<http auto-config="false" use-expressions="true"
entry-point-ref="loginUrlAuthenticationEntryPoint">
<intercept-url pattern="/index" access="permitAll"/>
<intercept-url pattern="/kayit" access="permitAll"/>
<intercept-url pattern="/sifre-hatirlatma" access="permitAll"/>
<intercept-url pattern="/checkUser" access="permitAll"/>
<intercept-url pattern="favicon.ico" access="permitAll"/>
<intercept-url pattern="/kullanici/**" access="hasAnyAuthority('ROLE_ADMIN','ROLE_OFFICER','ROLE_CLIE...Continue Reading
Create Password
After downloaded Jasypt CLI Tools, execute following code by using encrypt.sh for linux based OS, or encrypt.bat file for Windows located in bin folder:
encrypt.bat input="secret" password=encryptorpassword algorithm=PBEWithMD5AndTripleDES
Output looks like this: AdK2HjMDfxTABg9ZP3kXSWsKo3t4rSn7
Note: Whenever run above command in command prompt, you will get different password each time because PBEWithMD5AndTripleDES algorithm and many other algorithms use random salt generator. For more information please click
Add Maven Depe...Continue Reading
Using Java Reflection, we can copy fields from one object to another object by using below class:
public class FieldCopyUtil {
public static void setFields(Object from, Object to) {
Field[] fields = from.getClass().getDeclaredFields();
for (Field field : fields) {
try {
Field fieldFrom = from.getClass().getDeclaredField(field.getName());
Object value = fieldFrom.get(from);
to.getClass().getDeclaredField(field.getName()).set(to, value);
} catch (IllegalAccessExc...Continue Reading
Internationalization (i18n) or localization (L10n) is used to change language of a web application for better interaction. In this article, I will try to configure Spring MVC web application to use the Internationalization concept.
Spring MVC Configuration Steps
1. mvc-dispatcher-servlet.xml file
<mvc:interceptors>
<!-- Changes the locale when a 'lang' request parameter is sent; e.g. /?lang=tr -->
<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="para...Continue Reading
If we want to save or update only association table, we shouldn't use cascade in @JoinTable annotation.
UserGroup Table
@Entity
public class UserGroup {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int userGroupId;
@Size(min = 4, max = 20, groups = SizeConstraintGroup.class)
@Column(length = 20)
private String name;
@ManyToMany(fetch = FetchType.LAZY)
@JsonIgnore
@JoinTable(
name = "UserGroup_User",
joinColumns = @JoinColumn(name = "userGroupId", nullable = false, updatable...Continue Reading
<!-- enable use-expressions -->
<http auto-config="true" use-expressions="true">
<!-- src** matches: src/bar.c src/baz.c src/test/bartest.c-->
<intercept-url pattern="/problemSolution/home/**" access="hasRole('ROLE_ADMIN')"/>
<intercept-url pattern="favicon.ico" access="permitAll"/>
<form-login
authentication-success-handler-ref="authenticationSuccessHandler"...Continue Reading
In Hibernate ManyToMany relationship, save or saveOrUpdate operations must be as follows:
User Entity
@ManyToMany(fetch = FetchType.LAZY,cascade = CascadeType.ALL)
@JoinTable(
name = "UserRole_User",
joinColumns = @JoinColumn(name = "userId", nullable = false, updatable = false),
inverseJoinColumns = @JoinColumn(name = "userRoleId", nullable = false, updatable = false)
)
private List userRoleList=new ArrayList();
In Line 1, cascade provides that when user is saved, an insert operation will be executed on UserRol...Continue Reading
20-02-2015
Custom Logout and Logout Success URL:
<logout logout-url="/logout"
logout-success-url="/problemSolution/index?logout"/>
Note: /logout url is used by Spring Security. This url doesn't refer to any .jsp pages, so you can set any value.
index.jsp page:
<c:url var="logoutUrl" value="/problemSolution/logout"/>
<form action="${logoutUrl}" id="logout" method="post">
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
</form>
<a href="#" onclick="document.getElementById('logout').submit();">L...Continue Reading
OneToMany Unidirectional Mapping
Question:
In this document (scroll down to the Unidirectional section): http://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/#entity-mapping-association-collections
It says that a unidirectional one-to-many association with a join table is much preferred to just using a foreign key column in the owned entity. My question is, why is it much preferred?
Answer 1:
Consider the situation where the owned entity type can also be owned by another parent entity type. Do you put foreign key refere...Continue Reading
As an alternative to the Ehcache Monitor, JMX creates a standard way of instrumenting classes and making them available to a management and monitoring infrastructure.
Overview
JMX creates a standard way of instrumenting classes and making them available to a management and monitoring infrastructure. The net.sf.ehcache.management package contains MBeans and a ManagementService for JMX management of ehcache. It is in a separate package so that JMX libraries are only required if you wish to use it - there is no leakage of JMX dependencies into the core E...Devamını Oku
Using Spring Framework we can see Hibernate statistics such as second level hit count via JVM. JMX is good way to expose parts of your application for monitoring and management.
In order to use Hibernate statistics in JMX, we sould add following dependencies:
org.hibernate
hibernate-jmx
3.5.6-Final
org.hibernate
hibernateContinue Reading
To use EHCache library with Hibernate 4 we should add following dependencies:
org.hibernate
hibernate-ehcache
4.3.8.Final
net.sf.ehcache
ehcache-core
2.6.10
In Spring we can configure hibernate + ehcache as follows:
Problem: org.hibernate.HibernateException: createQuery is not valid without active transaction
Solution: If you are using <prop key="hibernate.current_session_context_class">thread</prop>, remove this. Because:
When using spring and spring managed transactions never mess around with the hibernate.current_session_context_class property UNLESS you are using JTA.
Spring will by default set its own CurrentSessionContext implementation (the SpringSessionContext), however if you set it yourself this will not be the case. Basically breaking proper tr...Continue Reading
31-01-2015
File Uploading is a very common in any web application. In this tutorial, we will introduce three different file upload operations: single file upload, multipe file upload, and ajax based file upload.
First of all, there are some pre-request steps needed:
1. Create a Maven project
2. Add Spring Framework dependencies
3. Add Javax Servlet and JSTL dependencies
3. Add Apache Commons dependencies
After initialization web application, directory structure of the application will look below image:
Necessary Dependencies
As stated above, we need...Continue Reading