Struts2 Example index

1

First Struts2 Application on Tomcat Web Server v6

2

Actions in Struts2

The action classes are core concepts in Struts2. It is Model class and acts as controller for processing on the data. So actions are the core part of Struts2 framework.

3

More in Struts2 Action

In Struts2 Action class can be created by different ways - by extending ActionSupport class, by implementing Action interface and POJO class.

4

Model Driven action in Struts2

Action class itself treats as model class and request parameters are bound directly to fields in the actions class.

5

Dependency Injection In Struts2

The Inversion of Control (IoC) and Dependency Injection (DI) are programming design pattern, which is used to reduce compiling in computer programs.

6

ApplicationAware Interface

It allows to set Map object with Application object. This Map object contains different other objects which can be accessible from entire application.

7

CookieAware Interface Demo

To implement traditional J2EE Cookie in Struts 2 application, you need to implement CookieAware interface and override setCookiesMap() method.

8

ServletContext Interface

It allows to set attributes in Application scope.The ServletContext is used to store information and it can be accessible from any web components.

9

RequestAware Interface

To manage attributes of J2EE HttpServletRequest object in Struts 2 framework, you need to implement RequestAware interface and override public void setRequest(Map reqmap) { } method.

10

ServletRequestAware Interface

To obtain J2EE HttpServletRequest object in Struts 2 framework, you need to implement ServletRequestAware interface and override public void setServletRequest(HttpServletRequest req) { } method.

11

ServletResponseAware Interface

To obtain J2EE HttpServletResponse object in Struts 2 framework, you need to implement ServletResponseAware interface and override public void setServletResponse(HttpServletResponse response) { } method.

12

SessionAware Interface

To obtain J2EE HttpSession object implement SessionAware interface and override public void setSession(Map map) { } method.

13

Interceptors in Struts2

Interceptors is used to controlling request and response. It executed before and after the Action. So It can be used to process some logics on the request before Action and after Action also.

14

alias Interceptor

Normally, the request parameter's name must same as action's properties name or HTML control name must be same as properties in Action class. But in some cases, you can define different UI control name than properties in Action or POJO class.

15

Chaining Interceptor

It is used to copies all the object from current value stack to the next value stack in action class. It will not copy the objects which implements Unchainable interface.

16

Cookie Interceptor

It is used to manage cookies in the stack/action. It is not part of defaultStack interceptor in struts-default.xml. I have to configure manually in struts.xml file.

17

Debugging Interceptor

18

Execute and wait Interceptor

19

Fileupload Interceptor

20

Logger Interceptor

21

Params Interceptor

22

Prepare Interceptor

23

User defined interceptor

24

Tags in Struts2

The Struts2 framework provides powerful tag library to create view pages. It can be used to create view pages in very easy.

25

Control Tag - if-elseif-else

It is used to control the execution behavior of data in the page. Find below details of each control tags.

26

Control Tag - append

It is used to appends iteratable two or more lists together to create appended list. Sometimes we need to append more collection objects in single object.

27

Control Tag - generator

It is used to generate an iterator based on val attribute supplied to this tag. The genarated iterator is pushed to the top of the stack and popped at the end of the tag.

28

Control Tag - iterator

It is ued to iterate over the list or given values. It allows traversal over any collections object like, Collection, Map, Enumeration and array.

29

Control Tag - merge

It is used to create a new iteratable list by merging two or more lists. It is used with iterator tag and different than append tag.

30

Control Tag - sort

It is used to sorts a List using a Comparator both passed in as the tag attribute.

31

Control Tag - subset

A tag that takes an iterator and outputs a subset of it. It delegates to SubsetIteratorFilter internally to perform the subset functionality.

32

data tag - a

The Data tags are used for manipulating data in page. These tags are used to fetch data from valuestack or store value/object in the value stack.

33

data tag - action

This tag enables developers to call actions directly from a JSP page by specifying the action name. The body content of the tag is used to render the results from the action.

34

data tag - bean

It is used to create instance of a POJO class which conforms to the JavaBeans specification. It allows to create an instance of simple JavBean and push it into the valuestack.

35

data tag - date

It is used to format Date in differently. You can specify a custom format or you can use a predefined format as key 'struts.date.format' from properties file.

36

data tag - i18n

It is used to access the key-value information from resource bundle [.properties file ]. It has two attribute - and name.

37

data tag - include

It is used to include the Servlet or JSP pages. It has two attributes id and name.

38

data tag - param

It is used to pass parameters to other tags. The include tag and bean tag are examples of param tag.

39

data tag - property

It is used to get the value from valueStack. If the value is not specofied it retunrs the object from the top of the ValueStack.

40

data tag - push

It is used to list the value in the ValueStack. It also allows to push an object reference onto the ValueStack.

41

data tag - set

The set tag assigns a value to a variable in a specified scope.

42

data tag - text

It is used for accessing a text message from the resource bundle. If the named message is not found in the .properties file, then the body of the tag will be used as the default message.

43

data Tag - url

This tag is used to create a URL.

44

form tag - form

It is used to create a form to accept data from user. All the input tag must into this tag. It is frequently used in the web application.

