Deprecated: Creation of dynamic property wpdb::$categories is deprecated in /home2/learnera/public_html/tech/wp-includes/wp-db.php on line 760

Deprecated: Creation of dynamic property wpdb::$post2cat is deprecated in /home2/learnera/public_html/tech/wp-includes/wp-db.php on line 760

Deprecated: Creation of dynamic property wpdb::$link2cat is deprecated in /home2/learnera/public_html/tech/wp-includes/wp-db.php on line 760

Deprecated: Creation of dynamic property WPtouchProFour::$settings_object is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/wptouch/core/class-wptouch-pro.php on line 82

Deprecated: Automatic conversion of false to array is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/wptouch/core/admin-load.php on line 70

Deprecated: Creation of dynamic property WP_Block_Type::$skip_inner_blocks is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-block-type.php on line 391

Deprecated: Creation of dynamic property WP_Block_Type::$skip_inner_blocks is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-block-type.php on line 391

Deprecated: Creation of dynamic property Advanced_Editor_Tools::$toolbar_classic_block is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/tinymce-advanced/tinymce-advanced.php on line 347

Deprecated: Creation of dynamic property Advanced_Editor_Tools::$toolbar_block is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/tinymce-advanced/tinymce-advanced.php on line 349

Deprecated: Creation of dynamic property Advanced_Editor_Tools::$toolbar_block_side is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/tinymce-advanced/tinymce-advanced.php on line 350

Deprecated: Creation of dynamic property Advanced_Editor_Tools::$panels_block is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/tinymce-advanced/tinymce-advanced.php on line 351

Deprecated: Creation of dynamic property Advanced_Editor_Tools::$used_block_buttons is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/tinymce-advanced/tinymce-advanced.php on line 354

Deprecated: Creation of dynamic property YARPP::$is_custom_template is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/yet-another-related-posts-plugin/classes/YARPP_Core.php on line 56

Deprecated: Creation of dynamic property YARPP::$db_options is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/yet-another-related-posts-plugin/classes/YARPP_Core.php on line 69

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-term-query.php on line 1118

Deprecated: Return type of Requests_Cookie_Jar::offsetExists($key) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home2/learnera/public_html/tech/wp-includes/Requests/Cookie/Jar.php on line 63

Deprecated: Return type of Requests_Cookie_Jar::offsetGet($key) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home2/learnera/public_html/tech/wp-includes/Requests/Cookie/Jar.php on line 73

Deprecated: Return type of Requests_Cookie_Jar::offsetSet($key, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home2/learnera/public_html/tech/wp-includes/Requests/Cookie/Jar.php on line 89

Deprecated: Return type of Requests_Cookie_Jar::offsetUnset($key) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home2/learnera/public_html/tech/wp-includes/Requests/Cookie/Jar.php on line 102

Deprecated: Return type of Requests_Cookie_Jar::getIterator() should either be compatible with IteratorAggregate::getIterator(): Traversable, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home2/learnera/public_html/tech/wp-includes/Requests/Cookie/Jar.php on line 111

Deprecated: Return type of Requests_Utility_CaseInsensitiveDictionary::offsetExists($key) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home2/learnera/public_html/tech/wp-includes/Requests/Utility/CaseInsensitiveDictionary.php on line 40

Deprecated: Return type of Requests_Utility_CaseInsensitiveDictionary::offsetGet($key) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home2/learnera/public_html/tech/wp-includes/Requests/Utility/CaseInsensitiveDictionary.php on line 51

Deprecated: Return type of Requests_Utility_CaseInsensitiveDictionary::offsetSet($key, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home2/learnera/public_html/tech/wp-includes/Requests/Utility/CaseInsensitiveDictionary.php on line 68

Deprecated: Return type of Requests_Utility_CaseInsensitiveDictionary::offsetUnset($key) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home2/learnera/public_html/tech/wp-includes/Requests/Utility/CaseInsensitiveDictionary.php on line 82

Deprecated: Return type of Requests_Utility_CaseInsensitiveDictionary::getIterator() should either be compatible with IteratorAggregate::getIterator(): Traversable, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home2/learnera/public_html/tech/wp-includes/Requests/Utility/CaseInsensitiveDictionary.php on line 91

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-term-query.php on line 1118

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-term-query.php on line 1118

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-term-query.php on line 1118

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-term-query.php on line 1118

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-term-query.php on line 1118

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-term-query.php on line 1118

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-term-query.php on line 1118

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-term-query.php on line 1118

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-term-query.php on line 1118

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-term-query.php on line 1118

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-term-query.php on line 1118

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-term-query.php on line 1118

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-term-query.php on line 1118

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-term-query.php on line 1118

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-term-query.php on line 1118

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-term-query.php on line 1118

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-term-query.php on line 1118

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-term-query.php on line 1118

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-term-query.php on line 1118

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-term-query.php on line 1118

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-term-query.php on line 1118

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-term-query.php on line 1118

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-term-query.php on line 1118

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-term-query.php on line 1118

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-term-query.php on line 1118

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-term-query.php on line 1118

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-term-query.php on line 1118

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-term-query.php on line 1118

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-term-query.php on line 1118

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-term-query.php on line 1118

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-term-query.php on line 1118

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-term-query.php on line 1118

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-term-query.php on line 1118

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-term-query.php on line 1118

Deprecated: Creation of dynamic property WP_Term::$cat_ID is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 378

Deprecated: Creation of dynamic property WP_Term::$category_count is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 379

Deprecated: Creation of dynamic property WP_Term::$category_description is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 380

Deprecated: Creation of dynamic property WP_Term::$cat_name is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 381

Deprecated: Creation of dynamic property WP_Term::$category_nicename is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 382

Deprecated: Creation of dynamic property WP_Term::$category_parent is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 383

Warning: Cannot modify header information - headers already sent by (output started at /home2/learnera/public_html/tech/wp-includes/wp-db.php:760) in /home2/learnera/public_html/tech/wp-includes/feed-rss2.php on line 8
Struts2 – Rajesh http://tech.learnerandtutor.com A Developer Sat, 06 Dec 2014 06:59:33 +0000 en-US hourly 1 https://wordpress.org/?v=6.0.8 Struts2 Tutorial Part 5 – Namespaces http://tech.learnerandtutor.com/struts2-tutorial-part-5-namespaces/ http://tech.learnerandtutor.com/struts2-tutorial-part-5-namespaces/#comments Wed, 28 May 2014 10:39:19 +0000
Deprecated: Creation of dynamic property WP_Term::$cat_ID is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 378

Deprecated: Creation of dynamic property WP_Term::$category_count is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 379

Deprecated: Creation of dynamic property WP_Term::$category_description is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 380

Deprecated: Creation of dynamic property WP_Term::$cat_name is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 381

Deprecated: Creation of dynamic property WP_Term::$category_nicename is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 382

Deprecated: Creation of dynamic property WP_Term::$category_parent is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 383
http://tech.learnerandtutor.com/?p=430 Deprecated: Creation of dynamic property WP_Query::$yarpp_cache_type is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/yet-another-related-posts-plugin/classes/YARPP_Cache.php on line 30

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-term-query.php on line 1118

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-term-query.php on line 1118

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-term-query.php on line 1118

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-term-query.php on line 1118

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-term-query.php on line 1118

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-term-query.php on line 1118

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-term-query.php on line 1118

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-term-query.php on line 1118

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-term-query.php on line 1118

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-term-query.php on line 1118
In this session we will discuss about Struts2 namespaces.Struts2 namespaces are the Namespaces of the URL accessed. For example if a user types types a URL http://www.learnerandtutor.com/struts2/actions/method.action  in that /struts2/action  is namespace and method.action  is the action name. While naming a namespace we … Continue reading ]]>

Deprecated: Creation of dynamic property WP_Query::$yarpp_cache_type is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/yet-another-related-posts-plugin/classes/YARPP_Cache.php on line 30

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266
In this session we will discuss about Struts2 namespaces.Struts2 namespaces are the Namespaces of the URL accessed.

For example if a user types types a URL http://www.learnerandtutor.com/struts2/actions/method.action  in that /struts2/action  is namespace and method.action  is the action name.

While naming a namespace we have to keep the below points in mind.

  • A namespace will be like “/path1/subpath”, “/path2/path3/sub1”.
  • A namespace can also be root namespace. (i.e) namespace=”/”. But it is exactly like normal namespace. The URL should match for it.
  • For example if the url is “www.learner.com/rajesh.action” it will go to root namespace. If the url is “www.learner.com/path1/subpath” it will go to “/path1/subpath” namespace.
  • We can also create default namespace where we can put default actions. That means if the requested action is not found in any specific namespaces then the control will look for that action inside the default namespace. The default namespace syntax will be
<package name="serveAll" namespace="" extends="struts-default">
  .
  .
  .
</package>

One useful example of this default is we can handle all the unknown requests with the the simple setup like below.

<package name="serveAll" namespace="" extends="struts-default">
        <action name="*">
            <result>/error.jsp</result>
        </action>
</package>

What we done is, if any requested action is not found simply responding with error page instead of file not found error. That’s why we put the action name as *. So even if the user types an action name asdasdasd it will respond with error.jsp . Even we can use wild card characters also in action name. In later chapters we will work on wild char characters in action names.

]]>
http://tech.learnerandtutor.com/struts2-tutorial-part-5-namespaces/feed/ 1
Struts2 Tutorial Part 4 – Packages http://tech.learnerandtutor.com/struts2-tutorial-part-4-packages/ http://tech.learnerandtutor.com/struts2-tutorial-part-4-packages/#respond Wed, 28 May 2014 10:30:30 +0000
Deprecated: Creation of dynamic property WP_Term::$cat_ID is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 378

