Friday, March 14, 2014

Spring Java Script Decorator Message Internationalization



How do I achieve internationalization when using Spring Java Script Decorator?
See the following example for the answer. Note that text.invalid.coordinates should be defined in a message.properties file where the JSP can access.

        <div class="sp-field">
                                 <form:input cssClass="sp-textfield" path="missingNetworkNodeList.elements[${rowCounter.index}].coordinates.latitudeLongitude" />
                                 <spring:message code="text.invalid.coordinates" var="invalidCor"/>
                                 <script type="text/javascript">
                                      Spring.addDecoration(new Spring.ElementDecoration({
                                           elementId : "missingNetworkNodeList.elements${rowCounter.index}.coordinates.latitudeLongitude",                                      
                                           widgetType : "dijit.form.ValidationTextBox",
                                           widgetAttrs : {
                                               invalidMessage: "${invalidCor}",
                                               regExp: "^-?([0-8]?[0-9])\.[0-9]{1,6},-?((1?[0-7]?|[0-9]?)[0-9])\.[0-9]{1,6}"
                                           }
                                      }));
                                </script>                               
       </div>



Tuesday, January 7, 2014

Tiles Definition: Nested



The following is an example of nested Tiles Definition:

<definition name="myapp.homepage.body" template="/layouts/three_rows.jsp">
  <put-attribute name="one" value="/tiles/headlines.jsp" />
  <put-attribute name="two" value="/tiles/topics.jsp" />
  <put-attribute name="one" value="/tiles/comments.jsp" />
</definition>

<definition name="myapp.homepage" template="/layouts/classic.jsp">
  <put-attribute name="title" value="Tiles tutorial homepage" />
  <put-attribute name="header" value="/tiles/banner.jsp" />
  <put-attribute name="menu" value="/tiles/common_menu.jsp" />
  <put-attribute name="body" value="myapp.homepage.body" />
  <put-attribute name="footer" value="/tiles/credits.jsp" />
</definition>

The following apache link provides some very good information on Tiles Definition.
http://tiles.apache.org/framework/tutorial/advanced/nesting-extending.html

JSP EL


A primary feature of JSP technology version 2.0 is its support for an expression language (EL). An expression language makes it possible to easily access application data stored in JavaBeans components. For example, the JSP expression language allows a page author to access a bean using simple syntax such as ${name} for a simple variable or ${name.foo.bar} for a nested property. 

for more information, check out this EL tutorial:

Friday, December 20, 2013

DIV with Vertical Scroll Bar



How to create a DIV with a scroll bar? 
 
<div style="width: 200px; height: 100px; overflow-y: scroll;">
 




 

Wednesday, December 18, 2013

Hibernate based JPA Query


Can I use a direct SQL query to retrieve results in hibernate based JPA?
the answer is YES.
Below is some example code I wrote in a DAO class:

    @SuppressWarnings("unchecked")
    @Override
    public List<String> getSiteSubTypes() {
        EntityManager em = super.getJpaTemplate().getEntityManagerFactory().createEntityManager();
        Session session = (Session) em.getDelegate();       
       
        SQLQuery sqlQuery = session.createSQLQuery("SELECT DISTINCT siteSubType  FROM portMapping");
       
     
        @SuppressWarnings("rawtypes")
        List result = sqlQuery.list();       
           
        List<String> listResult = (List<String>) result;
 

        return listResult;
    }   

   

In the above example, I am doing something really simple. Get the list of siteSubType distinctive values.


For more SQL query examples, see this link: http://code.google.com/p/simplejpa/wiki/JPAQuery

I copied a few here:

Examples

