Thursday, March 29, 2012

different scopes of managed bean in JSF

Managed beans can have different scopes. They are as:

1. Request
2. Session
3. Application
4. None

A scope of request means that the bean is only available in a single HTTP request. Managed beans can also have session scope, in which case they are available in a single user's HTTP session. A scope of application means that the bean is accessible to all users in the application, across user sessions. Managed beans can also have a scope of none, which means that the managed bean is not stored at any scope, but is created on demand as needed. Additionally, managed beans can have a scope of view, in which case the bean is available until the user navigates to another page. View scoped managed beans are available across Ajax requests.

Wednesday, March 28, 2012

PROJECT_STAGE in configuration file of JSF project

when we start a JSF project using netbeans, we get a skleton project containing two files at first.

a single facelet file called index.xhtml,
a web.xml configuration file


web.xml is the standard, optional(from version 3.0) configuration file needed for Java web.

If we observer we find that the NetBeans automatically sets the JSF project stage to Development, setting the project stage to development configures JSF to provide additional debugging help not present in other stages.

The following are the valid values for the javax.faces.PROJECT_STAGE context parameter for the faces servlet:

1.Development
2. Production
3.SystemTest
4.UnitTest

Development project stage adds additional debugging information to ease development. The Production project stage focuses on performance. The other two valid values for the project stage (SystemTest and UnitTest), allow us to implement our own custom behavior for these two phases.

The javax.faces.application.Application class has a getProjectStage() method that allows us to obtain the current project stage. Based on the value of this method, we can implement the code that will only be executed in the appropriate stage.

Wednesday, March 21, 2012

Line limit for Text Area

In a HTML page, text area is used to accept multiple line texts at once. But some times, we may need to fixed the no. of line in a text area of web page. There is not attribute available for this purpose. This will have to implemented using javascript. The required function to check the no. of line as well as the no. of characters in a text area may be as:

<script type="text/javascript">

var alert_title='Input Restriction';

function limitTextarea(textarea,maxLines,maxChar){
var lines=textarea.value.replace(/\r/g,'').split('\n'),
lines_removed,
char_removed,
i;
if(maxLines&&lines.length>maxLines){
//alert('You can not enter\nmore than '+maxLines+' lines');
lines=lines.slice(0,maxLines);
lines_removed=1
}
if(maxChar){
i=lines.length;
while(i-->0)if(lines[i].length>maxChar){
lines[i]=lines[i].slice(0,maxChar);
char_removed=1
}
//if(char_removed)alert('You can not enter more\nthan '+maxChar+' characters per line')
}
if(char_removed||lines_removed)textarea.value=lines.join('\n')
}

function watchTextarea(){
document.getElementById('resticted').onkeyup()
}

function set_ie_alert(){
window.alert=function(msg_str){
vb_alert(msg_str)
}
}
</script>

and the above function can be called as

</script>
</head>
<body>
<textarea onkeyup="limitTextarea(this,6,40)" style="width:400px;height:200px" wrap="off" id="resticted" onfocus="focus_watch=setInterval('watchTextarea()',250)" onblur="clearInterval(focus_watch)">some text</textarea>
</body>
</html>

why JSF?

Java Server Faces technology , developed by Java Community Process(JCP) ,simplifies the development of Java Based web applications.
Java Server Faces is a component oriented and event driven framework for web applications. JSF eases the development of GUI for web applications. JSF allows the programmers to work with extensible user interfaces like buttons, text boxes, check boxes etc... Programmer writes the code for particular event such as button clicked. This makes programming much easier and now the there is no need to write request and response processing logic.

Main feature of JSF is ease of use. Developing web applications is easier and faster than other frameworks like struts because JSF supports UI components and easy event handling.JSF is based on well established Model-View-Controller (MVC) design pattern. Applications developed using JSF frameworks are well designed and easy to maintain then any other applications developed in JSP and Servlets.

JSF includes mainly:
Set of APIs to represent and manage state of components that helps server side validation, event handling, page navigation, data conversion etc.
JSP custom tag library to create UI components in a view page.

The UI (user interface) created using JSF technology runs on server and output is shown to the client. Goal of JSF is to create web applications faster and easier. Developers can focus on UI components, events handling, backing beans and their interactions rather than request, response and markup. JSF hides complexities to enable developers to focus on their own specific work.

Monday, March 12, 2012

fonts problem in jasper report for pdf

when we export jaspert report to pdf format, we face problem with the font of the file. As, jasper report deal with its own true type font only for pdf. For this reason, we need to use the steps of font extension. This can be done following the steps below:

Ingredients: Jar file of jasperreports-fonts-v.v.v.(v stand for Version), true type fonts

1. Download the jasperreports-fonts-v.v.v(i.e jasperreports-fonts-4.0.1) jar file to modify according your requirements.
2.Right click on the jar file and select Extract to jasperreports-fonts-v.v.v.
3.At the root directory of the extracted folder, there is a properties file named: jasperreports_extension.properties

The file contains:
net.sf.jasperreports.extension.registry.factory.simple.font.families=net.sf.jasperreports.engine.fonts.SimpleFontExtensionsRegistryFactory
net.sf.jasperreports.extension.simple.font.families.dejavu=net/sf/jasperreports/fonts/fonts.xml

For the practise, here we will try to add the Times New Roman fonts to this jar. So, now we will have to add the following lines at the above file.
net.sf.jasperreports.extension.simple.font.families.timesnewroman=net/sf/jasperreports/fonts/fonts.xml

After adding we will get the file as:

net.sf.jasperreports.extension.registry.factory.simple.font.families=net.sf.jasperreports.engine.fonts.SimpleFontExtensionsRegistryFactory
net.sf.jasperreports.extension.simple.font.families.dejavu=net/sf/jasperreports/fonts/fonts.xml
net.sf.jasperreports.extension.simple.font.families.timesnewroman=net/sf/jasperreports/fonts/fonts.xml

4.Now, create the specific location to save the font collection. for this purpose, we create a directory in the location of ~/jasperreports-fonts-v.v.v\net\sf\jasperreports\fonts\. The name of the file will be timesnewroman for our case.

5. Now we need to add the collections of true type fonts at the above newly created location. Generally four files will be added as the representative of four categories: normal, bold, italic, bold italic.

6. Now its the time to modifying the fonts.xml file. We need to add the definition of the font to tis xml file. For our case, the definition will be as:

<fontFamily name="Times New Roman">
<normal>net/sf/jasperreports/fonts/timesnewroman/times.ttf</normal>
<bold>net/sf/jasperreports/fonts/timesnewroman/timesbd.ttf</bold>
<italic>net/sf/jasperreports/fonts/timesnewroman/timesi.ttf</italic>
<boldItalic>net/sf/jasperreports/fonts/timesnewroman/timesbi.ttf</boldItalic>
<pdfEncoding>Identity-H</pdfEncoding>
<pdfEmbedded>true</pdfEmbedded>
<exportFonts>
<export key="net.sf.jasperreports.html">'Times New Roman', Arial, Helvetica, sans-serif</export>
<export key="net.sf.jasperreports.xhtml">'Times New Roman', Arial, Helvetica, sans-serif</export>
</exportFonts>
<!--
<locales>
<locale>en_US</locale>
<locale>de_DE</locale>
</locales>
-->
</fontFamily>

7. after completion of customization, again we will make the jar from the raw files. Entering into the extracted folder, select all the files. right clcik and select the option Add to Archive. select the type to ZIP. At the end of the name, change the extension to .jar.

8. After that use this jar where you want.



--
For converting code to show on blog, i have used http://centricle.com/tools/html-entities/