Deprecated: Creation of dynamic property WP_Term::$category_count is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 379

Deprecated: Creation of dynamic property WP_Term::$category_description is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 380

Deprecated: Creation of dynamic property WP_Term::$cat_name is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 381

Deprecated: Creation of dynamic property WP_Term::$category_nicename is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 382

Deprecated: Creation of dynamic property WP_Term::$category_parent is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 383
http://tech.learnerandtutor.com/?p=420 Deprecated: Creation of dynamic property WP_Query::$yarpp_cache_type is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/yet-another-related-posts-plugin/classes/YARPP_Cache.php on line 30

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266
In this tutorial session we will discuss about struts 2 packages.Struts 2 packages are like java packages where actions can be grouped together. See a sample package below. <package name="default" namespace="/" extends="struts-default"> <action name="login"> <result>/menu/login.jsp</result> </action> <action name="authendicate" class="com.rajesh.Authendicate" method="verify"> … Continue reading ]]>

Deprecated: Creation of dynamic property WP_Query::$yarpp_cache_type is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/yet-another-related-posts-plugin/classes/YARPP_Cache.php on line 30

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266
In this tutorial session we will discuss about struts 2 packages.Struts 2 packages are like java packages where actions can be grouped together. See a sample package below.

<package name="default" namespace="/" extends="struts-default">
    <action name="login">
        <result>/menu/login.jsp</result>
    </action>

    <action name="authendicate" class="com.rajesh.Authendicate" method="verify">
        <result>/menu/success.jsp</result>
    </action>
</package>

We can provide four parameters with package declaration.

    1. name        –        Name of the package (required)
    2. namespace   –   Namespace for the actions in the package
    3. extends     –      Parent package to inherit
    4. abstract    –       If true this package will only be used to define inheritable components not actions. (Discuss in detail later)
  • The main use of packages are we can group similar actions together.
  • We can inherit one packages options to another one simply by extending it.
  • An example is we can group all the actions that required authentication, then by adding particular rule for that package alone we can control the unauthorized access.

Most of the Struts 2 Framework default classes and functionality methods are grouped into packages. We can make use of those by simply extend it. See our example code above, we extended the “struts-default” package where major struts 2 default implementations are available.

]]>
http://tech.learnerandtutor.com/struts2-tutorial-part-4-packages/feed/ 0
Struts2 Tutorial Part 3 – Actions 2 http://tech.learnerandtutor.com/struts2-tutorial-part-3-actions-2/ http://tech.learnerandtutor.com/struts2-tutorial-part-3-actions-2/#respond Tue, 27 May 2014 11:38:48 +0000
Deprecated: Creation of dynamic property WP_Term::$cat_ID is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 378

Deprecated: Creation of dynamic property WP_Term::$category_count is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 379

Deprecated: Creation of dynamic property WP_Term::$category_description is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 380

Deprecated: Creation of dynamic property WP_Term::$cat_name is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 381

Deprecated: Creation of dynamic property WP_Term::$category_nicename is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 382

Deprecated: Creation of dynamic property WP_Term::$category_parent is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 383
http://tech.learnerandtutor.com/?p=404 Deprecated: Creation of dynamic property WP_Query::$yarpp_cache_type is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/yet-another-related-posts-plugin/classes/YARPP_Cache.php on line 30

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266
In this tutorial session we will create a simple struts 2 application to understand the complete flow of an action. If you want to know the clear picture about Struts2 action see our action post 1 here. If you want … Continue reading ]]>

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_author is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_date is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_date_gmt is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_content is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_title is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_excerpt is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$comment_status is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$ping_status is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_password is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_name is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$to_ping is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$pinged is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_modified is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_modified_gmt is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_content_filtered is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_parent is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$guid is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$menu_order is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_type is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_mime_type is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$comment_count is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$filter is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$author_name is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$author_profile_url is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$avatar_url is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$avatar is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$thumb is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$preview is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$file_count is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$page_url is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$page_link is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$page_url_qr is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$btnclass is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$file_size is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$file_type_icon is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$download_url is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$download_link is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$download_link_extended is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$download_link_popup is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$formatted is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$__template is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$__template_type is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WP_Query::$yarpp_cache_type is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/yet-another-related-posts-plugin/classes/YARPP_Cache.php on line 30

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266
In this tutorial session we will create a simple struts 2 application to understand the complete flow of an action. If you want to know the clear picture about Struts2 action see our action post 1 here. If you want to get clear idea over struts 2 from the beginning please refer the below posts.

Struts2 Introduction
Struts2 HelloWorld Application
Struts2 Tutorial Part 3 – Actions 1

While develop our application, we will discuss about the details of each configuration parameter. First we have to create the home.jsp file which is the first page of the application. See the code below.

home.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
    <html>

    <head>
        <title>Struts 2 Custom URL</title>
        <script src="jquery-1.11.1.min.js"></script>
    </head>

    <body>
        <h1>Struts 2 Action Tutorial</h1>
        <s:form action="welcome" method="post" enctype="multipart/form-data">
            <s:textfield key="Name" name="name" />
            <s:textfield key="City" name="city" />
            <s:submit key="submit" />
        </s:form>
    </body>

    </html>

We created a simple jsp file with struts2 UI tags. We will learn about Struts2 UI tags later. As of now just keep in mind struts2 tags are like normal html tags with more functionalists. So for clear understanding consider <s:form>  as normal <form>  tag, <s:textfield>  as normal <input type=’text’>  tag and so.So when we fill and submit the form it will submit the data to the url welcome.

Next we will create another jsp page where we will display the name and city what we entered in the home.jsp. See the code below.

welcome.jsp

<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
    <title>Struts2 Custom URL</title>
</head>

<body>
    <h2>
Welcome to Struts2 Actions Tutorial
<s:property value="name" />,<s:property value="city" />.
</h2>
    <a href="/Struts2Action">Go Back to Home</a>
</body>

</html>

Here also we used struts2 UI  tag <s:property> . It is used to retrieve the value from the OGNL stack where all the variables are stored. We will see about the Struts2 UI tags and OGNL stack later. As of now assume it will return the respective values.

Next we will create our struts.xml file which is the configuration file, See the code below.

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <package name="default" namespace="/" extends="struts-default">
        <action name="">
            <result>home.jsp</result>
        </action>
        <action name="welcome" class="com.rajesh.struts2.Welcome" method="welcome">
            <result name="success">welcome.jsp</result>
            <result name="error">error.jsp</result>
        </action>
    </package>
</struts>

See the web.xml file below.

web.xml

<?xml version="1.0" encoding="UTF-8" ?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
    <display-name>Struts2Action</display-name>

    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>

Finally see our java file where we have our business logic.

Welcome.java

package com.rajesh.struts2;

import com.opensymphony.xwork2.ActionSupport;

public class Welcome extends ActionSupport {

    private static final long serialVersionUID = 1500617172067606799L;

    private String name, city;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String welcome() {
        return SUCCESS;
    }
}