45

form tag - textfield

It is used to create HTML form input control. It has three important attributes - name (name of the control), label (display label before input control) and size (size of control).

46

form tag - password

It is used to accept password from user in HTMl form

47

form tag - textarea

It is used to create input text for multiple lines.

48

form tag - hidden

It is used to create hidden field in HTML form. It does not show to the user.

49

form tag - checkbox

It is used to create chebox in form. It takes String or boolena as value. It can be populated from ValueStack.

50

form tag - checkboxlist

It is used to create same as radio button in the form of checkbox. User can select none,one or many.

51

form tag - combobox

It is used to create HTML control to select an option from list or user can add new value other than list also.

52

form tag - radio

It is used to create HTML radio button. It allows to selexct only one from the list of options provided through radio button.

53

form tag - select

It is used to create a list of options to select from. It is same as combox, but user can not enter new value. User has to select from list only.

54

form tag - doubleselect

The two HTML select element is created and options in second select element will be populated dynamically based on selection item from first select element.

55

form tag - optgroup

It is used to create HTML control to select option from a list and provides grouping options within a category.

56

form tag - updownselect

57

form tag - optiontransferselect

Create a option transfer select component which is basically two tag with buttons in the middle of them allowing options in each of the
58

Non UI tag - actionerror

It is used to display errors of Action class. The Action error can be set using addActionError() method.

59

Non UI tag - fielderror

It is same as actionerror tag but it display error messages against each field in the form.

60

Non UI tag - actionmessage

It is used to display message associated with an action class. It can be set using addActionMessage(message) method.

61

Results Type in Struts2

After executing the Action class , the target view page will be executed based on configuration in struts.xml.

62

Struts2 Dispatcher result

It is used to include or forward to a target view page(JSP). It is default result type in Struts2 framework.

63

Struts2 Chain result

It is used to achive action chaining. In the action chaining the multiple actions can be executed in defined sequence.

64

Struts2 Freemarker result

It is used to integrate the Freemarker templates in the view page. The Freemarker engine renders the freemarker view page and extension will be .ftl of view page.

65

Struts2 Redirect result

It is used to redirect the browser request to the new resource. It works based on HttpServletResponse.sendRedirect() method.

66

Struts2 RedirectAction result

It is same as redirect result type. But tne target result must be an action either in same application or in the other application.

67

Struts2 Stream result

It is used to Streaming the InputStream back to the client and client can download the content in the specified formmat.

68

Struts2 Velocity result

It is usedn to integration of Velocity template in Struts2 application. Using the Servlet container's JspFactory, this result mocks a JSP execution environment and then displays a Velocity template that will be streamed directly to the servlet output.

69

Struts2 XSL result

It is used to integration of XML/XSLT in view page. It is used to display the ValueStack data in the XSLT format.

70

Struts2 PlainText result

It is used to display raw content of a file in the browser.

71

Handling Exception in Struts 2

The exception handling is the common task in any Java based application.

72

Dynamic Method Selection

Struts 2 has feature to execute method from action dynamically. Suppose you have five methods (execute, add, delete, update and query) in action class and want to configure in struts.xml file.

73

Populate data from action

You can populate data in the view page from action.

74

Data validations

The Struts 2 framework provides excellent validation feature. It can be configured vie XML of annotations.

75

Data validation through XML

The validation through XML based, the xml file must be -validation.xml file and root tag is validators.

76

Data validation through Annotations

You can validate data through annotations also.

77

Internationalization in Struts2

The Struts2 provides feature for web application can created for global use. The web content will be changed according to the language selection by the user at run time.

78

Tiles in Struts2

The Apache Tiles is templating framework is used to design several pages in easiest way. It provides features to divide entire page in different sections including title and page can be inserted in the sections at run time.

79

Annotation in Struts 2

The Struts2 framework scans Action class in classpath using Convention Plug-in. You can use annotations in action class for mapping rather than configuration file struts.xml.

80

@Actions and @Action

The Convention plugin allows action mapping using Action annotation in class. The annotation can also be used inside @Actions annotations for multiple URLs.

81

@Results and @Result

The Convention plugin allows action classes to define many results for an action.

82

@ExceptionMappings and @ExceptionMapping

This annotation can be used to define exception mappings to actions. These mappings can be applied to the class level and action level also.

83

@InterceptorRefs and @InterceptorRef

Interceptors can be specified at the method level, using the @Action annotation or at the class level using the @InterceptorRefs annotation.

84

Hibernate integration in Struts2

The Hibernate is most popular ORM (Object Relational Mapping) framework in the market. You can integrate it in all Java/J2EE based framework.

85

Spring integration

Spring is an Open Source lightweight framework works with all tier software application layers. It provides container to manage bean and uses Dependency Injection(DI).

86

Hibernate-plug-in in Struts 2

You can use hibernate plug-in for the same. You can inject Session and Transaction in DAO class and it can be configured using annotations, xml and both.



Write your Comment

Name :
Email Id : [ We will not publish your email id ]
Your Comment : [ Maximum 200 chars ]
Enter Security code