Archive

Author Archive

Equals Method for Objects in Java

March 24, 2018 Leave a comment

This post is about how to compare object based on their fields and return true if they’re equal. To achieve this, we must override equals AND hashCode method.

Below the example.


public class Book {

private int id;
private String title;
private String author;

public Book(){
}

public Book(int id, String title, String author){
this.id = id;
this.title = title;
this.author = author;
}

public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}

@Override
public boolean equals(Object obj) {
if (obj == null)
return false;

if (!(obj instanceof Book))
return false;

if (obj == this)
return true;

if (this.getTitle().equals(((Book) obj).getTitle()) && this.getTitle().equals(((Book) obj).getTitle())) {
return true;
} else {
return false;
}
}

@Override
public int hashCode() {
final int prime = 41;
int result = 1;
result = prime * result + ((this.getAuthor() == null) ? 0 : this.getAuthor().hashCode());
result = prime * result + ((this.getTitle() == null) ? 0 : this.getTitle().hashCode());
return result;
}
}

in piece code above, object is considered equal if field author and title have same value.

public static void main( String[] args )
{

Book firstBook = new Book(1, "Supernova", "Dee Lestari");
Book secondBook = new Book(2, "Supernova", "Dee Lestari");
Book thirdBook = new Book(3, "Aroma Karsa", "Dee Lestari");

List<Book> books = new ArrayList<Book>();
books.add(firstBook);

System.out.println("Is First equals Second book: "+firstBook.equals(secondBook));
System.out.println("Is First equals Third book: "+firstBook.equals(thirdBook));
System.out.println("Is Books List Containts Second book: "+books.contains(secondBook));
System.out.println("Is Books List Containts third book: "+books.contains(thirdBook));}

Result :

Equals Override Result

 

Overriding hashCode method.

The purpose overriding this method is if work with Java Hashed Collection, object that has same field value ( in this case title and author field) considered equal.

Let’s say, we don’t override hashCode method.

 
public static void main( String[] args ) {

Book firstBook = new Book(1, "Supernova", "Dee Lestari");
Book secondBook = new Book(2, "Supernova", "Dee Lestari");
Book thirdBook = new Book(3, "Aroma Karsa", "Dee Lestari");

HashSet <Book> hashBooks = new HashSet <Book> ();
hashBooks.add(firstBook);
hashBooks.add(secondBook);
hashBooks.add(thirdBook);
System.out.println("HashSet size: " + hashBooks.size());
}

Result:

HashCode Method Is Not Overrided

As you see, hashBooks variable containts 3 element, HashSet consider them as different object. We expect 2 element since object firstBook and secondBook has same field value.

Conclusion

  • It is mandatory to override hashcode()¬†each time we override equals()
  • If two objects are equal, they MUST have the same hash code

Thank you. ūüėÄ

Advertisements

Customize 403 Access Denied Spring Security

February 23, 2018 1 comment

In this post, I will show you how to add customize 403 access denied page in spring security. I use project in another post as basis.

1. Add 403 page

<html>
<body>
 <h1>HTTP Status 403 - Access is denied</h1>
 <h2>${msg}</h2>
</body>
</html>

2.Add a mapping for ‚Äú/403‚ÄĚ url in controller

 @RequestMapping(value = "/403", method = RequestMethod.GET)
 public ModelAndView aaccessDenied(Principal user) {
 mv = new ModelAndView("403");
 if (user != null) {
 mv.addObject("msg", "Hi " + user.getName()
 + ", you do not have permission to access this page!");
 } else {
 mv.addObject("msg",
 "You do not have permission to access this page!");
 }
 return mv;
 }

3.Add custom class to handle 403
In this class we can perform some logic before we redirect request to 403 page

</pre>
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;

public class CustomAccessDeniedHandler implements AccessDeniedHandler {

private String accessDeniedPage= "403";
@Override
public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException exception)
throws IOException, ServletException {
System.out.println("Logic in here");
response.sendRedirect(accessDeniedPage);
}
}

4. Edit Spring Security config xml

	<http auto-config="true">
	<access-denied-handler ref="customAccessDeniedHandler" />
         ...
	</http>

        <beans:bean id="customAccessDeniedHandler"  class="org.mvc.security.config.CustomAccessDeniedHandler" />

5. Screen shoot

403

Spring Security Form Login With Database

January 28, 2018 1 comment

In this post, we will show you how to perform authentication using Spring Security backed by database.

