Thursday 26 September 2013

Struts 2 action called up twice.

Problem :

If you are using JSON plugin with struts2 then you might end up with this problem. your action get called up twice even when you have define it only once and you are firing ajax only at once.


Solution:

This is happening because JSON plugin is calling all your methods that start with "get" in an attempt to serialize them for output.Try to rename the method name other then "getxxxxx()".



method name start with getXX:
public String getName() {
    return "Rakesh";
}

method name changed to fetchXX:

public String fetchName() {
   return "Rakesh";
}

Hopefully this will work.

Wednesday 4 September 2013

IE 8 Div/span/popup/HTML content section is not visible

Problem:

IE-8 Div/span/popup/HTMLcontent section is not visible 

Solution :

This issue usually occurs because the HTML content you are preparing is not correctly nested.

Please insure that each and every TAG which is open should be closed correctly and also nested properly,otherwise your content is not visible.

Regarding Firefox/Chrome these browsers are smart enough to handle these situations. They can nest the DOM by their own if its not properly nested. 


IE 8 issue :Downloading pop up comes out when firing ajax for uploading file


Downloading pop up comes out when firing ajax for uploading file




This pop up comes out because ajax call after success need a response type in IE-8. So we can set the response by any of these way.

If you are using Struts2 you can simply set the content type in the parameter.

Struts.Xml:

<result name="success" type="json">
  <param name="contentType">text/html</param>
</result>


you can achive this using java you just need to set the result response in the JSON


return Json(result, "text/html");




Saturday 31 August 2013

Recover Task manager in windows

Just follow these steps to recover your taskmanager

1.Type REGEDIT in RUN
2. Window gets open
3.Navigate to this path

HKEY_CURRENT_USER\Software\Microsoft\Windows\Curre ntVersion\Policies\ taskmanager

4. Delete the disable task manager key.


Lock Out Unwanted Users in windows

Want to keep people from accessing Windows, even as the default user? If you do not have a domain do not attempt this.

1. Open RegEdit
2. Go to HKEY_LOCAL_MACHINE\Network\Logon
3. Create a dword value "MustBeValidated"
4. Set the value to 1

Hide Folder in windows

First Create a folder without any name and with icon that is totally invisible

Just type these codes while renaming the folder name and  press enter once you are done

alt+0160
OR
alt+255


Now time to hide the folder, to hide a folder first select the folder then right click and go to properties,

in properties select the change icon option ...
now you can see a lot of folder icon.

(C:\Windows\system32\imageres.dll)..

you look for a blank icon select any of the 3 blank icon. click ok then apply u will c invisible folder...
(now with no name and no icon too..)

Folder Option Not working in windows

Sometimes folder options in your PC may get disabled by some virus and after removing the virus,
you can not use folder options.
Here i am sharing some tricks to activate folder options again
Before doing this first remove that virus from ur computer using some good AV

Method:1

Type “regedit” in run command and hit enter
Find any of the following keys:
User Key: HKEY_CURRENT_USER\Software\Microsoft\Windows\Curre ntVersion\Policies\ Explorer

System Key: HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\Curr entVersion\Policies\ Explorer

Value Name: NoFolderOptions
Data Type: REG_DWORD (DWORD Value)
Value Data: 0 = show options, 1 = hide options

Method:2

>do: start
> Run
> Type gpedit.msc
> hit enter
> User Configuration
> Administrative Templates
> Windows Components
> Windows Explorer
> Select Removes the Folder Options menu item from the Tools menu.
> Right click:
> Properties
> Disable
> Apply
“Shell”=”Explorer.exe “


Tuesday 27 August 2013

Java.net.BindException: Address already in use: JVM_Bind:8080 Solution


This exception is self explanatory, its saying that a Java application is trying to connect on port 8080 but that port is already used by some other process and JVM Bind to that particular port, here its 8080, is failed. Now to fix this error you need to find out which process is listening of port 8080, we will how to find a process which is listening on a particular port in windows and then how to kill that process to make our port free to use.


Common Scenario when you see "Address already in use: JVM_Bind"
1. While doing Java remote debugging in Eclipse and when Eclipse tries to connect your remote java application on a particular port and that port is not free.
2. Starting tomcat when earlier instance of tomcat is already running and bonded to 8080 port. It will fail with SEVERE: Error initializing endpoint java.net.BindException: Address already in use: JVM_Bind:8080



Find and listening port


1. Go to run
2. Type cmd
3. Write this command

netstat -ano | find "8080"


You will get a list of process listening to that port





4. Kill all the task 


write this command 


taskkill /F /PID 7820



7820 -- This is the Id which you can see in the last column