That’s all the files needs to be created. Now we will discuss the workflow of this application to understand actions clearly. At first when the user types http://localhost:8080/Struts2Action/  in the browser the control will go the web.xml file as we discussed in the previous chapter. In web.xml we never mentioned any index files. But we mentioned a filter to all the incoming requests.

<filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

So the control will go to the filter class. We no need to worry abouth these portions. These are all implemented by the framework itself.  Just for understanding I am explaining this.

Then the framework will look for struts.xml file. Then as we typed simply the application name alone it will look for the action with emty name. In our struts.xml file we have a action with name emty. So the framework will render the page home.jsp to the browser.

Here one important think we have to note is before sending the page to browser the framework will replace all struts2 UI tags by corresponding html tags. To understand this after the home.jsp is rendered on the browser see the source code of the page.

After rendering the page the page source code will look like below one.

<html>

<head>
    <title>Struts 2 Custom URL</title>
</head>

<body>
    <h1>Struts 2 Action Tutorial</h1>
    <form id="welcome" name="welcome" action="/Struts2Action/welcome.action" method="post" enctype="multipart/form-data">
        <table class="wwFormTable">
            <tr>
                <td class="tdLabel">
                    <label for="welcome_name" class="label">Name:</label>
                </td>
                <td>
                    <input type="text" name="name" value="" id="welcome_name" />
                </td>
            </tr>
            <tr>
                <td class="tdLabel">
                    <label for="welcome_city" class="label">City:</label>
                </td>
                <td>
                    <input type="text" name="city" value="" id="welcome_city" />
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <div align="right">
                        <input type="submit" id="welcome_submit" name="submit" value="submit" />
                    </div>
                </td>
            </tr>

        </table>
    </form>
</body>

</html>

Then  as in the home page we have to enter the name,city then submit the form. As we mentioned the data to the url welcome.action the control will look for it. So the request will be http://localhost:8080/Struts2Action/welcome.action . As usual the control will look for an action with name ‘welcome’. Once it identifies it will execute the corresponding class to it.

As we discussed all action methods should return a string value. Once that method returned a string value, the matching result tag will be rendered to the user. In our class com.rajesh.struts2.Welcome method welcome() we returned ‘success’ string. So welcome.jsp will be rendered to the user. If suppose our method returns string ‘error’ then the error.jsp will be rendered. If no name attribute is defined with the result tag ‘success’ will be taken as default value.

Note:

In our previous action with empty name we never mentioned any class name. Just remember that the class attribute of the action is an optional one (Discussed in previous chapter). If no class is specified it will take the result as ‘success’ by default

Another attribute for the action tag is method name. See our action welcome.

<action name="welcome" class="com.rajesh.struts2.Welcome" method="welcome">

We mentioned the method name as welcome(). If we don’t specify the method name the control will look for method with name execute() inside the action class.

What is the use of this?

The main reason is we can have multiple methods inside a single class. In that situation we can mention the method name, Otherwise we can simply use the same method name execute(). Anyway it is upto us to decide.

With this knowledge we will look back our initial action with empty name “”. We never mentioned the class to it. So it will take ‘success’ by default. Then it will look for result tage with name ‘success’. We never mentioned that also. So there also it will take ‘success’ by default. Then both matches, result page rendered.

<action name="">
    <result>home.jsp</result>
</action>

Another one important think we have to know what are all the background functions are running, while we call particular action. By default before running any action methods Struts 2 have a set of methods to run. Those are all called interceptors. We will discuss about interceptors in another chapter. As for now kepp in mind before running any action Struts 2 framework executing some methods.

With the above point in mind we will see about how the data from home page is getting travelled to the result page. As I said before running the action method the struts 2 will look for the methods to pass the data from the html form to server.

We will see this with an example. In our home.jsp we have to major data containing fields name and city.

<s:textfield key="Name" name="name" />
<s:textfield key="City" name="city" />

So after we submit the data to the server the struts2 framework will look for the method setName() and setCity(). (i.e.) By default adding the word get infront of the field name and adding the field name starts with capital letter.

Now to understand this see our Welcome.java methods.

public void setName(String name) {
    this.name = name;
}

public void setCity(String city) {
    this.city = city;
}

We have to methods setName() and setCity(). So those will get executed before the action method executes. For each url access a new instance of the class will be created. So when multiple user opens the URL it will not affect one another.

All the variables related to one session will be stored in a common stack called OGNL. So from any portion of the application we can get the value for the variables.

In the same way in our welcome.jsp the tags <s:property value=”name” />  and <s:property value=”city” />  will call getName() and getCity() methods.

Another one important think is all theses processes are happening in the server side. So only the replaced final html file will be sent back to the user. (i.e)The UI tags will not be visible at the user end.
action-post32
action-post31

With this we will complete this session on Struts 2 action. We will continue with action-packages and namespaces in our next session.

I have created a complete and simple .war file with source code for the example we discussed. Download and just place under Apache web apps folder and run.

If you have any issues while execution post in comments or send mail to rajeshmepco@gmail.com.

]]>
http://tech.learnerandtutor.com/struts2-tutorial-part-3-actions-2/feed/ 0
Struts2 Tutorial Part 3 – Actions 1 http://tech.learnerandtutor.com/struts2-tutorial-part-3-actions-1/ http://tech.learnerandtutor.com/struts2-tutorial-part-3-actions-1/#respond Sun, 25 May 2014 02:17:40 +0000
Deprecated: Creation of dynamic property WP_Term::$cat_ID is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 378

Deprecated: Creation of dynamic property WP_Term::$category_count is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 379

Deprecated: Creation of dynamic property WP_Term::$category_description is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 380

Deprecated: Creation of dynamic property WP_Term::$cat_name is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 381

Deprecated: Creation of dynamic property WP_Term::$category_nicename is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 382

Deprecated: Creation of dynamic property WP_Term::$category_parent is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 383
http://tech.learnerandtutor.com/?p=363 Deprecated: Creation of dynamic property WP_Query::$yarpp_cache_type is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/yet-another-related-posts-plugin/classes/YARPP_Cache.php on line 30

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-term-query.php on line 1118

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-term-query.php on line 1118

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-term-query.php on line 1118

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-term-query.php on line 1118

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-term-query.php on line 1118
In this tutorial session we will see the important key aspects of Struts2 Actions. If you want to get clear idea over struts 2 from the beginning please refer the below posts. Struts2 Introduction Struts2 HelloWorld Application In nutshell, Struts2 … Continue reading ]]>

Deprecated: Creation of dynamic property WP_Query::$yarpp_cache_type is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/yet-another-related-posts-plugin/classes/YARPP_Cache.php on line 30

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266
In this tutorial session we will see the important key aspects of Struts2 Actions. If you want to get clear idea over struts 2 from the beginning please refer the below posts.

Struts2 Introduction
Struts2 HelloWorld Application

In nutshell, Struts2 actions are string returning java methods. Those methods are organized in a effective way for Struts2 Framework. See a sample action below.

public String welcome() {
    return "success";
}

The above method is a valid struts2 action. So in short, any java method returns a string can be a struts2 action.

Actions mainly do three thinks.

  1. Action Encapsulates the unit of work. (i.e) The real code logic for the application will be here.
  2. Action returns control string to decide the result page. (i.e) Based on the result string from the action only the framework will decide which page to load (will explain later)
  3. Action is the place where data transfer is happening. (will explain later)

So now we want to know how the framework executes the correct java method when request arrives. Here we take tomcat apache as our example server where our application is running. If suppose the user types the url http://localhost:8080/Struts2Action  in the browser it will come to the application server (i.e) here tomcat.  Then the application server look for the application Struts2Action in its webapps folder.

Once it identifies it will look for web.xml file inside the application WEB_INF folder.Once it finds the web.xml it will read and execute based on that file.

As I explained in the HelloWorld Application the web.xml will redirect all the requests it receives to struts filter (For Struts2 only; not all). Because we added a filter to all the requests. See a sample web.xml below.

<?xml version="1.0" encoding="UTF-8" ?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
    <display-name>Struts2Action</display-name>
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

So here we redirected all the requests to org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter filter class. So the control will go to the org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter class. That class takes struts.xml as it’s configuration. So it will look for struts.xml in the class folder path.