Technology stack

  • Spring 4.2.5.RELEASE
  • Spring Security 4.2.3.RELEASE
  • Maven
  • Hibernate 5.2.5.Final
  • MySQL Database
  • Eclipse

Project structure

Project Structure

Below details step.

  • User entity
</pre>

package org.mvc.security.entity;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

/**
* The Class User.
*/
@Entity
@Table(name = "users")
public class User {

/** The id. */
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;

/** The username. */
@Column(name = "username")
private String username;

/** The password. */
@Column(name = "password")
private String password;

/** The password confirm. */
@Transient
private String passwordConfirm;

/** The roles. */
@ManyToMany(cascade = CascadeType.REMOVE, fetch =FetchType.EAGER)
@JoinTable(name = "users_roles", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
private List<Role> roles;

/**
* Gets the id.
*
* @return the id
*/
public Long getId() {
return id;
}

/**
* Sets the id.
*
* @param id the new id
*/
public void setId(Long id) {
this.id = id;
}

/**
* Gets the username.
*
* @return the username
*/
public String getUsername() {
return username;
}

/**
* Sets the username.
*
* @param username the new username
*/
public void setUsername(String username) {
this.username = username;
}

/**
* Gets the password.
*
* @return the password
*/
public String getPassword() {
return password;
}

/**
* Sets the password.
*
* @param password the new password
*/
public void setPassword(String password) {
this.password = password;
}

/**
* Gets the roles.
*
* @return the roles
*/
public List<Role> getRoles() {
return roles;
}

/**
* Sets the roles.
*
* @param roles the new roles
*/
public void setRoles(List<Role> roles) {
this.roles = roles;
}

/**
* Gets the password confirm.
*
* @return the password confirm
*/
public String getPasswordConfirm() {
return passwordConfirm;
}

/**
* Sets the password confirm.
*
* @param passwordConfirm the new password confirm
*/
public void setPasswordConfirm(String passwordConfirm) {
this.passwordConfirm = passwordConfirm;
}

}
  • Role entity

package org.mvc.security.entity;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

/**
* The Class Role.
*/
@Entity
@Table(name = "roles")
public class Role {

/** The id. */
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

/** The name. */
@Column(name = "name")
private String name;

/** The users. */
@ManyToMany(mappedBy = "roles", fetch = FetchType.EAGER)
private List<User> users;

/**
* Gets the id.
*
* @return the id
*/
public Long getId() {
return id;
}

/**
* Sets the id.
*
* @param id the new id
*/
public void setId(Long id) {
this.id = id;
}

/**
* Gets the name.
*
* @return the name
*/
public String getName() {
return name;
}

/**
* Sets the name.
*
* @param name the new name
*/
public void setName(String name) {
this.name = name;
}

/**
* Gets the users.
*
* @return the users
*/
public List<User> getUsers() {
return users;
}

/**
* Sets the users.
*
* @param users the new users
*/
public void setUsers(List<User> users) {
this.users = users;
}

}

  • Spring context

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">

<context:annotation-config/>
<bean id="springDatasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url" value="${jdbc.url}" />
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="springDatasource" />
<property name="packagesToScan" value="org.mvc.security.entity" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<prop key="hibernate.connection.autocommit">${hibernate.connection.autocommit}</prop>
</props>
</property>
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />

<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>

* Spring security xml configuration

</pre>

<beans:beans xmlns="http://www.springframework.org/schema/security" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-4.2.xsd">

<http auto-config="true">
<csrf disabled="true"/>
<intercept-url pattern="/" access="permitAll" />
<intercept-url pattern="/login*" access="isAnonymous()" />
<intercept-url pattern="/user/**" access="hasAuthority('USER')" />
<intercept-url pattern="/admin/**" access="hasAuthority('ADMIN')" />

<form-login login-page="/login" authentication-success-handler-ref="customSuccessHandler" authentication-failure-url="/login?error=true" username-parameter="username" password-parameter="password" />
<logout logout-success-url="/login?logout=true" />
</http>

<beans:bean id="customSuccessHandler" class="org.mvc.security.config.CustomSuccessHandler" />

<authentication-manager alias="authenticationManager">
<authentication-provider user-service-ref="userDetailsService">
</authentication-provider>
</authentication-manager>

</beans:beans>

* Create database with hibernate name and edit user and password for MySql database at application.properties file respectively. Table user and role will be created automatically

* Run this sql script


insert into roles (name) values ('USER');
insert into roles (name) values ('ADMIN');

insert into users (password, username) values ('12345', 'User');
insert into users (password, username) values ('12345', 'Admin');

insert into users_roles (user_id, role_id) values (1,1);
insert into users_roles (user_id, role_id) values (2,2);

* Add security filter in web.xml file

