Lesson 6: Handling Request Parameters with Form Beans #Java Short Notes

Training Video for this article
Code used for this article

  • In jsp pages, you can create forms with html form tag or html:form tag
  • Form Beans concept
    • You can use request.getParameter() to retrieve data from a form from the backend/server side. Or you can create a bean based on the form parameters/values. Then from the server side, you can retrieve values from these beans.
    • From server side, you can set output values to the request object directly or you can create a bean to carry output messages to the next step and set the output bean in the request/session object.
    • In the output jsp pages, you can use bean:write or jsp expression language to print those output/result bean values [provided that output values are set into a bean in the server side]
    • How to declare form beans: Just define a simple Java class that extends ActionForm and then declare it in the struts-config.xml file with form-beans tag
  • Steps in creating a struts application. This example will follow the same rules
    1. Modify the struts-config.xml file: declare the form beans, map incoimg .do to action classes, map all possible return strings from action calsses to destination pages
    2. Write the class for the bean that extends ActionForm. Write all the getter and setter methods, write reset method [useful for session scoped beans], write validate method for form input validation
    3. Create the result/output beans. You do not need to declare them in struts-config.xml file
    4. Define the action class to handle requests and return the right forward string that also need to be mapped in the config file
    5. Create the input pages that calles the actions
    6. Write output jsp pages

From: http://sitestree.com/?p=5009
Categories:Java Short Notes
Tags:
Post Data:2010-02-10 16:35:25

Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada

JSF: Lesson – 7: Using Message Bundles in JSF: Support Internationalization #Java Short Notes

  • Target: Intermediate level web-developers (Java-platform).
  • Sample application on Internationalization with JSF
  • Video Tutorial on Internationalization with JSF
  • To support Internationalization, in the web-pages, the text messages and the control texts make use of the keys for the values/messages. In the value fields, keys are mentioned/provided as the values. Later, based on the current locale/language, the keys are replaced with the appropriate texts for the current locale/language
  • In some property files, keys and their corresponding values are listed
  • For each language, one property file is provided
  • The file names usually end with .properties
  • Example: contents of the Property files:
    search.text.new=New SearchorderBtn.text = Confirm Order
  • Wherever orderBtn.text is used for values, will be replaced with “Confirm Order”
  • If the file name is messages.properties, the file can be loaded into the JSP pages as follows:
    
    
  • Example use of the property file from JSP pages

  • You are also required to list the default languages (that your application supports) in the faces-config.xml file

From: http://sitestree.com/?p=4998
Categories:Java Short Notes
Tags:
Post Data:2010-04-09 07:54:07

Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada

JSF: Lesson – 6: Validating User Input in JSF #Java Short Notes

  • Target: Intermediate level web-developers.
  • Sample application for this article
  • Video Tutorial for this article
  • In web-application development, validating user inputs takes much efforts. JSF has made validations much easier than usual
  • JSF Built-in Validators:
    • validateDoubleRange: Checks that the value provided is a double value. You can also set a minimum and a maximum.
    • validateLongRange: Validates the input to be a long. It also has the optional minimum and maximum parameters.
    • validateLength: Validates the length of a string
    • Without the minimum and the maximum values these validators actually do nothing
  • You can also enforce a value as follows. If no value is provided, an error message is displayed
    
    
  • You can also create your own custom validator that should implement javax.faces.validator.Validator interface.
  • You have to implement the validate() method: validate(FacesContext arg0, UIComponent arg1, Object arg2) throws ValidatorException
  • In the error condition, you should throw ValidatorException from the validate method
  • Then you need to register the custom validator in the faces-config.xml file using a id as follows:
        currency.validator    net.justetc.jsf.CurrencyValidator  
  • From your JSP, you can refer to the validator as follows:
  •                                         

From: http://sitestree.com/?p=4997
Categories:Java Short Notes
Tags:
Post Data:2012-05-25 08:43:55

Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada

