Search This Blog

Thursday, March 21, 2013

How to use .jsf extension in URLs?


The .jsf extension is where the FacesServlet is often by default mapped on in the web.xml.
<servlet-mapping>
    <servlet-name>facesServlet</servlet-name>
    <url-pattern>*.jsf</url-pattern>
</servlet-mapping>
The .xhtml extension is of the actual Facelets file as you've physically placed in the webcontent of your webapp, e.g. Webapp/WebContent/page.xhtml.
If you invoke this page with the .jsf extension, e.g. http://localhost:8080/webapp/page.jsfthen the FacesServlet will be invoked, locate the page.xhtml file and parse/render its JSF components. If the FacesServlet isn't invoked, then the enduser would end up getting the raw XHTML source code (which can be seen by rightclick, View Source).
Sometimes a *.faces extension or /faces/* foldermapping is been used. But this was from back in the JSF 1.0/1.1 ages. You're free to choose and use whatever mapping you'd like to let FacesServletlisten on, even if it's a nothing-saying *.xyz. The actual page itself should always have the .xhtmlextension, but this is configureable by the following <context-param> in web.xml:
<context-param>
    <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
    <param-value>.xml</param-value>
</context-param>
This will change the FacesServlet to locate page.xml instad of (default) page.xhtml.
More recently, with JSF/Facelets 2.0 a *.xhtml mapping is been used. In JSF/Facelets 1.x it was not possible to use the same mapping extension as the physical file. It would result in an infinite loop. But since JSF/Facelets 2.0 it is possible and this allows you to call the page byhttp://localhost:8080/webapp/page.xhtml.
<servlet-mapping>
    <servlet-name>facesServlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
This way you don't need to configure some security restrictions to hide the raw source files away for cases whenever the enduser changes for example .jsf in URL to .xhtml in browser address bar.

See also:

Sunday, March 17, 2013

What are the fundamental differences between Struts and JSF

from
http://struts.apache.org/release/2.0.x/docs/what-are-the-fundamental-differences-between-struts-and-jsf.html

Routing around the hyperbole and actually trying to answer the question...
The key differences between the two are the base paradigms that underly each platform.
Specifically, JSF is a "component" framework whereas Struts is an "action" framework.
What does that mean?
In a component framework, artifacts that are rendered on the page are initially developed as individual components, much like in modern GUI "fat client" libraries. You have components, they have events, and your code is written to work with those events against the components.
Most of the time, in mainstream development, your code is pretty much ignorant of the HTTP request cycle and processing.
Struts (both 1 and 2) are action frameworks. In essence they give you the ability to map URLs to activities and code on the back end. Here, the layout and workflow tends to be more page oriented. As a developer you tend to interact with the HTTP request cycle directly, though Struts 2 helps isolate at least the binding of the request data to the action implementation classes.
Action frameworks tend to be much "thinner" in how they stand between your code and the raw HTTP request compared to component frameworks.
For those people just cutting their teeth on web development, the component frameworks (especially with good tooling) can be much more approachable, as the tools tend to hide the heavy weight nature of the component frameworks. But, on the downside, their size and their "square peg in to round hole" nature of turning HTTP requests in to the rough equivalent of mouse clicks mean there's more complexity to try to understand if and when the framework starts misbehaving or getting in the way.
But, to be fair, frameworks like ASP.NET and JSF are popular because novices can get quick success with them with the modern tools.
Old School HTTP wranglers may simply be more comfortable with action frameworks, simply because they've been through the crucible of understanding how HTTP requests are structured. Action frameworks tend to work better with "pretty" urls (though component frameworks are getting better at his). Action framework coders can have more control of the structure and presentation of URLs, since their systems are more intimately tied to them compared to a component framework.
As a basic guideline, I find the action frameworks are better for "web sites", site like this one, sites that focus on delivering content to the user. Where it's mostly a "read only" experience for the end user who is likely to want to bookmark things, come back to arbitrarily deep pages, etc.
But the component frameworks are better for "web apps". CRUD screens, back office applications, lots of forms and controls, etc. Here, the workflow is more controlled. You tend to not get to a "detail" screen with going through the "list" screen or "header" screen first, for example.
It's nice to be able to just drag and drop a grid component on to a form, add a couple of buttons and point it as a DB table to get "instant" results. But these apps don't bookmark well, HATE the "refresh button", may behave poorly with the back button, etc. Overall, they can not be very good citizens when working with the web browser.
So, if it were me, I'd write a blog in a action framework like Struts 2 (or even better, Stripes), but I'd write an accounting package in JSF.

Saturday, March 16, 2013

Formatter and Beautifier tools


JSON , HTML , XML , SQL formatter tools
http://www.freeformatter.com/
JAVA formatter plus conver formatter to html
http://www.togotutor.com/

and last a very popular formatter syntax higlighter
SyntaxHighlighter is currently used by ApacheAptanaMozillaYahooWordpressBug Labs,Freshbooks and others.

 http://alexgorbatchev.com/SyntaxHighlighter/

Thursday, March 14, 2013

Order HashTable Or Vector ; HashMap TreeMap



/*Now remember you can use this in mvc frame work even hibernate lets say you have lookups listener to load lookups on deploy to servlet context instead of using javascript ordering on each client machine you can do this on deployment phase this way you will save alot of overhead on page request ... */

