Friday, January 11, 2013

Localization in struts2

1- add this line to the struts.xml file :

<constant name="struts.custom.i18n.resources" value="global" />

2-add the (global.properties) under source package



3-in the (global.properties)

connection.success.message=Connection Success %s


4-to use it in the action, just write the following :

getText("connection.success.message")

Friday, January 4, 2013

Connect multiple DB per Transaction using Hibernate, Struts2 and Spring

Hi Everyone,

I find a solution to connect multiple DB per one transaction using  Hibernate, Struts2 and Spring.
To do that in your project, you need to add Spring Jars:



org.springframework.aop-3.0.5.RELEASE.jar
org.springframework.asm-3.0.5.RELEASE.jar
org.springframework.aspects-3.0.5.RELEASE.jar
org.springframework.beans-3.0.5.RELEASE.jar
org.springframework.context-3.0.5.RELEASE.jar
org.springframework.context.support-3.0.5.RELEASE.jar
org.springframework.core-3.0.5.RELEASE.jar
org.springframework.expression-3.0.5.RELEASE.jar
org.springframework.instrument-3.0.5.RELEASE.jar
org.springframework.instrument.tomcat-3.0.5.RELEASE.jar
org.springframework.jdbc-3.0.5.RELEASE.jar
org.springframework.jdbc.jar
org.springframework.jms-3.0.5.RELEASE.jar
org.springframework.orm-3.0.5.RELEASE.jar
org.springframework.orm.jar
org.springframework.oxm-3.0.5.RELEASE.jar
org.springframework.test-3.0.5.RELEASE.jar
org.springframework.transaction-3.0.0.RELEASE.jar
org.springframework.transaction-3.0.5.RELEASE.jar
org.springframework.web-3.0.2.RELEASE.jar
org.springframework.web-3.0.5.RELEASE.jar
org.springframework.web.jar
org.springframework.web.portlet-3.0.5.RELEASE.jar
org.springframework.web.servlet-3.0.5.RELEASE.jar
org.springframework.web.struts-3.0.5.RELEASE.jar
spring-context-3.0.5.RELEASE.jar
spring-core.jar
commons-dbcp-1.4.jar
commons-pool-1.6.jar

you might don't need all of them. 

and here is the file hierarchies:


applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 
<bean id="dataSourceBean" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost/test" /> 
<property name="username" value="root" />
<property name="password" value="root" />
</bean>

<bean id="sessionFactoryBean"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSourceBean" />
<property name="mappingResources">
<list>
<value>tracking/User.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=true
</value>
</property>
</bean>
 
<bean id="hibernateTemplateBean" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactoryBean" />
</property>
</bean>
<bean id="userDAOBean" class="tracking.UserDAO">
<property name="hibernateTemplate" ref="hibernateTemplateBean" />
</bean>
<bean id="userAction" class="tracking.UserAction">
<property name="userDAO" ref="userDAOBean" />
</bean>
</beans>

UserAction.java

package tracking;

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

import java.sql.Types;

import javax.sql.DataSource;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import tracking.*;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class UserAction extends ActionSupport implements ModelDriven<User>

{

private static final long serialVersionUID = 1L;

private User          user         = new User();
private UserDAO          userDAO;

public User getModel()
{
return user;
}


public String test(){
System.out.println(" I am in test I " );
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml", UserAction.class);
System.out.println("ApplicationContext "+ac );
   DataSource dataSource = (DataSource) ac.getBean("dataSourceBean");
   // DataSource mysqlDataSource = (DataSource) ac.getBean("mysqlDataSource");
   System.out.println("dataSource "+dataSource );
   JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
   System.out.println("jdbcTemplate "+jdbcTemplate );
   jdbcTemplate.batchUpdate(new String[] { "update user set idUser = 8 where idUser=1",
       "update user set idUser = 9 where idUser=2" });
    
   return Action.SUCCESS;
}

/**
     * @return the user
     */
    public User getUser()
    {
    return user;
    }

/**
     * @param user the user to set
     */
    public void setUser(User user)
    {
    this.user = user;
    }

/**
     * @return the userDAO
     */
    public UserDAO getUserDAO()
    {
    return userDAO;
    }

/**
     * @param userDAO the userDAO to set
     */
    public void setUserDAO(UserDAO userDAO)
    {
    this.userDAO = userDAO;
    }
}