Monday 26 August 2013

Call ajax after completion of another ajax ,Wait for ajax to return.


Lets suppose you are trying to set loggedIn username to the header of your web page and you have 2 ajax which get fired

1st Ajax : Which is decorating the header

2nd Ajax : Which gets the username

So if 2nd AJAX become first in the race then username is not visible to you as 1st ajax is responsible for decorating the header and if there is no header there is no place where you can set header.

so to be very sure that our username is visible on the screen, we should wait for the header(1st ajax)to complete


document ready function:


$(document).ready(function () {

 //calling this method to get the fetch logged in user

 fetchloggeInUser();

});



1st Ajax request : run in parallel with 2nd ajax 

this is building our header part
(function ($) {
  $.pageDecorate = {
  //declare a global variable so that it can be used.
  decorateDeferred : null,
  //set response of the ajax in a variable
  var decoratorDeferred = $.ajax({
    url : 'setHeader',
    type : 'get',
    data : decorateHeaderData,
    dataType : 'json',
    success : function (data) {
     //response of testAjax sets in decorateDeferred
     decorateDeferred = $.pageDecorate.testAjax();
    }
   });


Global declaration :

  //set value of global variable decorateDeferred so
  // that it can be use as a reference

  $.pageDecorate.decorateDeferred = decorateDeferred;


  },

Method to call :
 testAjax : function () {

  var navHtml = '<ul>';
  navHtml += '<div class="divHeader" title="login">';
  navHtml += '<div class="role">';
  navHtml += '</div>';
  navHtml += '</div>';
  navHtml += '</ul>';
  //append this to header of your page
  $('.header').append(navHtml);
 }
});


2nd Ajax:

fetchloggeInUser : function () {

 $.ajax({

  url : "loggedInUser",

  type : "POST",

  success : function (data) {

   //although this success block call when ajax get 
   //fired but this code runs when it get response
   //after the success response of $.pageDecorate.decorateDeferred 
   this below success get called

   $.pageDecorate.decorateDeferred.success(function () {

    $('.divHeader').find('.role').text(userName);

   });

  }

 });

}


That 's it you are done.

Saturday 24 August 2013

Struts 2 : The requested resource is not available

404 in Tomcat while using struts2.

Check your struts.xml file .


There must be a missing entry in your Struts.xml file which is not referencing to your project repository.

All entries which are defined in your struts.xml file should point to correct java file in repository .




Project facet Java version is not supported.

Right click on your project and choose build path->configure build path 











select the latest JRE you want to use.




Error is resolved

The type javax.servlet.ServletContext cannot be resolved. It is indirectly referenced from required .class files


if you right click on your project and choose build path->configure build path 


and then choose the libraries tab. click on the add library button.




Now select the server runtime.



Select the appropriate server.


That's it you are done.

Ajax call Jquery


The ajax() method is used to perform an AJAX (asynchronous HTTP) request.

All jQuery AJAX methods use the ajax() method. This method is mostly used for requests where the other methods cannot be used.



Definition :

  $.ajax({name:value, ... })



Usage:

/* get some values from elements on the page: */

var username= $('#username').val();
var pass=$('#pass').val();
//parameters to pass
 var data = {
    username : username,
    pass:pass    
   };
//authenticateUser is the action which you need to call
var testUrl = "user/authenticateUser";
$.ajax({
 url : testUrl,
 type : 'post',
 data : data,
 dataType : 'json',
 success : function(data) {
    //success action Block
    alert("Login success");
  },
   error:function(){
    //Error action block
    alert("Login failed");
   }
});


or you can achieve via this way.


var id = '1'
var testUrl = "user/authenticateUser";
var request = $.ajax({
  url: testUrl,
  type: "POST",
  data: {id : id},
  dataType: "html"
});

request.done(function(msg) {
  $("#log").html( msg );
});

request.fail(function(jqXHR, textStatus) {
  alert( "Request failed: " + textStatus );
});

Thursday 22 August 2013

Hibernate dialects

DB2 - org.hibernate.dialect.DB2Dialect

HypersonicSQL - org.hibernate.dialect.HSQLDialect

Informix - org.hibernate.dialect.InformixDialect

Ingres - org.hibernate.dialect.IngresDialect

Interbase - org.hibernate.dialect.InterbaseDialect

Pointbase - org.hibernate.dialect.PointbaseDialect

PostgreSQL - org.hibernate.dialect.PostgreSQLDialect

Mckoi SQL - org.hibernate.dialect.MckoiDialect

Microsoft SQL Server - org.hibernate.dialect.SQLServerDialect