1-----------------the table search method (you can get result in any way you want ; google it like this one here
mysql get db results)

public static void loadGMajor(ServletContext servletContext) {  
        try {  
             /*the below method to hit database and get back with 
             hash map of  
             key value pairs e.g  
             it will look something like this ...
             for illustration only insid SearchGmajorLookup
            while (theResultSet.next()) {
             LkGmajor lkGmajor = new LkGmajor();

             lkGmajor.setMajor(theResultSet.getString("MAJOR"));
             lkGmajor.setMajorD(theResultSet.getString("MAJOR_D"));
             lkGmajor.setMajorEd(theResultSet.getString("MAJOR_ED")); 
                 
             //result hashmap returned 
             results.put(lkGmajor.getMajor(), lkGmajor);
            }*/
 

            HashMap gMajorHT = lookupsDAO.SearchGmajorLookup(params);  
              

     HashMap<String, LkGmajor> map = (HashMap<String, LkGmajor>)gMajorHT;  
            ValueComparator bvc = new ValueComparator(gMajorHT);  
            TreeMap<String, LkGmajor> sorted_map = new TreeMap(bvc);  

            //System.out.println("unsorted map");  
            /*for (String key : map.keySet()) {
            System.out.println("key/value: " + key + "-" + map.get(key));
            }*/
  

            sorted_map.putAll(map);  

            /*System.out.println("results");
            for (String key : sorted_map.keySet()) {
            System.out.println("key/value: " + key + "-" + sorted_map.get(key));
            }*/
  

//saving the Ordered TreeMap In servlet Context now you can use JSP tag for each to loop it and //display
            servletContext.setAttribute("gMajorHT", (TreeMap<String, LkGmajor>) sorted_map); 
              

        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    } 



2-----------the Bean implements Comparable Interface


import java.io.Serializable; 

/**
 *
 * @author shareef hiasat
 */
 
public class LkGmajor implements Serializable, Comparable<LkGmajor> { 

    private String major; 
    private String majorD; 
    private String majorEd; 

    public LkGmajor() { 
        major = ""
        majorD = ""
        majorEd = ""
    } 

    public LkGmajor(String major) { 
        this.major = major; 
    } 

    public LkGmajor(String major, String majorD) { 
        this.major = major; 
        this.majorD = majorD; 
    } 

    public String getMajor() { 
        return major; 
    } 

    public void setMajor(String major) { 
        this.major = major; 
    } 

    public String getMajorD() { 
        return majorD; 
    } 

    public void setMajorD(String majorD) { 
        this.majorD = majorD; 
    } 

    public String getMajorEd() { 
        return majorEd; 
    } 

    public void setMajorEd(String majorEd) { 
        this.majorEd = majorEd; 
    } 

    @Override 
    public int hashCode() { 
        int hash = 0; 
        hash += (major != null ? major.hashCode() : 0); 
        return hash; 
    } 

    @Override 
    public boolean equals(Object object) { 
        // TODO: Warning - this method won\'t work in the case the id fields are not set 
        if (!(object instanceof LkGmajor)) { 
            return false
        } 
        LkGmajor other = (LkGmajor) object; 
        if ((this.major == null && other.major != null) || (this.major != null && !this.major.equals(other.major))) {
            return false
        } 
        return true
    } 

/*this metho is the corner stone here it will compare 2 object of the same type depending on a private member such majorD here in our case*/


public int compareTo(LkGmajor o) { 


        if (o.getMajorD() != null && this.getMajorD() != null) { 
            if (o.getMajorD().equals(this.getMajorD())) { 
                return -1; 
            } else { 
                return this.getMajorD().compareTo(o.getMajorD()); 
            } 
        } else { 
            return 0; 
        } 
    } 
}






3----------The Most Important Part the Class Comparator


import java.util.Comparator; 
import java.util.Map; 


/**
 *
 * @author shareef hiasat
 */
 
public  class ValueComparator implements Comparator{ 

    Map base; 
  public ValueComparator(Map base) { 
      this.base = base; 
  } 

  public int compare(Object a, Object b) { 

      return compareValue(a, b); 
  
  } 

  private <E extends Comparable>int compareValue(Object a, Object b) 
  { 
      return ((E)base.get(a)).compareTo((E)base.get(b)); 
  } 



Its Simple Right But I searched the net and after many reading i built this not taken from any one so please dont foget to refer the link and do not copy my hard work and saying its yours :)


Thanks Java open source

//example of using you now in jsp
<td height="30" >
   <select id="ddlMajor" name="ddlMajor" style="width:160px">
      <option value="-1">Select</option>
      <c:forEach var="major" items="${gMajorHT}">
         <option value="${major.key }">${major.value.majorD}</option>
      </c:forEach>
   </select>
</td>

Wednesday, March 13, 2013

How to make war file in eclipse

How Easy Was That !

File -> Export -> Web -> WAR file

Java: iterate through HashMap


If you're only interested in the keys, you can iterate through the keySet() of the map:
Map<String, Object> map = ...;

for (String key : map.keySet()) {
    // ...
}
If you only need the values, use values():
for (Object value : map.values()) {
    // ...
}
Finally, if you want both the key and value, use entrySet():
for (Map.Entry<String, Object> entry : map.entrySet()) {
    String key = entry.getKey();
    Object value = entry.getValue();
    // ...
}