See a sample struts.xml file below.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <package name="default" namespace="/" extends="struts-default">
        <action name="">
            <result>home.jsp</result>
        </action>
        <action name="welcome" class="com.rajesh.struts2.Welcome" method="welcome">
            <result name="success">welcome.jsp</result>
        </action>
    </package>
</struts>

Once the struts.xml is found based on that configuration the flow continuous. Clear? As per our example once the user entered the URL http://localhost:8080/Struts2Action  the control will open this struts.xml file and tries to find corresponding action. In our example we never mentioned any name after the application Context Struts2Action.

But in struts.xml we mentioned a action with empty name as below.

<action name="">
     <result>home.jsp</result>
</action>

So it will get match with our request http://localhost:8080/Struts2Action . If suppose we type the url http://localhost:8080/Struts2Action/welcome then the welcome action mentioned in the struts.xml will get executed. See that below.

<action name="welcome" class="com.rajesh.struts2.Welcome" method="welcome">
    <result name="success">welcome.jsp</result>
</action>

Now we will see how to declare an action in the struts.xml. The syntax to declare an action is

<action name={actionName} [class={className}]>
<result [name={resultValue1}]>{resultFileA}</result>
<result [name={resultValue2}]>{resultFileB}</result>
 ...
<result [name={resultValuen}]>{resultFileZ}</result>
</action>

where

actionName is

  • The name of the action
  • Refered by the url
  • Compulsary

className is

  • A Java Class to be executed while calling the respective action
  • Optional

resultName is

  • A string which comes as return type from the action method (ex “success”,”input”,”error”)
  • Optional
  • If not mentioned will take success by default

resultFile is

  • A File to render if resultName matches
  • Complulsory

With this we  finish this session for action. We will continue with a simple example in the net session.

]]>
http://tech.learnerandtutor.com/struts2-tutorial-part-3-actions-1/feed/ 0
Custom URL in Struts2 for each User http://tech.learnerandtutor.com/custom-url-in-struts2-for-each-user/ http://tech.learnerandtutor.com/custom-url-in-struts2-for-each-user/#comments Fri, 23 May 2014 08:54:19 +0000
Deprecated: Creation of dynamic property WP_Term::$cat_ID is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 378

Deprecated: Creation of dynamic property WP_Term::$category_count is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 379

Deprecated: Creation of dynamic property WP_Term::$category_description is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 380

Deprecated: Creation of dynamic property WP_Term::$cat_name is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 381

Deprecated: Creation of dynamic property WP_Term::$category_nicename is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 382

Deprecated: Creation of dynamic property WP_Term::$category_parent is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 383
http://tech.learnerandtutor.com/?p=334 Deprecated: Creation of dynamic property WP_Query::$yarpp_cache_type is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/yet-another-related-posts-plugin/classes/YARPP_Cache.php on line 30

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266
In this tutorial we will see how to assign custom URL for each user logged in Struts2. For example like ‘www.gmail.com/rajesh’  for user rajesh and ‘www.gmail.com/kumar’  for user kumar. I assume that you already have a basic knowledge over struts … Continue reading ]]>

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_author is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_date is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_date_gmt is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_content is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_title is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_excerpt is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$comment_status is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$ping_status is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_password is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_name is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$to_ping is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$pinged is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_modified is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_modified_gmt is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_content_filtered is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_parent is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$guid is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$menu_order is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_type is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_mime_type is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$comment_count is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$filter is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$author_name is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$author_profile_url is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$avatar_url is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$avatar is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$thumb is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$preview is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$file_count is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$page_url is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$page_link is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$page_url_qr is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$btnclass is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$file_size is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$file_type_icon is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$download_url is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$download_link is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$download_link_extended is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$download_link_popup is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$formatted is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$__template is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$__template_type is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WP_Query::$yarpp_cache_type is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/yet-another-related-posts-plugin/classes/YARPP_Cache.php on line 30

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266
In this tutorial we will see how to assign custom URL for each user logged in Struts2. For example like ‘www.gmail.com/rajesh’  for user rajesh and ‘www.gmail.com/kumar’  for user kumar.

I assume that you already have a basic knowledge over struts 2 framework setup and simple Struts2 application. If you are not familiar please refer Struts2 Introduction and HelloWorld application.

To create custom URL for each user first we have to set the below constants in the struts.xml.

<constant name="struts.action.extension" value=",,action" />
<constant name="struts.mapper.alwaysSelectFullNamespace" value="false" />
<constant name="struts.enable.SlashesInActionNames" value="true" />
<constant name="struts.patternMatcher" value="regex" />

The first constant struts.action.extension  is to disable the default .action extension from the URL pattern. The next two constants struts.mapper.alwaysSelectFullNamespace  and struts.enable.SlashesInActionNames for transfering the parameter from the URL. The last one struts.patternMatcher is to use regular expression in the action names (For example <action name=”Lost*do”>).

Next is we have to map the request to an action. To do this create an action like below in struts.xml.

<action name="{username}" class="com.rajesh.struts2.CustomURL" method="customUrl">
    <result name="success">welcome.jsp</result>
</action>

In the above code we mapped the string whichever comes next to the application context to the field username. For example if the request is http://localhost:8080/CustomURL/rajesh  the framework will assign the value ‘rajesh’ to username field and then will proceed like normal struts2 flow. But one important thing is it will not accept slash in that. For example if the request is http://localhost:8080/CustomURL/test/rajesh the framework will not call the above action. Because the request is for inside a package test. See the finalized code of struts.xml below.

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <constant name="struts.action.extension" value=",,action" />
    <constant name="struts.mapper.alwaysSelectFullNamespace" value="false" />
    <constant name="struts.enable.SlashesInActionNames" value="true" />
    <constant name="struts.patternMatcher" value="regex" />

    <package name="default" namespace="/" extends="struts-default">

        <action name="">
            <result name="success">home.jsp</result>
        </action>

        <action name="{username}" class="com.rajesh.struts2.CustomURL" method="customUrl">
            <result name="success">welcome.jsp</result>
        </action>

    </package>
</struts>

In the above code we added one more action with empty name “”. It is for the index page. So when we type simply the url http://localhost:8080/CustomURL the home.jsp will be displayed. See our home.jsp below.See our welcome.jsp below.

welcome.jsp

<html>

<head>
    <title>Struts 2 Custom URL</title>
    <script src="jquery-1.11.1.min.js"></script>
    <script>
        function myFunction() {
            window.location.href = $("#username").val();
        }
    </script>
</head>

<body>
    <h1>Struts 2 Custom URL</h1>
    <h3>Enter User Name</h3>
    <input type="text" id="username" />
    <input type="submit" value="Login" onclick="myFunction()" />
</body>

</html>

See our action class CustomURL.java below

CustomURL.java

package com.rajesh.struts2;

import com.opensymphony.xwork2.ActionSupport;

public class CustomURL extends ActionSupport {

    private static final long serialVersionUID = -4337790298641431230L;

    private String username;

    public String getUsername() {
        System.out.println("Inside Getter");
        return username;
    }

    public void setUsername(String username) {
        System.out.println("Inside Setter " + username);
        this.username = username;
    }

    public String customUrl() {
        return SUCCESS;
    }
}

That’s all. Simply place all the above codes in respective files and run.

custom_url_demo_1
custom_url_demo_2
For the above example I have created a complete .war file with source code. Download and just place under Apache web apps folder and run.

If you face any issues while execution please post in comments or send email to rajeshmepco@gmail.com

]]>
http://tech.learnerandtutor.com/custom-url-in-struts2-for-each-user/feed/ 1
Dynamic Drop Down List with Struts2 UI tag without AJAX, jQuery http://tech.learnerandtutor.com/dynamic-drop-down-list-with-struts2-ui-tag-without-ajax-jquery/ http://tech.learnerandtutor.com/dynamic-drop-down-list-with-struts2-ui-tag-without-ajax-jquery/#comments Sat, 17 May 2014 12:59:30 +0000
Deprecated: Creation of dynamic property WP_Term::$cat_ID is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 378

Deprecated: Creation of dynamic property WP_Term::$category_count is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 379

Deprecated: Creation of dynamic property WP_Term::$category_description is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 380

Deprecated: Creation of dynamic property WP_Term::$cat_name is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 381

Deprecated: Creation of dynamic property WP_Term::$category_nicename is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 382