MySQL - org.hibernate.dialect.MySQLDialect

Oracle (any version) - org.hibernate.dialect.OracleDialect

Oracle 9 - org.hibernate.dialect.Oracle9Dialect

Progress - org.hibernate.dialect.ProgressDialect

FrontBase - org.hibernate.dialect.FrontbaseDialect

SAP DB - org.hibernate.dialect.SAPDBDialect

Sybase - org.hibernate.dialect.SybaseDialect

Sybase Anywhere - org.hibernate.dialect.SybaseAnywhereDialect


org.hibernate.AnnotationException: No identifier specified for entity:


Problem :

org.hibernate.AnnotationException: No identifier specified for entity:

Solution:

You are missing a field annotated with @Id.

Each @Enitity needs an @Id - this is the primary key in the database.

If you don't want your entity to be persisted in a separate table, but rather be a part of other entities, you can use @Embeddable instead of @Entity.

Maven Basics Tutorial

Maven


Maven is an automation and management tool developed by Apache Software Foundation. It was initially released on 13 July 2004.It is used for projects build, dependency and documentation. It simplifies the build process like ANT. In short terms we can tell maven is a tool that can beused for building and managing any Java-based project.



Objective :

The primary goal of Maven is to provide developer with the following :
  • A comprehensive model for projects, which is reusable, maintainable, and easier to comprehend.
  • Plugins or tools that interact with this declarative model.


There are cases when you want to run some specific test cases as many test cases are failing and breaking your build and you just want to check your test case , So you can do this by a simple maven command.




Maven POM :

POM stands for Project Object Model. It is fundamental unit of work in Maven. It is an XML file that resides in the base directory of the project as pom.xml.

The POM contains information about the project and various configuration detail used by Maven to build the project(s).

POM :
<project xmlns = "http://maven.apache.org/POM/4.0.0"
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0
   http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>

   <groupId>com.home.practice-project</groupId>
   <artifactId>my-project</artifactId>
   <version>1.0</version>
 <!-- dependencies for application -->
       <dependencies> 
               <dependency> 
                       <groupId>org.apache.logging.log4j</groupId> 
                       <artifactId>log4j-api</artifactId> 
                       <version>2.11.0</version> 
                 </dependency> 
       </dependencies> 

</project>


Elements used in pom.xml file and their explanation :

project -

It is the root elment of the pom.xml file.

modelVersion 


modelversion means what version of the POM model you are using. Use version 4.0.0 for maven 2 and maven 3.

groupId-


groupId means the id for the project group. It is unique and Most often you will use a group ID which is similar to the root Java package name of the project like we used the groupId com.home.practice-project

artifactId


artifactId used to give name of the project you are building. in our example name of our project is My-project.

version-


version element contains the version number of the project. If your project has been released in different versions then it is useful to give version of your project.

dependencies

dependencies element is used to defines a list of dependency of project.

dependency


dependency defines a dependency and used inside dependencies tag. Each dependency is described by its groupId, artifactId and version.

name


this element is used to give name to our maven project.

scope


this element used to define scope for this maven project that can be compile, runtime, test, provided system etc.

packaging

packaging element is used to packaging our project to output types like JAR, WAR etc.



Maven commands :



1   .  mvn package

create package



2   . mvn clean dependency:copy-dependencies package

The assembly plugin can then be used to package the whole appassembler directory to a zip.


3   . mvn clean

Clean target folder


4   . mvn install 

When you do a Mvn install, it will roughly
  • Generate whatever it needs,
  • Compile the sources,
  • Copy other resources,
  • Create the artifact for your project,
  • Run unit tests,
  • Copy the artifact to the local Mvn repository (this is usually $HOME/.m2/repository).

So a Mvn clean install will first clean the target and then run the steps above.


5   . mvn clean install

Clean target folder and Installs it to the local Maven repository.

6   . mvn compile

compile your source code for errors

7   . mvn test

run your test cases


8   . mvn -Dtest=TestClass#abcMethod test

Run specific test case of the class 

TestClass is the test class name 

abcMethod is the test method


9   . mvn clean install -DskipTests=true -Dmaven.test.failure.ignore=true

Skip all test cases.




Thursday 17 January 2013

org.hibernate.mappingexception unknown entity

Problem exception :

org.hibernate.mappingexception unknown entity


Solution:

Your entity is not correctly annotated, you must use the correct annotation .Check your imports.

Correct:
@javax.persistence.Entity .



Wrong:
 @org.hibernate.annotations.entity

Spring boot with CORS

CORS (Cross-Origin Resource Sharing) errors occur when a web application running in a browser requests a resource from a different domain or...