Tuesday, December 27, 2011

JSF with JQurey for Clientside Validations

Step1: Writing JSF managed beans

package beans;
public class UserBean {     

private String username;
public UserBean() { }
public String getPassword() {return password;}
public void setPassword(String password) {this.password = password;}

}
 Step2: Writing faces-config.xml

<managed-bean>
<managed-bean-name>userBean</managed-bean-name>
<managed-bean-class>beans.UserBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
Step3: JSF Form with JQuery
(We are validating username field)

<%@page contentType="text/html" %>de"> <%@page contentType="text/html" %>
<script language="javascript" src="jquery-1.4.2.js"></script>
        

<script language="javascript">
function checkUsername(){
$.get( "checkusername.jsp",{username : $("#registerform\\:username").val()},updateUsername);
}
function updateUsername(response) {
if (response) {
$("#usernameresult").text(response);
// update SPAN item with result
}
        </script>          

<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<f:view>
<h2>Registration </h2>
<h:form id="<b><font color=">registerform">
Username : <h:inputtext id="<b><font color=">username" value="#{userBean.username}" required="true" onblur="checkUsername()" /></h:inputtext>
</h:form>
</f:view>
</pre>
               
               usernameresult" /> 

              

lt;/f:view>

The most important thing is to know how to access JSF component from JQuery.

The id given to inputText is consisting of formid:componentid. So in this example the id given to textbox is registerform:username. But the presence of : (colon) causes problem to JQuery. So, we need to escape : (colon) using two \\ characters before colon - registerform\\:username.


Step 4: checkusername.jsp ( to check the user is valid)

<%@ page import="java.sql.*"  contentType="text/plain"%> <%  String username = request.getParameter("username");  // sent from client  // connect to oracle using thin driver  Class.forName("oracle.jdbc.driver.OracleDriver"); 
try{  Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","youruser","yourpassword");  PreparedStatement ps = con.prepareStatement("select username from users where username = ?");  ps.setString(1,username);  ResultSet  rs = ps.executeQuery();  if ( rs.next()) { // found username     out.println("Username is already present!");  // send this to client as response   }
}catch(Exception e){e.printStackTrace();}
finally{
try{  rs.close();  ps.close();  con.close();
}catch(Exception e) {}
%>

Tuesday, December 13, 2011

Oracle Help

  • How to Find a Child Tables List from the Parent Table?

SELECT table_name, constraint_name FROM user_constraints
WHERE r_constraint_name in
(SELECT constraint_name FROM user_constraints WHERE table_name = 'PARENT_TABLE' )
ORDER BY Table_name ;

Friday, December 9, 2011

PrimeFaces 3.0.M4 is integrated with JQuery Mobile on JSF2.0

  • 100+ Readily available RICH Components

  • Configurable Themes

  • Ajax PUSH

  • Mobile TouchFaces built with JQuery mobile

  • Light Weight

  • Easy to use

  • Standard based development