Wednesday, 12 August 2015

Select Radio button from a tr on click


If you click on tr (any row) also radio button will select 

<script type="text/javascript"src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>


<table border="2" cellpadding="5">
      <tr>
      <td>1 </td><td>Java </td><td><input type="radio" id="one" name="numbers">One</input></td>
      </tr>
      <td>2 </td><td>JQuery </td><td><input type="radio" id="two" name="numbers">Two</input></td>
      </tr>
      <td>3 </td><td>Ajax </td><td><input type="radio" id="three" name="numbers">Three</input></td>
      </tr>
</table>
<script>
$(document).ready(function() {

$("tr").bind("click",function () {

$(this).find('td input:radio').prop('checked', true);

});

});
</script>

Thursday, 6 August 2015

Spring @RequestMapping Annotation

=> Based on this Annotation only Request will come to any method in the Controller.

=> We can mention that , the method is supporting either POST request or GET request

=> We can mention the value of the url 
(Ex : http://ip:port/projName/..../value)
For each method we have to differentiate the value

=> We can append variables with URL as Path Variables

=> We can append variables with URL as Request Parameters

Annotation 

@RequestMapping

import org.springframework.web.bind.annotation.RequestMapping;

How to specify the method is only supports either POST / GET ?

@RequestMapping(value="/callController1", method=RequestMethod.GET)

@RequestMapping(value="/callController1", method=RequestMethod.POST)

@RequestMapping(value="/callController1", method={RequestMethod.GET,RequestMethod.POST})
[It will support both GET and POST Requests]


@PathVariable Example

@RequestMapping(value="/callController2/{name}",method=RequestMethod.GET)

public ModelAndView method2(@PathVariable("name") String person) {
           // Write the Code whatever you want
}

Ex URL : http://localhost:8888/SpringMVC/spring/callController2/Ram

For More Path variables 

@RequestMapping(value="/callController2/{name}/one/{salary}",method=RequestMethod.GET)

Ex URL : http://localhost:8888/SpringMVC/spring/callController2/Ram/one/50000

@RequestParam with Params attribute

@RequestMapping(value="/callController3",params="id",method=RequestMethod.GET)
public ModelAndView method3(@RequestParam("id") String uid) {
// Code

}

Ex URL : http://localhost:8888/SpringMVC/spring/callController3?id=25

For Multiple Request Parameters

@RequestMapping(value="/callController5",params={"id","name"},method={RequestMethod.GET,RequestMethod.POST})
public ModelAndView method5(@RequestParam("id") String uid,@RequestParam("name") String name) {
// Code

}

Ex URL : http://localhost:8888/SpringMVC/spring/callController5?id=25&name=Ram


Example :

package com.ram;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class SpringMvcController {

//http://localhost:8888/SpringMVC/spring/springController
//By default it will support GET method
@RequestMapping(value="/springController")
public String method() {

return "returnPage";

}

//http://localhost:8888/SpringMVC/spring/callController1
@RequestMapping(value="/callController1",method=RequestMethod.GET)
public String method1(ModelMap model) {

model.addAttribute("key1", "Hi Ram");

return "returnPage1";

}

//http://localhost:8888/SpringMVC/spring/callController2/Ram
@RequestMapping(value="/callController2/{name}",method=RequestMethod.GET)
public ModelAndView method2(@PathVariable("name") String person) {

ModelAndView model = new ModelAndView();
model.setViewName("returnPage2");
model.addObject("msg1", person);
model.addObject("msg2", "This is Message2");

return model;

}

//http://localhost:8888/SpringMVC/spring/callController3?id=25
//It will suppoert Both POST and GET
@RequestMapping(value="/callController3",params="id",method={RequestMethod.GET,RequestMethod.POST})
public ModelAndView method3(@RequestParam("id") String uid) {

ModelAndView model = new ModelAndView();
model.setViewName("returnPage3");
model.addObject("msg1", uid);

return model;

}



//http://localhost:8888/SpringMVC/spring/callController4
@RequestMapping(value="/callController4",method=RequestMethod.GET)
public ModelAndView method4() {

ModelAndView model = new ModelAndView("returnPage4");
model.addObject("msg5", "Hi Ram");

return model;

}

//http://localhost:8888/SpringMVC/spring/callController5?id=25&name=Ram
//It will suppoert Both POST and GET
@RequestMapping(value="/callController5",params={"id","name"},method={RequestMethod.GET,RequestMethod.POST})
public ModelAndView method5(@RequestParam("id") String uid,@RequestParam("name") String name) {

ModelAndView model = new ModelAndView();
model.setViewName("returnPage5");
model.addObject("id", uid);
model.addObject("name", name);

return model;

}

}

spring-servlet.xml

<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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.2.xsd">

<context:component-scan base-package="com.ram" />

<mvc:annotation-driven />

<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>

</beans>


Web.xml

<web-app> <display-name>Spring MVC</display-name> <welcome-file-list> <welcome-file>home.jsp</welcome-file> </welcome-file-list>
<servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/spring/*</url-pattern> </servlet-mapping> </web-app>

returnPage.jsp

<html> <body> <h1>Spring MVC</h1> </body> </html>

returnPage1.jsp

<html> <body> <h1>Spring MVC</h1> <h2>${key1}</h2> </body> </html>

returnPage2.jsp

<html> <body> <h1>Spring MVC</h1> <h2>${msg1}</h2> <h2>${msg2}</h2> </body> </html>

returnPage3.jsp

<html> <body> <h1>Spring MVC</h1> <h2>${msg1}</h2> </body> </html>

returnPage4.jsp

<html> <body> <h1>Spring MVC</h1> <h2>${msg5}</h2> </body> </html>

returnPage5.jsp

<html> <body> <h1>Spring MVC</h1> <h2>Id : ${id}</h2> <h2>Name : ${name}</h2> </body> </html>







Wednesday, 5 August 2015

JQuery Device Detector [Mobile Device]

JQuery Device Detector [Mobile Device / IPad / IPhone / Other]

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
            $(document).ready(function() {
                     fun();
            });
</script>

<script>
function fun() {

var device = navigator.userAgent;
var checker = {
      microsoft: device.match(/IEMobile/),
      ipad: device.match(/(iPad)/),
     iphone: device.match(/(iPhone)/),
     blackberry: device.match(/BlackBerry/),
     android: device.match(/Android/)
   };
   if (checker.microsoft){
    alert("Microsoft");
   }
   else if(checker.ipad) {
   alert("ipaD");
   }
   else if (checker.iphone){
       alert("iphone...");
   }
   else if (checker.blackberry){
       alert("BlackBerry");
   }
   else if(checker.android) {
    alert("Android");
 }
   else {
       alert("Unknown Device/Desktop .... ");
   }
};
</script>
</head>
<body>


</body>
</html>

Tuesday, 4 August 2015

JQuery Back button [Go to previous page]

For Go to previous page use following method

parent.history.back();

[OR]

document.referrer;

A.html

<html>
<body>
From Page A
<a href="C.html">Next</a>
</body>
</html>

B.html

<html>
<body>
From Page B
<a href="C.html">Next</a>
</body>
</html>

C.html

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>

$(document).ready(function(){
   $("#back").click(function() {
parent.history.back();
});
});


 /* $(document).ready(function(){
   $("#back").click(function() {
   var previousPage =  document.referrer;
window.location.href = previousPage;
});

}); */

</script>
</head>
<body>
From Page C ...
<button id="back">Back</button>
</body>
</html>