User.hbm.xml


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping default-access="field">
    <class name="tracking.User" schema="test" table="user">
     
        <id name="idUser" type="java.lang.Integer">
            <column length="15" name="idUser" />
            <generator class="assigned" />
        </id>
   
    </class>
</hibernate-mapping>


!!!!!!!!!!  Important Note !!!!!!!!!! 

if you get exception while trying to connect the DB, 

TNS:listener does not currently know of SID given in connect descriptor 


use this in the applicationContext.xml 


<bean id="XXX" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=1.1.1.1) (PORT=1525))(CONNECT_DATA=(SERVICE_NAME=XXX)))"/>
<property name="username" value="xxx" />
<property name="password" value="xxx" />
</bean>



http://www.mkyong.com/spring/spring-jdbctemplate-querying-examples/

http://www.mkyong.com/spring/spring-error-classnotfoundexception-org-springframework-web-context-contextloaderlistener/

http://francisoud.blogspot.com/2008/11/simple-jdbc.html

http://www.nabisoft.com/tutorials/struts2/basic-struts2-project-setup#Step4


Tuesday, October 2, 2012

custom converter for BigDecimal struts2

if you are trying to map a BigDecimal field in jsp to the Action, you may get exception,
to solve this exception you need a converter to convert it.

1-you need to define file by the name Action_Name-conversion.properties


attrb1=com.a.b.BigDecimalConverter
attrb2=com.a.b.BigDecimalConverter

2-add the BigDecimalConverter in the package you want


public class BigDecimalConverter extends StrutsTypeConverter{

@Override
public Object convertFromString(Map context, String[] values, Class toClass) {
if(values!=null && values.length>0){
String s=values[0];
if(s!=null && s.trim().length()>0){
BigDecimal bd = new BigDecimal(s);
return bd.setScale(2, BigDecimal.ROUND_HALF_UP);
}
}
return null;
}

@Override
public String convertToString(Map context, Object o) {
return o.toString();

}





Wednesday, September 19, 2012

show/hide html component using java script


HTML Code: 

<TD>
<s:select id="dateTargetWriteOffComparitor" 
onchange="showHide('dateTargetWriteOffComparitor','toDateOfTarget')"
value="searchDateCriteriaOperators[0]"
list="searchDateCriteriaOperators"
name="dateTargetWriteOffComparitor" />
</TD>

<TD height="23" width="91"> 
    <sj:datepicker name="dateOfTarget" displayFormat="mm-dd-yy" style="width: 80px;" />

<div id="toDateOfTarget" style="display: none;">
   <sj:datepicker name="toDateOfTarget" displayFormat="mm-dd-yy" style="width: 80px; " />
</div>


Java Script Code: 


function showHide(htmlComp,target){
var listValue=document.getElementById(htmlComp).value;  
if(listValue=='Between'){
document.getElementById(target).style.display = 'block';
}else{
document.getElementById(target).style.display = 'none';
}
}

Friday, August 24, 2012

Clear Form with jQuery

<html>

<head>
<script language="JavaScript" type="text/JavaScript" src="Script/jquery-1.8.0.min.js"></script>
</head>

<body>

<form id="frm">
          <input type="button"  value="Clear" onclick="resetForm('frm')" />
</form>

<script>
/**
* This function clears all input fields value except button, submit, reset, hidden fields
* */
function resetForm(formid) {
                  $(':input','#'+formid) .not(':button, :submit, :reset, :hidden') .val('') .removeAttr('checked').removeAttr('selected');
 
}

</script>

</body>
</html>

Thursday, August 16, 2012

scrollable div


To make the div scroll-able in vertical direction:  


<div  style="height: 300px; overflow-y: scroll">

</div>

How to view List inside list using

if you have a list inside another list, for example you want to iterate over students list and students courses 
and you want to view it in the jsp page, you can use the <s:iterator>  as follows: 

<s:iterator value="studentsList" >

        <s:property value="studentName"/>

       <s:iterator value="%{courses}" >

                      <s:property value="courseName"/>

       </s:iterator> 

</s:iterator> 


This example read the studentsList from struts action.