Sorting ArrayList in Java is a common task for Java developers and we have touched it while discussing in my last article 10 Examples of ArrayList in Java and again when we discussed comparator and comparable in Java. There are multiple ways to sort ArrayList in Java for example, you an use Collections.sort() method or List.sort(), or Stream.sort() method to sort an ArrayList in Java depending upon which Java version are you using. If you want a solution irrespective of Java version then Collections.sort() is the best way because this method is available since JDK 1.1. In order to sort an ArrayList, we need to use the Collections utility class which contains an overloaded sort() method for sorting different collections and supports different comparators in Java. In this article, we will see how to sort ArrayList in the natural order of elements and then sorting ArrayList in Java with a comparator.
Thursday, December 28, 2023
Sunday, December 17, 2023
How to enable Spring MVC in Java Web Application? Example Tutorial
In last article, I have shared thoughts on how to enable Spring Security on Java Web application, where my reader also shown interest to know how to configure/enable Spring MVC in Java web application. Though, this is very basic information, it helps a lot to anyone who is new to String framework and want to understand then essential steps required to enable Spring MVC framework for a Java based web application. It's easy to configure Spring MVC if you have developed a simple hello word kind of program using Servlet or JSP and familiar with web application fundamentals in Java world e.g. Servlet container like Tomcat, deployment descriptor file or web.xml, Servlet or JSP itself and some essential tags on web.xml e.g. <servlet> and <servlet-mapping>, <url-pattern> etc.
Labels:
spring
,
spring mvc
Monday, December 11, 2023
How to conditionally render view in JSP using Spring Security tag library? Example tutorial
One of the common requirement of secure Java application is to show and hide content based upon role of current user. For example, a logged in user can see his name e.g. Welcome User1 but that link is not visible to an unauthenticated user. Similarly, a user with admin role can see a lot of admin related functionality which should not be visible to a normal user e.g. add/remove users, disable users, create roles, edit roles etc. How can you achieve such condition based rendering in view? Does spring security support that? Well, the answer is Yes. Spring security provide a tag library which you can use in JSP pages to perform a lot of authentication and authorization related stuff e.g. you can access current user's name, you can access access current user's role and you can also conditionally show/hide certain section of views based upon user's role.
Labels:
spring
,
spring interview questions
,
spring security
Monday, December 4, 2023
How to get current logged in user in JSP and Controller using Spring Security? Example Tutorial
One of the common task while using Spring Security in a Java web application is getting the username of currently logged in user. Sometime, you need that in your controller class and sometime in JSP for view purpose, but the big question mark is how do you get the current user in Spring Security? Well, there are many ways to do it and depending upon whether you need it inside a JSP page or Controller class, you can choose them. The current user, also known as Principal in Spring Security can be obtained from the UserDetails class, which holds all details for currently logged in user. You can get this class from the SecurityContext, which in turn can be retrieved from SecurityContextHolder as shown below:
Labels:
spring
,
spring interview questions
,
spring security
Subscribe to:
Posts
(
Atom
)