JSF: Lesson – 5: Converting Data #Java Short Notes

  • Sample application for JSF Data Type Converter
  • Video Tutorial for JSF Data Type Converter
  • Pre-requisite: JSF Lesson 1, 2, 3, 4
  • JSF provides two different types of built-in data type converters such as: String and Date Conversion, Number and String Conversion.
  • Related tags: f:convertDateTime, f:convertNumber, f:converter
  • In our previous examples, we could accept departing and returning dates to be date/time types rather than string types [as expected]
  • How to achieve:
      
  • For other types of conversions, you have to use custom converters.
  • To make use of the strategy, you have to write a class that will do the conversion work.
  • Your class should implement: javax.faces.convert.Converter interface that has two methods that you must implement. One method takes input as String and returns an Object. Another method, takes an Object as the input and returns String.
  • Methods
    Object getAsObject(javax.faces.context.FacesContext context,   javax.faces.component.UIComponent component, java.lang.String value)String getAsString(javax.faces.context.FacesContext context,   javax.faces.component.UIComponent component, java.lang.Object value)
  • getAsObject: Returns an object
  • getAsString: Returns String
  • As you have access to the JSF context, and the corresponding user interface component in these methods- you will be able to greatly customize your converter
  • How to use the custome converter:
    • Define a converter in the configuration file (faces-config.xml) as follows where TerminalConverter is your custom converter class:
      terminal.converter  net.justetc.jsf.TerminalConverter
    • From JSPs refer to a converter-id as defined in the configuration file. See example below:
    • 		  	or	
    • You can also, define/use the converter for all properties of a class such as Terminal as follows:
        net.justetc.jsf.Terminal  net.justetc.jsf.TerminalConverter
    • In this last scenario, whenever JSF identifies a Terminal type data it converts the data using the TerminalConverter class.
         

      In this example, as it encounters bus.origin to be of Terminal type, it will convert the origin to be a Terminal object though the user will just enter a String.

From: http://sitestree.com/?p=4996
Categories:Java Short Notes
Tags:
Post Data:2011-05-14 09:49:51

Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada

JSF: Lesson – 4: Accessing Context Data from Beans #Java Short Notes

  • In some cases, from the beans used in JSF applications, you will need to access the request parameters. For example, in the BusSearch.java, we accessed, the request parameters [Please check the previous JSF lesson].
  • How to access: FacesContext context = FacesContext.getCurrentInstance();
  • Some methods of FacesContext:
    • Map getApplicationMap()
    • String getInitParameter(java.lang.String name)
    • Map getInitParameterMap()
    • String getRemoteUser()
    • Map getRequestCookieMap()
    • Map getRequestHeaderMap()
    • Map getRequestHeaderValuesMap()
    • Map getRequestMap()
    • Map getRequestParameterMap()
    • Iterator getRequestParameterNames()
    • Map getRequestParameterValuesMap()
    • Map getSessionMap()
  • Sample code:
    FacesContext context = FacesContext.getCurrentInstance();
  • Map requestParams =    context.getExternalContext().getRequestParameterMap();
  • String tripNum = (String) requestParams.get("tripNum");

From: http://sitestree.com/?p=4995
Categories:Java Short Notes
Tags:
Post Data:2007-08-18 13:22:28

Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada

