Tuesday, 15 March 2016

Spring Project Setup in Eclipse

Step 1:

create workspace in your drive.

Step 2:

add jdk in windows -> preferences -> java -> installed jres

Step 3:

create new dynamic web project.

goto file -> new -> dynamic web project

give module version as 2.5

Step 4:

add tomcat in windows -> preferences -> server -> runtime environments -> add

Step 5:

add spring jars

right click on project -> Build path -> Configure Build Path -> Libraries -> Add external jars -> add spring jar from your local computer

Step 6:

set deployment assembly

right click on project -> Build path -> Configure Build Path -> select deployment assembly -> add -> java build path entries -> add -> apply -> finish

Step 7:

set web.xml:

copy below code within web-apps in web.xml

<!-- project name here -->
<display-name>RestaurantApp</display-name>
 
  <servlet>
<servlet-name>sdnext</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
</servlet>

<!-- what files you allow in this project  -->
<servlet-mapping>
<servlet-name>sdnext</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>

<!-- 2 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- 1 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/sdnext-*.xml,</param-value>
</context-param>

<!-- starting page here -->
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>

<!-- Spring Security -->
<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>


Step 8:

place sdnext-security.xml and sdnext-servlet.xml files in WEB-INF

sdnext-servlet.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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

<!-- <context:property-placeholder location="classpath:resources/database.properties" /> -->

<!-- Base Package start--> <!-- give base packages here -->
<context:component-scan base-package="com.gb" />
<!-- Base Package end-->

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

<!-- view(JSP) setup start-->
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- view(JSP) setup End-->

<!-- database setup start -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/springsecuritydbs" /> <!-- DB name here -->
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<!-- database setup end -->

<!-- Model creation in database -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list> <!-- Domains Here -->
<!-- <value>com.gb.model.Login</value>
<value>com.gb.model.Signup</value>
<value>com.gb.model.Employee</value>
<value>com.gb.setups.model.Charges</value>
<value>com.gb.setups.model.SpecialitySetup</value>
<value>com.gb.setups.model.DoctorSetup</value>
<value>com.gb.setups.model.OutPatient</value>  -->
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<!-- Model creation in database -->


<!-- Hibernate Template start -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory"> <ref bean="sessionFactory" /> </property>
</bean>
<bean id="hibernateTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- Hibernate Template end -->

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"></bean>
</beans>

Here you give


  • base package
  • jsp pages path & extentions
  • database name & credentials
  • domains in sessionFactory
sdnext-security.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:security="http://www.springframework.org/schema/security"
       xmlns:p="http://www.springframework.org/schema/p" 
       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-3.1.xsd">

   <security:http auto-config="true">
<security:intercept-url pattern="/index*" access="ROLE_USER" />
<!-- <security:form-login login-page="/login" default-target-url="/index"  
   authentication-failure-url="/fail2login" />  -->  
</security:http>

    <security:authentication-manager>
 <security:authentication-provider>
   <security:user-service>
<security:user name="admin" password="admin" authorities="ROLE_USER" />
   </security:user-service>
 </security:authentication-provider>
</security:authentication-manager>

</beans>

Step 9:

Flow:

JSP -> Controller -> Service -> Dao -> Model

Step 10:

Create packages in src

like com.gb.spring.controller
com.gb.spring.services
com.gb.spring.dao
com.gb.spring.model

create package like com.vamsi.security

create java file like HelloSecurityController

place the given code in controller for view after login authentication

@Controller
@RequestMapping("/index")
public class HelloSecurityController {
@RequestMapping(method = RequestMethod.GET)
public String executeSecurity(ModelMap model) {
System.out.println("Path Recognized.....");
model.addAttribute("message", "Spring Security Hello World");
model.addAttribute("author", "By DineshOnJava.com");
return "welcome";
}
}

in welcome file list we give file as index, based on that request mapping it renders the welcome.jsp. we created in WEB-INF

the files structure as follows





after running the it shows like as follows


after successful login it will show welcome page as follows


Saturday, 5 March 2016

How to use eachWithIndex in Groovy

Code:

class GroovyController {

def index(){
println '-----------------------------------'
def array = [10,20,30,40,50]
array.eachWithIndex {it, i->
println 'array['+i+'] = '+it
}
render ''
}

}

Output:

array[0] = 10
array[1] = 20
array[2] = 30
array[3] = 40
array[4] = 50

Difference between toSet() and unique() in Groovy

toSet() and unique() are used for used for getting unique elements from given array. the difference is toSet() didn't modify the given array, it returns result array. but unique modifies the given array that means, the result is stored in given array.

Code:

class GroovyController {

def index(){
println '-----------------------------------'
def array = [1,2,2,3,4,5]
println 'use set : '+array.toSet()
println 'result array : '+array

array = [1,2,2,3,4,5]
println 'use unique : '+array.unique()
println 'result array : '+array
render ''
}

}

Output:

use set : [1, 2, 3, 4, 5]
result array : [1, 2, 2, 3, 4, 5]
use unique : [1, 2, 3, 4, 5]
result array : [1, 2, 3, 4, 5]

How to remove particular index of an element in array of Groovy

Code:

class GroovyController {

def index(){
println '-----------------------------------'
def array = [1,2,3,4,5]
println 'remove 2nd element : '+array.remove(1)
println 'result array : '+array
render ''
}

}

Output:

remove 2nd element : 2
result array : [1, 3, 4, 5]

How to remove all elements in array of Groovy

Code:

class GroovyController {

def index(){
println '-----------------------------------'
def array1 = [1,2,3,4,5]
def array3 = [1,2,8]
println 'Initial array : '+array1
println 'Remove array3 from array1 : '+array1.removeAll(array3)
println 'Afer Remove array3 : '+array1
println 'Remove with closure condition  : '+array1.removeAll{ it > 4 }
println 'After closure condition : '+array1
render ''
}

}

Output:

Initial array : [1, 2, 3, 4, 5]
Remove array3 from array1 : true
Afer Remove array3 : [3, 4, 5]
Remove with closure condition  : true
After closure condition : [3, 4]

How to use + and - in arrays of Groovy

Code:

class GroovyController {

def index(){
println '-----------------------------------'
def array1 = [1,2,3,4,5]
def array2 = [6,7]
def array3 = [1,2,8]
println 'Add array1 and array2 : '+(array1 + array2)
println 'Subract array3 from array1  : '+(array1 - array3)
render ''
}

}

Output:

Add array1 and array2 : [1, 2, 3, 4, 5, 6, 7]
Subract array3 from array1  : [3, 4, 5]

How to add an element in array of Groovy

Code:

class GroovyController {

def index(){
println '-----------------------------------'
def originalArray = [1,2,3,4,5]
def element = 6
println 'Original array : '+originalArray
println 'After insertion of element : '+(originalArray << element)
render ''
}

}

Output:

Original array : [1, 2, 3, 4, 5]
After insertion of element : [1, 2, 3, 4, 5, 6]

How to get common elements in arrays of Groovy

Code:

class GroovyController {

def messageSource

def index(){
def array1 = [1,2,3,4,5]
def array2 = [3,4]
println 'Intersection of array1 and array2 : '+array1.intersect(array2)
render ''
}

}

Output:

Intersection of array1 and array2 : [3, 4]

Friday, 4 March 2016

How to rise an event when modal show / hide in JQuery


$('#modalId').on('show.bs.modal',function(){

            // place your code here

});


$('#modalId').on('hide.bs.modal',function(){

            // place your code here

});

How to use inject in Groovy


Tuesday, 1 March 2016