 <filter>
 <filter-name>springSecurityFilterChain</filter-name>
 <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
 </filter>
 <filter-mapping>
 <filter-name>springSecurityFilterChain</filter-name>
 <url-pattern>/*</url-pattern>
</filter-mapping

* Login form

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<div class="container">
<form class="form-horizontal" method="post" action="login" >
 <h2>Login Form</h2>
 <div class="form-group">
 <label class="control-label col-sm-2" for="username">Username</label>
 <div class="control-label col-sm-8">
 <input type="text" class="form-control" id="username" name="username" placeholder="Type Username"/>
 </div>
 </div>
 
 <div class="form-group">
 <label class="control-label col-sm-2" for="password">Password</label>
 <div class="control-label col-sm-8">
 <input type="password" class="form-control" id="password" name="password" placeholder="Type Password"/>
 </div>
 </div>
 <span>${error}</span>
 <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
 <button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>

* Screen shoot

Login Form

 

You can get complete code in here

Hope it will help.

Thank you.

How To Find Longest Palindrome In String

July 29, 2017 Leave a comment

Below is code to find longest palindrome in string.


public class Palindrome {

public String getLongestPalindrome(String words) {
String longest = words.substring(0, 1);
for (int i = 0; i < words.length(); i++) {
String temp = helper(words, i, i);
if (longest.length() < temp.length()) {
longest = temp;
}

temp = helper(words, i, i + 1);
if (longest.length() < temp.length()) {
longest = temp;
}
}
return longest;
}

private String helper(String words,int left, int right) {
while (left >= 0 && right < words.length()&& words.charAt(left) == words.charAt(right)) {
left--;
right++;
}
return words.substring(left + 1, right);
}
}

ūüôā

Terekam Tak Pernah Mati

June 30, 2017 Leave a comment

Photo Idul Fitri 1438 H,

 

 

 

 

 

 

 

 

Alhamdulillah. ūüôā

Struts2 CRUD Tutorial

May 8, 2017 Leave a comment

In this tutorial I will show you how to perform create, retrieve, update and delete operation using struts2 framework. In this tutorial I have used Spring as DI, Hibernate as ORM and Maven as build tool also. Below are tutorial screen shoot,

Add View

 

Update View

 

 

Lets get started,

Structure Directory Project

  • Entity class (Student.java)
package strutshelloworld.net.ren.struts2.domain;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

// TODO: Auto-generated Javadoc
/**
 * The Class Student.
 */
@Entity
@Table(name="student")
public class Student {
    
    /** The id. */
	@Id
	@GeneratedValue
	@Column(name="id")
    private int id;
    
    /** The first name. */
	@Column(name="first_name")
    private String firstName;
    
    /** The last name. */
	@Column(name="last_name")
    private String lastName;
    
    /**
     * Instantiates a new student.
     */
    public Student() {
	
    }
    
    /**
     * Instantiates a new student.
     *
     * @param id the id
     * @param firstName the first name
     * @param lastName the last name
     */
    public Student(int id, String firstName, String lastName) {
	super();
	this.id = id;
	this.firstName = firstName;
	this.lastName = lastName;
    }
    
    /**
     * Gets the id.
     *
     * @return the id
     */
    public int getId() {
	return id;
    }
    
    /**
     * Sets the id.
     *
     * @param id the new id
     */
    public void setId(int id) {
	this.id = id;
    }
    
    /**
     * Gets the first name.
     *
     * @return the first name
     */
    public String getFirstName() {
	return firstName;
    }
    
    /**
     * Sets the first name.
     *
     * @param firstName the new first name
     */
    public void setFirstName(String firstName) {
	this.firstName = firstName;
    }
    
    /**
     * Gets the last name.
     *
     * @return the last name
     */
    public String getLastName() {
	return lastName;
    }
    