Deprecated: Creation of dynamic property WP_Term::$category_parent is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 383
http://tech.learnerandtutor.com/?p=279 Deprecated: Creation of dynamic property WP_Query::$yarpp_cache_type is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/yet-another-related-posts-plugin/classes/YARPP_Cache.php on line 30

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266
In this tutorial session we will create simple application to load the drop down list options dynamically from the server side. Also we will use only struts 2 UI tag, not ajax or jquery. In our previous example we used … Continue reading ]]>

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_author is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_date is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_date_gmt is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_content is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_title is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_excerpt is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$comment_status is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$ping_status is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_password is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_name is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$to_ping is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$pinged is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_modified is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_modified_gmt is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_content_filtered is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_parent is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$guid is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$menu_order is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_type is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_mime_type is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$comment_count is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$filter is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$author_name is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$author_profile_url is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$avatar_url is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$avatar is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$thumb is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$preview is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$file_count is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$page_url is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$page_link is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$page_url_qr is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$btnclass is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$file_size is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$file_type_icon is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$download_url is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$download_link is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$download_link_extended is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$download_link_popup is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$formatted is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$__template is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$__template_type is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WP_Query::$yarpp_cache_type is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/yet-another-related-posts-plugin/classes/YARPP_Cache.php on line 30

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266
In this tutorial session we will create simple application to load the drop down list options dynamically from the server side. Also we will use only struts 2 UI tag, not ajax or jquery.

In our previous example we used ajax call to load the drop down options from server side via jquery. Here not.

We assume that we already have a very basic knowledge over struts2 setup. If not please refer our Hello World application. To achieve our goal first create a simple jsp file as below.

home.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
    <html>

    <head>
        <script src="js/jquery-1.11.1.min.js"></script>
        <script>
            $(function () {

                //We commented this code because we are directly loading the drop down options via struts UI tag
                /*$('#state').html('');
		$.getJSON("readStates", function(res) {
			for ( var i = 0; i < res.states.length; i++) {
				$('#state').append(
						'<option value='+res.states[i]+'>' + res.states[i]
								+ '</option>');
			}
		});*/

                $("#state").change(
                    function () {
                        $('#district').html('');
                        var state = {
                            "state": $("#state").val()
                        };
                        $.ajax({
                            url: "readDistricts",
                            data: JSON.stringify(state),
                            dataType: 'json',
                            contentType: 'application/json',
                            type: 'POST',
                            async: true,
                            success: function (res) {
                                console.log(res.districts.length);
                                for (var i = 0; i < res.districts.length; i++) {
                                    console.log(" " + res.districts[i]);
                                    $('#district').append(
                                        '<option value=' + res.districts[i] + '>' + res.districts[i] + '</option>');
                                }
                            }
                        });
                    });
            });
        </script>
    </head>

    <body>
        <h3>Struts 2 Dynamic Drop down List</h3>
        <s:select label="What's your State" headerKey="-1" headerValue="Select State" list="states" name="state" value="defaultState" />
District :<select id="district"></select>
    </body>

    </html>

Then in web.xml divert all requests to struts2 framework as usual. See the web.xml below.

Also please note that we never mentioned the welcome file list in our web.xml. (i.e) Home page. We will handle that in our struts.xml.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	id="WebApp_ID" version="3.0">
	<display-name>DynamicData</display-name>

	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>

	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

</web-app>

See our struts.xml below.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <package name="default" namespace="/" extends="json-default">
        <interceptors>
            <interceptor-stack name="defaultStack">
                <interceptor-ref name="json">
                    <param name="enableSMD">true</param>
                </interceptor-ref>
            </interceptor-stack>
        </interceptors>
        <default-interceptor-ref name="defaultStack" />

        <action name="readDistricts" class="com.rajesh.struts2.ReadData" method="dbDistricts">
            <result type="json"></result>
        </action>

        <action name="" class="com.rajesh.struts2.ReadData" method="dbStates">
            <result name="success">home.jsp</result>
        </action>

    </package>
</struts>

What is the change we made in our struts.xml. We mapped a empty action to a class and if the result is success then we are rendering the page home.jsp to the browser. Why instead of mentioning the home page in web.xml we mentioned in struts.xml? There is a reason behind that. We will discuss it later.

Now see the corresponding action class ReadData.java below.

ReadData.java

package com.rajesh.struts2;

import java.util.ArrayList;

import com.opensymphony.xwork2.ActionSupport;

public class ReadData extends ActionSupport {

    private static final long serialVersionUID = -8819352697303500472L;
    private String state;
    private ArrayList < String > districts;
    private ArrayList < String > states;

    public ReadData() {
        System.out.println("Inside Constructor ");
        states = new ArrayList < String > ();
        states.add("tamilnadu");
        states.add("kerala");
        states.add("karnataka");
        states.add("delhi");
    }

    public String getDefaultState() {
        System.out.println("Inside default State Getter");
        return "Select State";
    }

    public ArrayList < String > getDistricts() {
        return districts;
    }

    public ArrayList < String > getStates() {
        return states;
    }

    public void setState(String state) {
        System.out.println("Inside Setter " + state);
        this.state = state;
    }

    public String dbDistricts() {
        System.out.println("Getting Districts for " + state);
        // Do the database code or business logic here.
        districts = new ArrayList < String > ();
        if (state.equalsIgnoreCase("tamilnadu")) {
            districts.add("chennai");
            districts.add("madurai");
            districts.add("trichy");
            districts.add("Covai");
            districts.add("Pudukkottai");
        } else if (state.equalsIgnoreCase("kerala")) {
            districts.add("allappey");
            districts.add("trivandrum");
            districts.add("kozhikkode");
        } else if (state.equalsIgnoreCase("karnataka")) {
            districts.add("bangalore");
            districts.add("Bommanahalli");
            districts.add("Mysore");
        } else if (state.equalsIgnoreCase("-1")) {} else {
            districts.add("District 1");
            districts.add("District 2");
            districts.add("District 3");
            districts.add("District 4");
            districts.add("District 5");
        }
        return SUCCESS;
    }

    public String dbStates() {
        // Do the database code or business logic here.
        System.out.println("Inside dbStates method");
        return SUCCESS;
    }
}

That’s all the setup. But we need to know why we rendered the home page via struts.xml? Also the empty action pointed to the method dbStates() is not doing anything. It simply returns a “success”  string. Then why?

The answer is here. In our home page home.jsp we used struts2 tag for the drop down list. See the code

<s:select label="What's your State" headerKey="-1" 
headerValue="Select State" list="states" name="state" value="defaultState" />

So if we render the page directly from web.xml it don’t have any values for the drop down list options. We need to run some code before rendering the page. Right? That’s why we diverted the empty action to ReadData.java . See the constructor of the ReadData.java .

public ReadData() {
    System.out.println("Inside Constructor ");
    states = new ArrayList < String > ();
    states.add("tamilnadu");
    states.add("kerala");
    states.add("karnataka");
    states.add("delhi");
}

So when we point to this class it will automatically creates an arraylist and fills it with the required value. So after this constructor executed the control will go to the dbStates() method. There we simply returns a “success” string. So the control will come back to struts.xml. There we mentioned <result name=”success”>home.jsp</result> . So it will render the page home.jsp to the browser.

But the difference is while render the page home.jsp. When it comes to the Struts UI tag

<s:select label="What's your State" headerKey="-1" 
headerValue="Select State" list="states" name="state" value="defaultState" />

it will look for method getStat() in the action class. Because we mentioned the name as state. So as per struts 2 design pattern it will try to run a method by name getStat() .

As we have that method in our action class it will get executed. Then with the return value as arraylist it will be filled inside the drop down list by the framework and rendered to the browser .

Simply run the code as below http://localhost:8080/DynamicData/ and see the result in action.
struts2UI_dropdown

For the above example I have created a complete .war file with source code. Download and just place under Apache web apps folder and run.

If you face any issues while execution please post in comments or send me email to rajeshmepco@gmail.com

]]>
http://tech.learnerandtutor.com/dynamic-drop-down-list-with-struts2-ui-tag-without-ajax-jquery/feed/ 3
Dynamic Drop Down List with Struts 2 and AJAX http://tech.learnerandtutor.com/dynamic-drop-down-list-with-struts-2-and-ajax/ http://tech.learnerandtutor.com/dynamic-drop-down-list-with-struts-2-and-ajax/#comments Fri, 16 May 2014 10:36:41 +0000
Deprecated: Creation of dynamic property WP_Term::$cat_ID is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 378