Basic query
query = entityManager.createQuery("select o from MyTestObject o where o.income = :income and o.age = :age");
query.setParameter("income", 50507.0);
query.setParameter("age", 12);
List<MyTestObject> obs = query.getResultList();
Query with referenced object filter (@ManyToOne)
query = entityManager.createQuery("select o from MyTestObject o where o.anotherObject = :anotherObject");
query.setParameter("anotherObject", anotherObject);
List<MyTestObject> obs = query.getResultList();
Which is equivalent to:
query = entityManager.createQuery("select o from MyTestObject o where o.anotherObject.id = :anotherObjectId");
query.setParameter("anotherObjectId", anotherObject.getId());
List<MyTestObject> obs = query.getResultList();
Can also query down object graph (1 level only right now):
query = entityManager.createQuery("select o from MyTestObject o where o.someOtherObject.name = :name");
query.setParameter("name", "finnigan");
List<MyTestObject> obs = query.getResultList();
But keep in mind, querying down the graph can be slow because it will query for the id's down the graph, then apply them to the top level query.
Like Query:
query = em.createQuery("select o from MyTestObject3 o where o.someField3 like :x");
query.setParameter("x", "fred and%"); List<MyTestObject3> obs = query.getResultList();
This will return all MyTestObject3's that start with "fred and".
Sort query:
query = entityManager.createQuery("select o from MyTestObject o where o.income = :income and o.age = :age order by o.age");
query.setParameter("income", 50507.0);
query.setParameter("age", 12);
List<MyTestObject> obs = query.getResultList();

Count:

Count returns a result list with a single entry that contains a Long.
query = entityManager.createQuery("select count(o) from MyTestObject o where o.income = :income and o.age = :age order by o.age");
query.setParameter("income", 50507.0);
query.setParameter("age", 12);
List obs = query.getResultList();
long count = obs.get(0);

QueryBuilder

QueryBuilder helps with creating parameterized queries.
Example usage:
// the one liner
Query qb = new QueryBuilderImpl()
   .append("select o from IndexStats o where o.numKeywords < :x", "x", 10).makeQuery(em);
   List<IndexStats> resultList = qb.getResultList();
private List<Book> getBooks(BookFilter filter) {
    QueryBuilder qb = new QueryBuilderImpl();
    qb.append("select o from Book o where 1 = 1");
    if (filter.getTitle() != null) {
        qb.append(" and o.title = :title", "title", filter.getTitle());
    }
    Query q = qb.makeQuery(em());
    return q.getResultList();
}

JSP Dropdown Example



 The following example displays:
(1)a default option: ---Select--- , means empty input.
(2) options to choose from. Option values are obtained from a list ${technologyList}. JSTL is used to retrieve and pass the values to <form:option> tag.

                         <span class="sp-fieldlabel">Sub-Type</span>

                        <form:select path="searchCriteria.subType" cssClass="sp-select">
                            <option value="-1">
                                      --<spring:message code="generic.dropdown.selection" />--
                            </option>

                            <c:forEach items="${SiteSubTypes}" var="element">
                                <form:option value="${element}">${element}</form:option>
                            </c:forEach>
                        </form:select>

In the above code example,   <spring:messagecode="generic.dropdown.selection"/>  uses Spring's localization feature. Below shows the results of the above JSP:

Friday, November 29, 2013

JSP vs.HTML

(1)JSP simply puts Java inside HTML pages.

 You can take any existing HTML page and change its extension to ".jsp" instead of ".html".

Now load the new file, with the ".jsp" extension, in your browser. You will see the same output, but it will take longer! But only the first time. If you reload it again, it will load normally. What is happening behind the scenes is that your JSP is being turned into a Java file, compiled and loaded.
This compilation only happens once, so after the first load, the file doesn't take long to load anymore. (But everytime you change the JSP file, it will be re-compiled again.)

 (2)can enclose Java expressions and java code (called a scriplets), which are evaluated at run time. use <%= and %>

 It is true that JSPs are primarily HTML with embedded Java code. However, they have many advantages, such as requiring less (typically none) configuration, and being easy to modify and deploy. Therefore they can lend themselves to other usages, such as writing simple and straightforward controllers.

 JSP's eventually are turned into servlets, and though JSPs are much simpler than servlets, for some cases servlets are still useful.