    /**
     * Sets the last name.
     *
     * @param lastName the new last name
     */
    public void setLastName(String lastName) {
	this.lastName = lastName;
    }
    
}
  • Repository class (AbstractBaseRepository.java)
package strutshelloworld.net.ren.struts2.repository;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

import javax.persistence.Query;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * The Class AbstractBaseRepository.
 *
 * @param <T>
 *            the generic type
 */
public abstract class AbstractBaseRepository<T> implements BaseRepository<T> {

	/** The entity class. */
	private Class<T> entityClass;

	/** The session factory. */
	@Autowired
	private SessionFactory sessionFactory;

	/** The hql. */
	private String hql;

	/** The entities. */
	private List<T> entities;
	/**
	 * Instantiates a new abstract base repository.
	 */
	public AbstractBaseRepository() {
		Type t = getClass().getGenericSuperclass();
		ParameterizedType pt = (ParameterizedType) t;
		entityClass = (Class) pt.getActualTypeArguments()[0];
	}

	/**
	 * Sets the session factory.
	 *
	 * @param sessionFactory
	 *            the new session factory
	 */
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	/**
	 * Gets the current session.
	 *
	 * @return the current session
	 */
	protected Session getCurrentSession() {
		try {
			return sessionFactory.getCurrentSession();
		} catch (HibernateException e) {
			return sessionFactory.openSession();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * strutshelloworld.net.ren.struts2.repository.BaseRepository#add(java.lang.
	 * Object)
	 */
	public void add(T entity) {
		getCurrentSession().saveOrUpdate(entity); // .save(entity);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * strutshelloworld.net.ren.struts2.repository.BaseRepository#deleteById(
	 * int)
	 */
	@SuppressWarnings("unchecked")
	public void deleteById(int id) {
		hql = "delete " + entityClass.getName() + " where id = :id";
		Query q = getCurrentSession().createQuery(hql).setParameter("id", id);
		q.executeUpdate();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * strutshelloworld.net.ren.struts2.repository.BaseRepository#getById(int)
	 */
	@SuppressWarnings("unchecked")
	public T getById(int id) {
		return (T) getCurrentSession().get(entityClass.getName(), id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see strutshelloworld.net.ren.struts2.repository.BaseRepository#getAll()
	 */
	@SuppressWarnings({ "deprecation", "unchecked" })
	public List<T> getAll() {
		return getCurrentSession().createCriteria(entityClass).list();
	}
	
	/**
	 * Delete all.
	 */
	public void deleteAll(){
		entities = getAll();
		for(T entity : entities){
			getCurrentSession().delete(entity);
		}
	}
	
	/**
	 * Delete.
	 *
	 * @param entity the entity
	 */
	public void delete(T entity){
		getCurrentSession().delete(entity);
	}
}
  • Repository interface (BaseRepository.java)
package strutshelloworld.net.ren.struts2.repository;

import java.util.List;


/**
 * The Interface BaseRepository.
 *
 * @param <T> the generic type
 */
public interface BaseRepository<T> {

	/**
	 * Adds the.
	 *
	 * @param entity the entity
	 */
	public  void add(T entity);

	/**
	 * Delete by id.
	 *
	 * @param id the id
	 */
	public void deleteById(int id);

	/**
	 * Gets the by id.
	 *
	 * @param id the id
	 * @return the by id
	 */
	public  T getById(int id);

	/**
	 * Gets the all.
	 *
	 * @return the all
	 */
	public  List<T> getAll();
	
	/**
	 * Delete all.
	 */
	public void deleteAll();
	
	/**
	 * Delete.
	 *
	 * @param entity the entity
	 */
	public void delete(T entity);

}
  • Repository interface(StudentRepository.java)
package strutshelloworld.net.ren.struts2.repository;

import strutshelloworld.net.ren.struts2.domain.Student;

/**
 * The Interface StudentRepository.
 */
public interface StudentRepository extends BaseRepository<Student> {

}
  • Repository class (StudentRepositoryImpl.java)
package strutshelloworld.net.ren.struts2.repository.impl;

import org.springframework.stereotype.Repository;

import strutshelloworld.net.ren.struts2.domain.Student;
import strutshelloworld.net.ren.struts2.repository.AbstractBaseRepository;
import strutshelloworld.net.ren.struts2.repository.StudentRepository;

/**
 * The Class StudentRepositoryImpl.
 */
@Repository("studentRepository")
public class StudentRepositoryImpl extends AbstractBaseRepository<Student> implements StudentRepository {

}
  • Service Interface (StudentService.java)
package strutshelloworld.net.ren.struts2.service;

import java.util.List;

import strutshelloworld.net.ren.struts2.domain.Student;

// TODO: Auto-generated Javadoc
/**
 * The Interface StudentService.
 */
public interface StudentService {

	/**
	 * Adds the student.
	 *
	 * @param student
	 *            the student
	 */
	public void addStudent(Student student);

	/**
	 * Delete student by id.
	 *
	 * @param id
	 *            the id
	 */
	public void deleteStudentById(int id);

	/**
	 * Find student by id.
	 *
	 * @param id
	 *            the id
	 * @return the student
	 */
	public Student findStudentById(int id);

	/**
	 * Find all.
	 *
	 * @return the list
	 */
	public List<Student> findAll();
	
	/**
	 * Delete all.
	 */
	public void deleteAll();
	
	/**
	 * Delete.
	 *
	 * @param entity the entity
	 */
	public void delete(Student entity);
}
  • Service class (StudentServiceImpl.java)
package strutshelloworld.net.ren.struts2.service.impl;

import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import strutshelloworld.net.ren.struts2.domain.Student;
import strutshelloworld.net.ren.struts2.repository.StudentRepository;
import strutshelloworld.net.ren.struts2.service.StudentService;

// TODO: Auto-generated Javadoc
/**
 * The Class StudentServiceImpl.
 */
@Service
@Transactional
public class StudentServiceImpl implements StudentService {

	/** The student repository. */
	@Autowired
	private StudentRepository studentRepository;

	/*
	 * (non-Javadoc)
	 * 
	 * @see strutshelloworld.net.ren.struts2.service.StudentService#addStudent(
	 * strutshelloworld.net.ren.struts2.domain.Student)
	 */
	public void addStudent(Student student) {
		studentRepository.add(student);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * strutshelloworld.net.ren.struts2.service.StudentService#deleteStudentById
	 * (int)
	 */
	public void deleteStudentById(int id) {
		studentRepository.deleteById(id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * strutshelloworld.net.ren.struts2.service.StudentService#findStudentById(
	 * int)
	 */
	public Student findStudentById(int id) {
		return studentRepository.getById(id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see strutshelloworld.net.ren.struts2.service.StudentService#findAll()
	 */
	public List<Student> findAll() {
		return studentRepository.getAll();
	}

	/* (non-Javadoc)
	 * @see strutshelloworld.net.ren.struts2.service.StudentService#deleteAll()
	 */
	public void deleteAll() {
		studentRepository.deleteAll();
	}

	/* (non-Javadoc)
	 * @see strutshelloworld.net.ren.struts2.service.StudentService#delete(strutshelloworld.net.ren.struts2.domain.Student)
	 */
	public void delete(Student entity) {
		studentRepository.delete(entity);
	}
}
  • Action class (StudentAction.java)
package strutshelloworld.net.ren.struts2.actions;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

import strutshelloworld.net.ren.struts2.domain.Student;
import strutshelloworld.net.ren.struts2.service.StudentService;

// TODO: Auto-generated Javadoc
/**
 * The Class StudentAction.
 */
public class StudentAction extends ActionSupport implements ModelDriven, Preparable {

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 3064301885095539523L;

	/** The student service. */
	@Autowired
	private StudentService studentService;

	/** The list student. */
	private List<Student> listStudent = new ArrayList<Student>();

	/** The student. */
	private Student student = new Student();
	
	/** The request. */
	private HttpServletRequest request;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.opensymphony.xwork2.ModelDriven#getModel()
	 */
	public Object getModel() {
		return student;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.opensymphony.xwork2.Preparable#prepare()
	 */
	public void prepare() throws Exception {
		
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	public String execute() throws Exception {
		return SUCCESS;
	}

	/**
	 * Adds the student.
	 *
	 * @return the string
	 */
	public String addStudent() {
		studentService.addStudent(student);
		return SUCCESS;
	}
	
	/**
	 * Update student.
	 *
	 * @return the string
	 */
	public String updateStudent() {
		studentService.addStudent(student);
		return SUCCESS;
	}

	/**
	 * Delete student.
	 *
	 * @return the string
	 */
	public String deleteStudent() {
        request = (HttpServletRequest) ActionContext.getContext().get( ServletActionContext.HTTP_REQUEST);
		studentService.deleteStudentById((Integer.parseInt(request. getParameter("id"))));
		return SUCCESS;
	}

	/**
	 * Gets the all student.
	 *
	 * @return the all student
	 */
	public String getAllStudent() {
		listStudent = studentService.findAll();
		return SUCCESS;
	}

	/**
	 * Update student.
	 *
	 * @return the string
	 */
	public String getStudentById(){
		request = (HttpServletRequest) ActionContext.getContext().get( ServletActionContext.HTTP_REQUEST);
		student = studentService.findStudentById(Integer.parseInt(request. getParameter("id")));
		return SUCCESS;
	}
	
	/**
	 * Delete all.
	 *
	 * @return the string
	 */
	public String deleteAll(){
		studentService.deleteAll();
		return SUCCESS;
	}
	
	/**
	 * Gets the list student.
	 *
	 * @return the list student
	 */
	public List<Student> getListStudent() {
		return listStudent;
	}

}
  • struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
	<constant name="struts.enable.DynamicMethodInvocation" value="false" />
	<constant name="struts.devMode" value="false" />
	<constant name="struts.custom.i18n.resources" value="app-resources" />
	<constant name="struts.action.extension" value="html" />
    <constant name="struts.mapper.action.prefix.enabled" value="true"/>

	<package name="default" namespace="/" extends="struts-default">

		<action name="index">
			<result type="redirect">/login.html</result>
		</action>

	  <action name="showAddStudent" class="strutshelloworld.net.ren.struts2.actions.StudentAction" method="execute">
			<result name="success">/WEB-INF/jsp/AddStudent.jsp</result>
		</action>

		<action name="addStudent" class="strutshelloworld.net.ren.struts2.actions.StudentAction" method="addStudent">
			<result name="success" type="redirect">listStudent.html</result>
		</action>
		
		<action name="updateStudent" class="strutshelloworld.net.ren.struts2.actions.StudentAction" method="updateStudent">
			<result name="success" type="redirect">listStudent.html</result>
		</action>
		
		<action name="deleteStudent" class="strutshelloworld.net.ren.struts2.actions.StudentAction" method="deleteStudent">
			<result name="success" type="redirect">listStudent.html</result>
		</action> 
        
        <action name="deleteAll" class="strutshelloworld.net.ren.struts2.actions.StudentAction" method="deleteAll">
              <result name="success" type="redirect">listStudent.html</result>
        </action>
	
		<action name="getStudentById" class="strutshelloworld.net.ren.struts2.actions.StudentAction" method="getStudentById">
				<result name="success">/WEB-INF/jsp/AddStudent.jsp</result>
		</action> 
		  
	   <action name="listStudent" class="strutshelloworld.net.ren.struts2.actions.StudentAction" method="getAllStudent">
			<result name="success">/WEB-INF/jsp/ListStudent.jsp</result>
		</action>
		
		<action name="login" class="strutshelloworld.net.ren.struts2.actions.LoginAction">
			<result name="success">/WEB-INF/jsp/Welcome.jsp</result>
			<result name="error">/WEB-INF/jsp/Login.jsp</result>
			<result name="input">/WEB-INF/jsp/Login.jsp</result>
		</action>
	</package>

</struts>
  • application-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

	<context:property-placeholder location="classpath:hibernate.properties" />
	
    <context:annotation-config/>
    
    <context:component-scan base-package="strutshelloworld.net.ren.struts2"></context:component-scan>
    
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="${jdbc.driverClassName}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
	</bean>

	<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="packagesToScan" value="strutshelloworld.net.ren.struts2.domain" />
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
				<prop key="hibernate.dialect">${hibernate.dialect}</prop>
				<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
			</props>
		</property>
	</bean>

	<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

	<bean id="persistenceExceptionTranslationPostProcessor" class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
		
	<tx:annotation-driven transaction-manager="transactionManager" />

</beans>
  • application-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="struts_blank" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <display-name>Struts Blank</display-name>
  
   <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/application-context.xml</param-value>
    </context-param>
     
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>
      org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
    </filter-class>
  </filter>

  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
</web-app>
  • AddStudent.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Add Student</title>
</head>
<body>
	<h1>Struts 2 ModelDriven example</h1>
	<s:if test="%{model.id == 0}">
		<h2>Add Student</h2>
	</s:if>
	<s:else>
		<h2>Update Student</h2>
	</s:else>
	<s:form action="addStudent">
		<s:if test="%{model.id != 0}">
            <s:textfield name="id" key="label.id" value="%{model.id}" />
	   </s:if>
		<s:textfield name="firstName" key="label.fname"
			value="%{model.firstName}" />
		<s:textfield name="lastName" key="label.lname"
			value="%{model.lastName}" />
		<s:if test="%{model.id == 0}">
			<s:submit key="label.submit" align="center"/>
		</s:if>
		<s:else>
			<s:submit key="label.update" align="center" action="updateStudent"/>
		</s:else>
	</s:form>
	
</body>
</html>
  • ListStudent.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>List of Student</title>
</head>
<body>
     <table cellpadding="5px">
		<tr class="even">
			<th>Id</th>
			<th>Fist Name</th>
			<th>Last Name</th>
			<th>Edit </th>
			<th>Delete </th>
		</tr>
        <c:forEach items="${listStudent}" var="student">
			<tr>
				<td><c:out value="${student.id}"/></td>
				<td><c:out value="${student.firstName}"/></td>
				<td><c:out value="${student.lastName}"/></td>
		    	<td>
               <s:url var="updateUrl" action="getStudentById">
					<s:param name="id">${student.id}</s:param>
				</s:url> 
				<s:a href="%{updateUrl}">Update</s:a>
                </td>
				<td>
                <s:url var="deleteUrl" action="deleteStudent">
					<s:param name="id">${student.id}</s:param>
				</s:url> 
                <s:a href="%{deleteUrl}">Delete</s:a>
                </td> 
			</tr>
		</c:forEach> 
	</table>
	<s:url var="deleteAllUrl" action="deleteAll"/>
	<s:a href="%{deleteAllUrl}">Delete All</s:a>
</body>
</html>

You can get project in here via Github
Thank you.

PS.
Before running this code, you must edit hibernate.properties file to adjust your local and create database with “hibernate” name in mysql.

Struts2 Dynamic Method Invocation

April 14, 2017 Leave a comment

Dynamic invocation in Struts2 is used to avoid configuration separate action mapping for each method in action class by using wildcard method.

Look at Struts configuration below.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
	<constant name="struts.enable.DynamicMethodInvocation" value="true" />
	<constant name="struts.devMode" value="false" />
	<constant name="struts.custom.i18n.resources" value="app-resources" />
	<constant name="struts.action.extension" value="html" />
        <constant name="struts.mapper.action.prefix.enabled" value="true"/>
 
	<package name="default" namespace="/" strict-method-invocation="false" extends="struts-default">

		<action name="*Book" method="{1}" class="strutshelloworld.net.ren.struts2.actions.BookAction">
		      <result name="success">/WEB-INF/jsp/Book.jsp</result>
		</action>
	</package>
</struts>

Action code.

import com.opensymphony.xwork2.ActionSupport;

/**
 * The Class BookAction.
 */
public class BookAction extends ActionSupport {

    /** The message. */
    private String message;

    /* (non-Javadoc)
     * @see com.opensymphony.xwork2.ActionSupport#execute()
     */
    public String execute() {
        message = "Inside execute method";
        return SUCCESS;
    }

    /**
     * Adds the.
     *
     * @return the string
     */
    public String add() {
        message = "Inside add method";
        return SUCCESS;
    }

    /**
     * Update.
     *
     * @return the string
     */
    public String update() {
        message = "Inside update method";
        return SUCCESS;
    }

    /**
     * Delete.
     *
     * @return the string
     */
    public String delete() {
        message = "Inside delete method";
        return SUCCESS;
    }

    /**
     * Gets the message.
     *
     * @return the message
     */
    public String getMessage() {
        return message;
    }

    /**
     * Sets the message.
     *
     * @param message the new message
     */
    public void setMessage(String message) {
        this.message = message;
    }
}

Please consider this as view code

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>In here</title>
</head>
<body>


<h1>Hello Here</h1>


<s:form action="Book">
<s:submit action="addBook" key="label.submit" align="center" />
<s:submit action="updateBook" key="label.update" align="center" />
<s:submit action="deleteBook" key="label.delete" align="center" />
</s:form>
</body>
</html>

As you can see, instead writing different name for each action name, action is provided with name=”*Book” method=”{1}” means if the input is ‚ÄúaddBook‚ÄĚ or “updateBook”¬Ě then the add() / update() method will be executed in Action class accordingly.

Please note it, strict-method-invocation (SMI) property in package tag has default value true. For futher reading about Strict Method Invocation (SMI)  you can see in here and here.

Thank you. ūüôā