Deprecated: Creation of dynamic property WP_Term::$category_count is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 379

Deprecated: Creation of dynamic property WP_Term::$category_description is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 380

Deprecated: Creation of dynamic property WP_Term::$cat_name is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 381

Deprecated: Creation of dynamic property WP_Term::$category_nicename is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 382

Deprecated: Creation of dynamic property WP_Term::$category_parent is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 383
http://tech.learnerandtutor.com/?p=252 Deprecated: Creation of dynamic property WP_Query::$yarpp_cache_type is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/yet-another-related-posts-plugin/classes/YARPP_Cache.php on line 30

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266
In this tutorial session we will create simple application to load the drop down list options dynamically from the server side. Also we will load one drop down list based on the selection of another drop down list. We use AJAX, JQuery, … Continue reading ]]>

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_author is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_date is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_date_gmt is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_content is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_title is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_excerpt is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$comment_status is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$ping_status is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_password is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_name is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$to_ping is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$pinged is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_modified is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_modified_gmt is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_content_filtered is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_parent is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$guid is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$menu_order is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_type is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_mime_type is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$comment_count is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$filter is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$author_name is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$author_profile_url is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$avatar_url is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$avatar is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$thumb is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$preview is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$file_count is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$page_url is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$page_link is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$page_url_qr is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$btnclass is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$file_size is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$file_type_icon is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$download_url is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$download_link is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$download_link_extended is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$download_link_popup is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$formatted is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$__template is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$__template_type is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WP_Query::$yarpp_cache_type is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/yet-another-related-posts-plugin/classes/YARPP_Cache.php on line 30

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266
In this tutorial session we will create simple application to load the drop down list options dynamically from the server side. Also we will load one drop down list based on the selection of another drop down list.

We use AJAX, JQuery, Struts2 and json methodologies to achieve this. Also we assume the basic setup for struts2 is already ready. (i.e) All necessary jar files placed in right place. If you want more info please refer the Struts 2 Hello World application.

Create a simple html file with the below content.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>

<head>
    <script src="js/jquery-1.11.1.min.js"></script>
    <script>
        $(function () {
            $('#state').html('');
            $.getJSON("readStates", function (res) {
                for (var i = 0; i < res.states.length; i++) {
                    $('#state').append(
                        '<option value=' + res.states[i] + '>' + res.states[i] + '</option>');
                }
            });

            $("#state").change(
                function () {
                    $('#district').html('');
                    var state = {
                        "state": $("#state").val()
                    };
                    $.ajax({
                        url: "readDistricts",
                        data: JSON.stringify(state),
                        dataType: 'json',
                        contentType: 'application/json',
                        type: 'POST',
                        async: true,
                        success: function (res) {
                            console.log(res.districts.length);
                            for (var i = 0; i < res.districts.length; i++) {
                                console.log(" " + res.districts[i]);
                                $('#district').append(
                                    '<option value=' + res.districts[i] + '>' + res.districts[i] + '</option>');
                            }
                        }
                    });
                });
        });
    </script>
</head>

<body>
    <h3>Struts 2 Dynamic Drop down List</h3>
    State :
    <select id="state"></select>District :
    <select id="district"></select>
</body>

</html>

Create struts.xml file with the below content and place it under class folder root path.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <package name="default" namespace="/" extends="json-default">
        <interceptors>
            <interceptor-stack name="defaultStack">
                <interceptor-ref name="json">
                    <param name="enableSMD">true</param>
                </interceptor-ref>
            </interceptor-stack>
        </interceptors>
        <default-interceptor-ref name="defaultStack" />

        <action name="readDistricts" class="com.rajesh.struts2.ReadData" method="dbDistricts">
            <result type="json"></result>
        </action>

        <action name="readStates" class="com.rajesh.struts2.ReadData" method="dbStates">
            <result type="json"></result>
        </action>

    </package>
</struts>

As we are sending and receiving the data as json object we need to enable the json option in the struts.xml file. So implement a custom interceptor and json result type as above. (To do this struts2-json-plugin.jar is required)

See our web.xml file below.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	id="WebApp_ID" version="3.0">
	<display-name>HelloStruts</display-name>

	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>

	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<welcome-file-list>
		<welcome-file>/index.html</welcome-file>
	</welcome-file-list>
</web-app>

Also create the java class ReadData which contains the business logic with the below code and place under com.rajesh.struts2 package in the class folder.

package com.rajesh.struts2;

import com.opensymphony.xwork2.ActionSupport;

public class ReadData extends ActionSupport {

    private static final long serialVersionUID = -8819352697303500472L;

    private String state, district;
    private String states[], districts[];

    public String[] getDistricts() {
        return districts;
    }

    public void setDistricts(String[] districts) {
        this.districts = districts;
    }

    public String[] getStates() {
        return states;
    }

    public void setStates(String[] states) {
        this.states = states;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        System.out.println("Inside Setter " + state);
        this.state = state;
    }

    public String getDistrict() {
        return district;
    }

    public void setDistrict(String district) {
        this.district = district;
    }

    public String dbDistricts() {
        System.out.println("Getting Districts for " + state);
        //Do the database code or business logic here.
        districts = new String[5];
        if (state.equalsIgnoreCase("tamilnadu")) {
            districts[0] = "chennai";
            districts[1] = "madurai";
            districts[2] = "trichy";
            districts[3] = "Covai";
            districts[4] = "Pudukkottai";
        } else if (state.equalsIgnoreCase("kerala")) {
            districts[0] = "allappey";
            districts[1] = "trivandrum";
            districts[2] = "kozhikkode";
            districts[3] = "District 4";
            districts[4] = "District 5";
        } else if (state.equalsIgnoreCase("karnataka")) {
            districts[0] = "bangalore";
            districts[1] = "Bommanahalli";
            districts[2] = "Mysore";
            districts[3] = "District 4";
            districts[4] = "District 5";
        } else {
            districts[0] = "District 1";
            districts[1] = "District 2";
            districts[2] = "District 3";
            districts[3] = "District 4";
            districts[4] = "District 5";
        }
        return SUCCESS;
    }

    public String dbStates() {
        //Do the database code or business logic here.
        states = new String[5];
        states[0] = "tamilnadu";
        states[1] = "kerala";
        states[2] = "karnataka";
        states[3] = "delhi";
        states[4] = "kashmir";
        return SUCCESS;
    }
}

See the complete structure of the folder below.

dynamic_dd_structure

That’s all. Simply run the program as below and see the result in action.

http://localhost:8080/DynamicData/

dynamic_dd

For the above example I have created a complete .war file with source code. Download and just place under Apache web apps folder and run.

If you face any issues while execution please post in comments or send me email to rajeshmepco@gmail.com

]]>
http://tech.learnerandtutor.com/dynamic-drop-down-list-with-struts-2-and-ajax/feed/ 5
Struts2 Tutorial Part 2 – HelloWorld application http://tech.learnerandtutor.com/struts2-tutorial-part-2-hello-world-application/ http://tech.learnerandtutor.com/struts2-tutorial-part-2-hello-world-application/#respond Thu, 15 May 2014 11:04:38 +0000
Deprecated: Creation of dynamic property WP_Term::$cat_ID is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 378

Deprecated: Creation of dynamic property WP_Term::$category_count is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 379

Deprecated: Creation of dynamic property WP_Term::$category_description is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 380

Deprecated: Creation of dynamic property WP_Term::$cat_name is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 381

Deprecated: Creation of dynamic property WP_Term::$category_nicename is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 382