JSF: Lesson – 3: Controlling Page Navigation in JSF #Java Short Notes

  • Sample application for this article
  • Video Tutorial for this article
  • Pre-requisite: JSF Lesson 1 & 2
  • Navigation: Two Types:
    • Static: The destination page is fixed
    • Dynamic: The destination page varies with the conditions
    • Static
      • Static: You provide a fixed value for the “action” attribute for a JSF event control/action element
      • That fixed value is mapped to the fixed destination page in the configuration file such as the faces-config.xml
      • If you check our previous lesson, the searchForms.jsp contains . In this line, the text ‘action=”submit”‘ defines the static navigation.
      • The navigation rule in the following text maps submit action to searchResults.jsp page [static/fixed mappping]
        /searchForm.jsp

        submit
        /searchResults.jsp

    • Dynamic Navigation
      • In this case, the action takes a dynamic value. All possible values are mapped into [different] destination pages. Hence, when the action gets a different value, the destination page is also different [unless mapped to the same destination page]
      • To get dynamic nature, the action is mapped to a value/property or a method of a managed bean. The property must be of String type. Also, the method must return String and must not take any parameter.
      • At the time, the action is executed [the button is pressed], corresponding property value is retrieved or the method is called to get value. This value is then matched with the navigation cases in faces-config.xml.
      • The user is then redirected to the destination of the mathching navigation case.
      • Example: Binding action to a value/method expression in jsp: . Here, search is a method in the bus bean. Hence, at the button click, the search method is executed. For example, it may get values such as success, noTrips
      • Mapping in faces-config.xml: success, and noTrips are mapped in the following navigation cases.
      • /searchForm.jsp

        success
        /searchResults.jsp

        noTrips
        /noTrips.jsp

  • Navigation Rules:
    • If you observe the navigation rules above, you will notice elements. This is to mention, from where the action is generated.
    • : is the action message based on which flow is maintained
    • element provides the destination page
    • is optional. If you omit , then for all <from -outcome&gt message for the application, will be redirected to the related destination page. If all page refers to the same privacy policy file, this rule can be useful
    • Example:

      privacy-policy
      /WEB-INF/privacy.jsp

    • You can also redirect for the same message for a group of pages to the same destiation. Such as:
      /order/*
      success
      /WEB-INF/invoice.jsp
    • element: Consider a case, in the same page, in two command buttons, the actions are mapped to save and search methods. Both save and search methods can return success as the element. But, if you want them (two successes from the two methods) to go to two different destination pages, then element comes handy.
    • You can use element in the following way:
    • /searchForm.jsp

      #{bus.search}
      success
      /searchResults.jsp

      #{bus.save}
      success
      /searchForm.jsp

  • Now check the sample example and the video.
  • From: http://sitestree.com/?p=4994
    Categories:Java Short Notes
    Tags:
    Post Data:2012-08-07 12:03:08

    Shop Online: https://www.ShopForSoul.com/
    (Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
    In Bengali: http://Bangla.SaLearningSchool.com
    http://SitesTree.com
    8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
    Shop Online: https://www.ShopForSoul.com/
    Medium: https://medium.com/@SayedAhmedCanada

    JSF: Lesson – 2: JSF Managed Beans #Java Short Notes

    Codes for this article
    Video Demonstration for this article

    • Target: Intermediate level programmers and web-developers. Any programmer/web-developer can take a look.
    • Pre-requisite: HTML, JSP, Servlet, Tomcat, J2EE, MVC, and JSF Introduction. Check the corresponding sections of this web-site, to have an idea on the required technology knowledge
    • Pre-requisite: Download this sample JSF application
    • Beans used by JSF enabled applications are called managed beans as they are created and managed by JSF
    • JSF is based on the MVC architecture where beans provide the models
    • To be used with JSF, a bean must have a no argument constructor. The bean can expose its properties with get or set methods for reading or writing from outside
    • Beans can be used in two ways in JSF applications: The information required to create managed beans are provided in the configuration file (faces-config.xml), JSF uses value binding expressions or method binding expressions, to refer to the properties of the managed beans.
    • Configuring Managed Beans
      • Beans are configured in the faces-config.xml file. The file is usually provided under WEB-INF folder. Though from the web.xml file (using the javax.faces.CONFIG_FILES context parameter), you can refer to any other file where the configuration is provided.
      • META-INF/faces-config.xml file can also be used
      • Multiple configuration files can also be used. The file names should be mentioned in the web.xml file.
      • Initialize Bean Properties
      • Accessing Bean properties
        • Bind managed bean methods to expressions in JSP pages
        • or : set flight property as the control is a JSF input control
        • or : Retrieve value from the origin property.
        • Access list property values: #{bus.times[“1”]}
        • Access Map property values: flight.airportNames.key, flight.airportNames[key]
        • Similarly, you can also bind managed bean methods to expressions in JSP pages.

    From: http://sitestree.com/?p=4993
    Categories:Java Short Notes
    Tags:
    Post Data:2010-10-14 11:11:25

    Shop Online: https://www.ShopForSoul.com/
    (Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
    In Bengali: http://Bangla.SaLearningSchool.com
    http://SitesTree.com
    8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
    Shop Online: https://www.ShopForSoul.com/
    Medium: https://medium.com/@SayedAhmedCanada

    JSF: Lesson – 1: JSF Specifications #Java Short Notes

  • Video Demonstration of a sample JSF application create: Read the article first
    • JSF Specifications
    • JSF is not standalone technology, you have to use it in conjunction with JSPs, Servlets, EJBs
    • How to use JSF with Servlets and EJBs: In Servlet or EJB, explicitly create instances of UI components and use the UI classes directly
    • JSF with JSPs: Use the JSF custom tag library with JSP
    • To use JSF, you need to understand JSF lifecycle: take a look at JSF Lifecycle: Restore view, Apply request values, Process validations, Update model values, Invoke application, Render response
    • How to install JSF: Current version of Sun’s Application Server contains JSF and JSTL
    • For Tomcat 5.5:
      • Download JSF from http://java.sun.com/j2ee/javaserverfaces/download.html and JSTL from http://jakarta.apache.org/taglibs/doc/standard-doc/intro.html.
      • Put these Six JSF JARs into your project WEB-INF/lib folder or tomcat lib folder: commons-beanutils.jar, commons-collections.jar, commons-digester.jar, commons-logging.jar, jsf-api.jar, and jsf-impl.jar
      • Download JSF Jars
      • Put these two JSTL JARs in the same way: jstl.jar and standard.jar
    • JSF provides different custom actions as will be provided below:
    • You can use these elements/controls/actions to collect user data, to display output, to validate user inputs, control the flow of the application dynamically/statically also, convert data from one type to another.
    • HTML Custom Actions
      Input: To create input elements
      h:inputHidden, h:inputSecret,h:inputText, h:inputTextarea

      Output:create output elementsh:message, h:messages,h:outputFormat, h:outputLabel,h:outputLink, h:outputText
      Selection: Create selection elements like combo boxesh:selectBooleanCheckbox,h:selectManyCheckbox,h:selectManyListbox,h:selectManyMenu,h:selectOneListbox,h:selectOneMenu,h:selectOneRadio
      Commands: Create form submission buttons or links
      h:commandButton,h:commandLink
      Miscellaneous
      h:dataTable, h:form,h:graphicImage, h:panelGrid,h:panelGroup, h:column

    • JSF Core custom actions:

      Converters:Standard
      f:convertDateTime,f:convertNumber, f:converter
      Listeners:Listener for a component
      f:actionListener,f:valueChangeListener
      Miscellaneous
      f:attribute, f:loadBundle,f:param, f:verbatim
      Selection
      f:selectItem, f:selectItems
      Validators:Standard
      f:validateDoubleRange,f:validateLength ,f:validateLongRange ,f:validator
      View: Create JSF view or sub-view
      f:facet, f:subview, f:view

    • An example of JSF with JSP with Managed Bean Support
      • You need a configuration file like faces-config.xml , or you can use any other file but you have to supply the file name in the web.xml file.
      • To use a managed bean, you need to define a managed bean in the JSF configuration file with a name, class, and scope
      • Also you need to define navigation rules in the configuration file. A navigation-rule defines the start page, a condition, and which page to navigate to when the condition occurs
      • Create the managed bean class
      • In your JSP, you should refer to the JSF tag libs and JSTL tag libs by
      • Then in the JSP page you will be able to use the JSF tags and the managed beans directly
      • A sample example with code
      • Video Demonstration of the example JSF application create
      • Details about other JSF features like: using managed beans, controlling page navigation, accessing JSF context data in beans, converting data, validating user inputs, using message bundles to support internationalization will be disucssed in another article.
  • From: http://sitestree.com/?p=4991
    Categories:Java Short Notes
    Tags:
    Post Data:2012-10-30 13:33:53

    Shop Online: https://www.ShopForSoul.com/
    (Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
    In Bengali: http://Bangla.SaLearningSchool.com
    http://SitesTree.com
    8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
    Shop Online: https://www.ShopForSoul.com/
    Medium: https://medium.com/@SayedAhmedCanada

    Key J2EE Components : Basic Concepts with Examples #Java Short Notes

    • Java EE 5 (J2EE 5) uses XML deployment descriptors for the configuration of the web-applications and web-components.
    • What Java EE provides? It provides the internal framework/structure/system level capability/system-level infrastructure to support large enterprise level applications with features like distributed database, distributed computing , security, and transaction management.
    • J2EE also provides specifications for Containers, Connectors, Interfaces, Communications among different components for enterprise applications
    • J2EE is J2SE based, it supports all of J2SE
    • J2EE in addition to Swing and AWT introduces JSF for user interface creations
    • Sun provides the J2EE specifications and a reference implementation. Anyone can re-implement the complete specification. Hence, if you develop applications based on the core J2EE, it should be compatible across different vendors who later re-implemented J2EE.
    • However, J2EE does not cover 100% of the requirements of all types of enterprise applications, hence vendors usually add values/extra features to their implementations.
    • J2EE provides multi-tier application development
    • Elements of J2EE
      • Clients: Fat Clients: A console application, and JFC, Swing, AWT based GUI applications. Thin clients: Browser based client applications
      • Servers: Connect client components to the business logic.
      • Server side components: Web-Components: JSPs, Servlets. Business components: EJBs.
      • J2EE containers provide the support/framework/infrastructure for these server side components. Web Container contains JSPs/Servlets. EJB Container contains EJBs
    • Some concepts in J2EE
      • Containers, JSF, JDBC, XML Support, Web-Services, Transaction Management, Security
      • Servlet: Mostly for Processing/calculations: From The clients, you can refer to a Servlet for example in HTML Form Actions. The Servlet runs in the server, does required processing and returns HTML web-page to the clients.
      • JSP: Mostly for user interfaces, can also include processing using Java codes
      • JSF: JSF is used to create rich user interfaces in conjunction with JSPs and Servlets.
      • JSF provides API based server side user interface creation
      • JDBC provides APIs to access relational databases. Enterprise applications very often will store information in databases. JDBC helps to access these information.
      • EJBs evolved on RMI’s limitations to support enterprise applications. EJB components contain business logic.
      • EJBs are of three types: entity beans, session beans, message beans
    • J2EE 5 XML Support Technologies
      • JAXP: DOM based processing. SAX: Stream based processing
      • JAXB: Mapping between XML and Java classes
      • JAXR: XML Registries
      • JAXM for messaging
      • JAX-RPC: XML based RPC
      • JAXR, JAXM, and JAX-RPC: provide support for SOAP and web-services
    • Web-services: Here functions/operations are provided as web-based services. Others can call and make use of these services. Great for B2B communications and integrations.
    • Web-services: Related concepts: WSDL: Describes the services provided – Web Service Description Language. Service Registries such as ebXML, UDDI. JAXR API can access these registries. SOAP: The protocol for communications (web-services).
    • Transaction Management: Java EE including EJBs provides transaction management support.
    • J2EE also provides security supports such as role-based authorization, both declarative and programmatic securities.

    From: http://sitestree.com/?p=4990
    Categories:Java Short Notes
    Tags:
    Post Data:2011-02-12 03:58:54

    Shop Online: https://www.ShopForSoul.com/
    (Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
    In Bengali: http://Bangla.SaLearningSchool.com
    http://SitesTree.com
    8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
    Shop Online: https://www.ShopForSoul.com/
    Medium: https://medium.com/@SayedAhmedCanada

    Struts 2 vs. Struts 1 #Java Short Notes

    Please check the following resources:

    From: http://sitestree.com/?p=4987
    Categories:Java Short Notes
    Tags:
    Post Data:2008-11-22 05:09:37

    Shop Online: https://www.ShopForSoul.com/
    (Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
    In Bengali: http://Bangla.SaLearningSchool.com
    http://SitesTree.com
    8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
    Shop Online: https://www.ShopForSoul.com/
    Medium: https://medium.com/@SayedAhmedCanada