Skip to main content

Posts

Showing posts from September, 2011

Datetimepicker Tag Example

In this section, we are going to describe the datetimepicker tag. The datetimepicker tag is a UI tag that is used to render a date/time picker in a dropdown container. A stand-alone DateTimePicker widget that makes it easy to select a date/time or increment by week, month, and/or year. It is possible to customize the user-visible formatting with either the 'formatLength' (long, short, medium or full) or 'displayFormat' attributes. By default current locale will be used. Add the following code snippet into the struts.xml file. struts.xml <action name="datetimepickerTag" class="net.struts2.includeTag">    <result>/pages/uiTags/datetimepickerTag.jsp</result> </action> Create an action class as shown: includeTag.java package net.struts2; import com.opensymphony.xwork2.ActionSupport; import java.util.*; public class includeTag extends ActionSupport {   private Date m...

Struts2 - Combobox Tag (Form Tags)

The struts2 combobox is basically an HTML INPUT of type text and HTML SELECT grouped together to give you a combo box functionality. You can place text in the INPUT control by using the SELECT control or type it in directly in the text field. Add the following code snippet into the struts.xml file. struts.xml <action name="comboboxTag" class="net.struts2.comboboxTag">    <result>/pages/uiTags/comboboxTag.jsp</result> </action> Create "comboboxTag " action class and a list in the class and populate it with various items as shown in the class. comboboxTag.java package net.struts2; import com.opensymphony.xwork2.ActionSupport; import java.util.*; public class comboboxTag extends ActionSupport{      private List fruits;   public String execute()throws Exception{   fruits = new ArrayList();   fruits.add("Appl...

Create Struts2 project Using NetBeans

If you decide to start learning and using struts2, probably one of the first things you think about is which IDE to use, and what you need in order to create and run a Struts2 project... If you search the web , you'll find many tutorials on how to create a struts2 project, and start developing. This is what I did when I was starting, and soon find out that nothing I found on the web worked for me in proper manner... So, I decided to create a small tutorial on how to create an empty struts2 project , in order to help others, and save their time. Not to teach you Struts2, but to help you create a working Struts2 project! Ok, I will use NetBeans 6.5, and a Tomcat 6 that comes with it... First of all, go to http://struts.apache.org/2.x/index.html and download the latest version of Struts2. I downloaded struts-2.1.6-all.zip You won't be needing all the libraries in it. Just 6 of them. Because of this, you can download essential dependencies only. When you download one...

Global Action Forwards and Action Mappings

What is an action forward? A action forward can be used to forward to a jsp or action mapping. There are two different action forwards. The global action forward and the local action forward. You can access a global action forward on each jsp or action class. A local action forward can only be accessed by the assigned action class. What is a action mapping? The action mapping is the heart of struts. It managed all actions between the application and the user. You can define which action will be executed by creating a action mapping. The diagram show you, how the application server manage the request of the index.jsp or a non existing action mapping. In the first step we create a new action mapping. Open the struts-config.xml, you will find it in the folder WebRoot/WEB-INF. Right click in the outline window on action-mapping. Choose Use Case default and Action Type Forward. The Forward Path is the welcome page /jsp/index.jsp In the second step you create a global act...

Create Struts2 Project Using MyEclipse

Create a struts project Create a new struts project with File > New > Project or use the shortcut Strg + n. Select the Wizard in J2EE Web Project Create a nice name for your project After creating the project , your Package Explorer looks like the picture below. For now your project is a normal J2EE project , so we need to add the struts capabilityies. Right click on the project and add the capabilityies for struts with Add Struts Capabilityies. Change the properties Base package for new classes and Default application resource Create a default welcome page Ok, now we want to create a default page. Right click (yes again) on the Folder WebRoot in the Project and choose New > JSP. Set the name to index.jsp and choose on template to use > Standard JSP using Struts 1.1 MyEcplise will use the template to create the JSP File. You will find the file index.jsp in the folder WebRoot of the project . On the top of the file you will find the struts tag l...

Struts2 - Checkboxlist Tag (Form Tags)

In this section, we are going to describe the checkboxlist tag. The checkboxlist tag is a UI tag that creates a series of checkboxes from a list. Setup is like or , but creates checkbox tags. Add the following code snippet into the struts.xml file. struts.xml <action name="checkboxlistTag" class="net.struts2.checkboxlistTag">    <result>/pages/uiTags/checkboxlistTag.jsp</result> </action> Create two lists in the action class and populate them with various items as shown in the " checkboxlistTag " class. checkboxlistTag.java package net.struts2; import com.opensymphony.xwork2.ActionSupport; import java.util.*; public class checkboxlistTag extends ActionSupport{      private List fruits;   private List animals;     public String execute()throws Exception{   fruits = new ArrayList();...

Struts2 - Checkbox Tag (Data Tags)

In this section, we are going to describe the checkbox tag. The checkbox tag is a UI tag that is used to render an HTML input element of type checkbox, populated by the specified property from the ValueStack. Add the following code snippet into the struts.xml file. struts.xml <action name="checkboxTag">    <result>/pages/uiTags/checkboxTag.jsp</result> </action> Create a jsp using the tag It renders an HTML input element of type chechbox. checkboxTag.jsp <%@ taglib prefix="s" uri="/struts-tags" %> <html>   <head>    <title>Checkbox  (Form Tag) Tag Example!</title>   </head>   <body>   <h1><span style="background-color: #FFFFcc"> Checkbox Tag Example!</span></h1>   <b>Sex</b><br>   <s:checkbox label="Ma...

Struts2 - Auto Completer Tag

In this section, we are going to describe the autocompleter tag. The autocompleter tag always displays a dropdown list with the options that have at least a partial match with entered text in the textbox. If the user clicks on the dropdown button then all options shown in the dropdown list. The autocompleter tag generates two input fields. First is "text", whose name is specified with the "name" attribute and another one is "hidden" whose name is "$(name). Key", where ${name} is the value in the "name" attribute. The autocompleter tag loads its options asynchronously when the page loads suggested options based on the text entered by you in textbox. If the autoComplete attribute is set to 'true' (By defalut 'false') then it makes suggestions in the textbox. [Note: When we use the "simple" theme, the autocompleter can be used like the ComboBox. When used on the "ajax" theme, the list can be retriev...

Struts2 - Property Tag (Data Tags)

In this section, we are going to describe the property tag. The property tag is a generic tag that is used to get the property of a value, which will default to the top of the stack if none is specified. Add the following code snippet into the struts.xml file. struts.xml <action name="propertyTag" class="net.struts2.propertyTag">   <result>/pages/genericTags/propertyTag.jsp</result> </action> Create an action class as shown: propertyTag.java package net.struts2; import com.opensymphony.xwork2.ActionSupport; public class propertyTag extends ActionSupport {   public String execute() throws Exception{   return SUCCESS;   } } Create a bean class " companyName " as shown: companyName.java package net.struts2; public class companyName {      private String name;   public voi...

Struts2 - Text Tag (Data Tags)

In this section, we are going to describe the text tag. The text tag is a generic tag that is used to render a I18n text message. Follow one of the three steps: 1. Keep the message to be displayed in a resource bundle with the same name as the action that it is associated with ie. create a properties file in the same package as your Java class with the same name as your class, but with .properties extension. 2. If the property file does-not work or the message is not found in the resource bundle, then the body of the tag will be used as default message. 3. If there is no body, then the name of the message will be used. Add the following code snippet into the struts.xml file. struts.xml <action name="textTag" class="net.struts2.textTag">    <result>/pages/genericTags/textTag.jsp</result> </action> Create an action class as shown below: textTag.java package net.struts2; import com.opensymphony.xwork2.ActionSuppor...

Struts2 - Set Tag (Data Tags)

In this section, we are going to describe the Set tag. The set tag is a generic tag that is used to assign a value to a variable in a specified scope. It is useful when you wish to assign a variable to a complex expression and then simply reference that variable each time rather than the complex expression. Add the following code snippet into the struts.xml file. struts.xml <action name="setTag">    <result>/pages/genericTags/setTag.jsp</result> </action> Now create a jsp page using tag as shown in the setTag.jsp page. The set tag is used to assign a value to a variable in a specified scope. The parameters name and value in the tag acts as the name-value pair. Here we set the parameters as name="technologyName" value="Java". setTag.jsp <%@ taglib prefix="s" uri="/struts-tags" %> <html>   <head>   <title>Set Tag (Data Tag) Example!</title>...

Struts2 - Param Tag (Data Tags)

In this section, we are going to describe the param tag. The param tag is a generic tag that is used to parameterize other tags. For example the include tag and bean tag. The parameters can be added with or without a name as a key. The param tag has the following two parameters. 1. name (String) - the name of the parameter 2. value (Object) - the value of the parameter Note: When you declare the param tag, the value can be defined in either a value attribute or as text between the start and end tag. Struts behaves a bit different according to these two situations. Case 1. AmitHere the value would be evaluated to the stack as a java.lang.String object. Case 2. Here the value would be evaluated to the stack as a java.lang.Object object. Add the following code snippet into the struts.xml file. struts.xml <action name="paramTag">   <result>/pages/genericTags/paramTag.jsp</result> </action> Now create a jsp page to see the w...