Deprecated: Creation of dynamic property WP_Term::$category_parent is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 383
http://tech.learnerandtutor.com/?p=225 Deprecated: Creation of dynamic property WP_Query::$yarpp_cache_type is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/yet-another-related-posts-plugin/classes/YARPP_Cache.php on line 30

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266
In this tutorial session we will create simple helloStruts application. To create a simple struts2 application follow the steps below. I assume we already have a basic knowledge over jsp/servlet workflow. As per jsp/servlet whenever we create new jsp/servlet application, … Continue reading ]]>

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_author is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_date is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_date_gmt is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_content is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_title is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_excerpt is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$comment_status is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$ping_status is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_password is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_name is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$to_ping is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$pinged is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_modified is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_modified_gmt is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_content_filtered is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_parent is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$guid is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$menu_order is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_type is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_mime_type is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$comment_count is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$filter is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$author_name is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$author_profile_url is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$avatar_url is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$avatar is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$thumb is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$preview is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$file_count is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$page_url is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$page_link is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$page_url_qr is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$btnclass is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$file_size is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$file_type_icon is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$download_url is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$download_link is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$download_link_extended is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$download_link_popup is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$formatted is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$__template is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$__template_type is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WP_Query::$yarpp_cache_type is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/yet-another-related-posts-plugin/classes/YARPP_Cache.php on line 30

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266
In this tutorial session we will create simple helloStruts application. To create a simple struts2 application follow the steps below.

I assume we already have a basic knowledge over jsp/servlet workflow.

As per jsp/servlet whenever we create new jsp/servlet application, the control will look for web.xml inside the application context WebContent/WEB-INF folder. Application Context is nothing but the application name referred by the servlet container (ex. Apache tomcat). See the sample web.xml below.

<?xml version="1.0" encoding="UTF-8" ?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
    <display-name>helloStruts</display-name>
    <welcome-file-list>
        <welcome-file>/index.html</welcome-file>
    </welcome-file-list>
</web-app>

So the first step we need to do is to divert all requests from web.xml to our struts2 config file. The config file for struts 2  is struts.xml which is fixed. (i.e) We cannot rename the file.

How can we divert the request?

We knew that in web.xml we can add filters to url patterns to do pre/post processing. Here we will use that approach to divert the requests. (i.e) We add a filter to all the url patterns and divert to struts 2 config file.  For that add the below code in web.xml.

<filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

What we done in the above code is, we added a filter struts2 to all the patterns (*). So that filter will call the class org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.

No more changes required in the web.xml file. See the consolidated web.xml file below.

<?xml version="1.0" encoding="UTF-8" ?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
    <display-name>HelloStruts</display-name>

    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <welcome-file-list>
        <welcome-file>/index.html</welcome-file>
    </welcome-file-list>
</web-app>

Next step is we have to create the configuration file struts.xml for all the requests. As discussed the name of the config file is struts.xml which should be in the class folder root path. See the struts.xml below.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <package name="default" namespace="/" extends="struts-default">
        <action name="home">
            <result>/welcome.jsp</result>
        </action>
    </package>
</struts>

Then create a file with above code and save it with name struts.xml and place it under class folder root path. (i.e) WebContent/WEB-INF folder.

Next step we need to do is adding the necessary jar files to the library. Download the struts2 library files and place it under lib folder inside WEB-INF folder. All the jar files are not required for basic application. For most application below listed jars are enough. Later if we require we can add the necessary jars to the library.

struts2-jars

Next step is to create a necessary jsp, html files. Create the below two files namely index.html and welcome.jsp with the below content and place it under WebContent folder.

index.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
    <META HTTP-EQUIV="Refresh" CONTENT="0;URL=home">
</head>
<body>
    <h3>One moment please.</h3>
</body>
</html>

 welcome.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>

    <head>
        <style>
            h1 {
                color: blue;
            }
        </style>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>Welcome to Struts2</title>
    </head>

    <body>
        <h1>Welcome to Struts2</h1>
    </body>

    </html>

That’s all. See the final structure of the folders below.

struts2-structure

Run the project in eclipse by Run as -> Run on server in eclipse or place the folder structure above in the web-apps folder in tomcat. Then type the below code in the browser (if tomcat is running under port 8080).

http://localhost:8080/helloStruts

You will get the below screen.
struts2-home

For the above method I have created a complete .war file with source code. Download and just place under Apache web apps folder and run.

If you have any issues while execution please post in comments or send mail to rajeshmepco@gmail.com.

]]>
http://tech.learnerandtutor.com/struts2-tutorial-part-2-hello-world-application/feed/ 0
Send HTML Form data to struts2 action by Model Driven method http://tech.learnerandtutor.com/send-html-form-data-to-struts2-action-by-model-driven-method/ http://tech.learnerandtutor.com/send-html-form-data-to-struts2-action-by-model-driven-method/#respond Sun, 11 May 2014 12:28:49 +0000
Deprecated: Creation of dynamic property WP_Term::$cat_ID is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 378

Deprecated: Creation of dynamic property WP_Term::$category_count is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 379

Deprecated: Creation of dynamic property WP_Term::$category_description is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 380

Deprecated: Creation of dynamic property WP_Term::$cat_name is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 381

Deprecated: Creation of dynamic property WP_Term::$category_nicename is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 382

Deprecated: Creation of dynamic property WP_Term::$category_parent is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 383
http://tech.learnerandtutor.com/?p=206 Deprecated: Creation of dynamic property WP_Query::$yarpp_cache_type is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/yet-another-related-posts-plugin/classes/YARPP_Cache.php on line 30

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266
HTML form data can be transferred to Struts2 Action in three ways. 1. Normal Method with simple getters() and setters() 2. Object Backed Method 3. Model Driven Method We will see the third method now. Model Driven Method Another way … Continue reading ]]>

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_author is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_date is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_date_gmt is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_content is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_title is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_excerpt is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$comment_status is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$ping_status is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_password is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_name is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$to_ping is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$pinged is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_modified is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_modified_gmt is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_content_filtered is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_parent is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$guid is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$menu_order is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_type is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_mime_type is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$comment_count is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$filter is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$author_name is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$author_profile_url is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$avatar_url is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$avatar is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$thumb is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$preview is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$file_count is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$page_url is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$page_link is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$page_url_qr is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$btnclass is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$file_size is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$file_type_icon is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$download_url is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$download_link is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$download_link_extended is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$download_link_popup is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$formatted is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$__template is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$__template_type is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WP_Query::$yarpp_cache_type is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/yet-another-related-posts-plugin/classes/YARPP_Cache.php on line 30

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266
HTML form data can be transferred to Struts2 Action in three ways.

1. Normal Method with simple getters() and setters()
2. Object Backed Method
3. Model Driven Method

We will see the third method now.

Model Driven Method

Another way of transfering the form data directly inside the object is Model Driven approach. This approach is the best among all the three because for this approach no need to specify the field names deeply. We can define the fields as usual.

Create a simple jsp file index.jsp with struts2 form tags as below. Note that we never named the fields deeply like what we did earlier for object backed approach.

<%@ taglib prefix="s" uri="/struts-tags" %>
    <html>

    <head>
        <title>Struts Data Transfer - Normal Transfer Method</title>
    </head>

    <body>
        <h3>Struts Data Transfer - Normal Transfer Method</h3>
        <s:form action="register">
            <s:textfield name="fname" label="First name" />
            <s:textfield name="lname" label="Last name" />
            <s:textfield name="email" label="Email" />
            <s:textfield name="age" label="Age" />
            <s:submit />
        </s:form>
    </body>

    </html>

So As per the jsp page when the form is getting submitted it will look for an action with name register. See out struts.xml below.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="false" />
    <constant name="struts.custom.i18n.resources" value="ApplicationResources" />

    <package name="default" extends="struts-default" namespace="/">
        <action name="register" class="com.rajesh.dataTransfer.Register" method="register">
            <result name="success">success.jsp</result>
        </action>
    </package>
</struts>

In our Struts.xml we pointed the register action to register() method in the Register class. So when the form is getting submitted the control will go the register() method.  Now see the Register action class below.

package com.rajesh.dataTransfer;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@SuppressWarnings("rawtypes")
public class Register extends ActionSupport implements ModelDriven {
    private static final long serialVersionUID = -367986889632883043L;
    private User user = new User();

    public User getModel() {
        return user;
    }

    public String register() {
        System.out.println("Inside Action Method");
        System.out.println(user.getFname());
        System.out.println(user.getLname());
        System.out.println(user.getAge());
        System.out.println(user.getEmail());
        return SUCCESS;
    }
}

In our register action class we made two changes from Object backed method. One is the class implements ModelDriven interface and it’s method getModel(). So while the control look for setter methods it will run the getModel() method. Then it will take care of the data movement from form to object. Finally it will simply return the user object.

How this is achieved is by the framework interface ModelDriven. One important thing here is we have to create the User Object explicitly outside the method. And as usual the User class should have all the setters() and getters() for the required variables. See our User class below.

package com.rajesh.dataTransfer;

public class User {
    private String fname;
    private String lname;
    private int age;
    private String email;

    public String getFname() {
        return fname;
    }

    public void setFname(String fname) {
        this.fname = fname;
    }

    public String getLname() {
        return lname;
    }

    public void setLname(String lname) {
        this.lname = lname;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

}

So the Struts2 framework will execute the getModel() method before executing the actual action method, in our case register(). So while executing the getModel() method it will look for all the submitted data and if it matches with the method return type then  it will call the corresponding setter methods and return as object. Thus all the variables will be assigned to the user object and then returned.

In our action method we just displayed the values in the console, then returning success message as result name. So as we mentioned the result page is success.jsp for string “SUCCESS” struts2 will load the page success.jsp to the user. See our success.jsp below.

<%@ taglib prefix="s" uri="/struts-tags" %>
    <html>

    <head>
        <title>Struts Data Transfer - Normal Transfer Method</title>
    </head>

    <body>
        <h3>Struts Data Transfer - Normal Transfer Method</h3>
        <h4>User Data saved successfully</h4>
        First Name:<s:property value="fname" />
        <br>Last Name:<s:property value="lname" />
        <br>Email:<s:property value="email" />
        <br>Age:<s:property value="age" />
        <br>
    </body>

    </html>

What will happen for those tags like <s:property value =”fname”/> . Struts2 will look for getter() methods in the corresponding action class. As we discussed earlier due to ModelDriven interface implementation both the getter and setter process will be take care by the framework.

So it will run the getModel method and runs the getter methods inside the User object then it will return the object with all values. Then all the fields will be filled then returned to the user as a rendered page.

For the above method I have created a complete .war file with source code. Download and just place under Apache web apps folder and run.

If you have any issues while execution please post in comments or send mail to rajeshmepco@gmail.com.

]]>
http://tech.learnerandtutor.com/send-html-form-data-to-struts2-action-by-model-driven-method/feed/ 0
Send HTML Form data to struts2 action by Object Backed Method http://tech.learnerandtutor.com/send-html-form-data-to-struts2-action-java-objects-directly/ http://tech.learnerandtutor.com/send-html-form-data-to-struts2-action-java-objects-directly/#respond Sun, 11 May 2014 11:48:21 +0000
Deprecated: Creation of dynamic property WP_Term::$cat_ID is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 378

Deprecated: Creation of dynamic property WP_Term::$category_count is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 379

Deprecated: Creation of dynamic property WP_Term::$category_description is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 380

Deprecated: Creation of dynamic property WP_Term::$cat_name is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 381

Deprecated: Creation of dynamic property WP_Term::$category_nicename is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 382

Deprecated: Creation of dynamic property WP_Term::$category_parent is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 383
http://tech.learnerandtutor.com/?p=176 Deprecated: Creation of dynamic property WP_Query::$yarpp_cache_type is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/yet-another-related-posts-plugin/classes/YARPP_Cache.php on line 30

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266
HTML form data can be transferred to Struts2 Action in three ways. 1. Normal Method with simple getters() and setters() 2. Object Backed Method 3. Model Driven Method We will see the second method now. Object Backed Method Create a … Continue reading ]]>

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_author is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_date is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_date_gmt is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_content is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_title is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_excerpt is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$comment_status is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$ping_status is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_password is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_name is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$to_ping is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$pinged is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_modified is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_modified_gmt is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_content_filtered is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_parent is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$guid is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$menu_order is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_type is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_mime_type is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$comment_count is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$filter is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$author_name is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$author_profile_url is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$avatar_url is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$avatar is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$thumb is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$preview is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$file_count is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$page_url is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$page_link is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$page_url_qr is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$btnclass is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$file_size is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$file_type_icon is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$download_url is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$download_link is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$download_link_extended is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$download_link_popup is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$formatted is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$__template is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$__template_type is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WP_Query::$yarpp_cache_type is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/yet-another-related-posts-plugin/classes/YARPP_Cache.php on line 30

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266
HTML form data can be transferred to Struts2 Action in three ways.

1. Normal Method with simple getters() and setters()
2. Object Backed Method
3. Model Driven Method

We will see the second method now.

Object Backed Method

Create a simple jsp file index.jsp with struts2 form tags as below. The only difference from normal method and Object backed method is while naming the fields the name should be defined deeply. See below.

<%@ taglib prefix="s" uri="/struts-tags" %>
    <html>

    <head>
        <title>Struts Data Transfer - Normal Transfer Method</title>
    </head>

    <body>
        <h3>Struts Data Transfer - Normal Transfer Method</h3>
        <s:form action="register">
            <s:textfield name="user.fname" label="First name" />
            <s:textfield name="user.lname" label="Last name" />
            <s:textfield name="user.email" label="Email" />
            <s:textfield name="user.age" label="Age" />
            <s:submit />
        </s:form>
    </body>

    </html>

The meaning of the above code is all the values will go inside the user object. So As per the above page when the form is getting submitted it will look for an action with name register.

Now See our struts.xml below.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="false" />
    <constant name="struts.custom.i18n.resources" value="ApplicationResources" />

    <package name="default" extends="struts-default" namespace="/">
        <action name="register" class="com.rajesh.dataTransfer.Register" method="register">
            <result name="success">success.jsp</result>
        </action>
    </package>
</struts>

In our Struts.xml we pointed the register action to register() method in the Register class. So when the form is getting submitted the control will execute the register() method. As per struts2 design the control will search the methods for all available data fields. In our case we named all the fields deeply like user.fname,user.lname.

So the control will look for method setUser() in the action class that takes an object as parameter which have variables matching to the data fields. (i.e) An object contains lname, fname, age and email as members will be the parameter for setUser() method.

Also the parameter object class should contains setters() and getters() method for required variables. So while executing the setUser() method the control will call the setters inside the User object for corresponding data fields.

Now see our Register class and User Class below.

package com.rajesh.dataTransfer;
import com.opensymphony.xwork2.ActionSupport;

public class Register extends ActionSupport {
    private static final long serialVersionUID = -367986889632883043L;
    private User user;

    public void setUser(User user) {
        this.user = user;
    }

    public User getUser() {
        return user;
    }

    public String register() {
        System.out.println("Inside Action Method");
        System.out.println(user.getFname());
        System.out.println(user.getLname());
        System.out.println(user.getAge());
        System.out.println(user.getEmail());
        return SUCCESS;
    }
}
package com.rajesh.dataTransfer;

public class User {
    private String fname;
    private String lname;
    private int age;
    private String email;

    public String getFname() {
        return fname;
    }

    public void setFname(String fname) {
        this.fname = fname;
    }

    public String getLname() {
        return lname;
    }

    public void setLname(String lname) {
        this.lname = lname;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

}

As we know the Struts2 framework will execute the setUser() method before executing the actual action method register(). So while executing the setUser() method it will call the setter methods inside the User Object. Here user.setLname(), user.setFname(), user.setAge() and user.setEmail(). So all the variables will be assigned to the user object.

In our action method we just displayed the values in the console, then returning success message as result name.
So as we mentioned the result page is success.jsp for string “SUCCESS” struts2 will load the page success.jsp to the user. See our success.jsp below.

<%@ taglib prefix="s" uri="/struts-tags" %>
    <html>

    <head>
        <title>Struts Data Transfer - Normal Transfer Method</title>
    </head>

    <body>
        <h3>Struts Data Transfer - Normal Transfer Method</h3>
        <h4>User Data saved successfully</h4>
        First Name:<s:property value="user.fname" />
        <br>Last Name:<s:property value="user.lname" />
        <br>Email:<s:property value="user.email" />
        <br>Age:<s:property value="user.age" />
        <br>
    </body>

    </html>

What will happen for those tags like <s:property value =”user.fname”/> . Struts2 will look for get methods in the corresponding action class. As we discussed earlier due to the name of the field it will look for method getUser() ninside the action class.  As we have getUser() method inside the class it will return the object with all values. Then all the fields will be filled then returned to the user.

For the above method I have created a complete .war file with source code. Download and just place under Apache web apps folder and run.

If you have any issues while execution please post in comments or send mail to rajeshmepco@gmail.com.

]]>
http://tech.learnerandtutor.com/send-html-form-data-to-struts2-action-java-objects-directly/feed/ 0