Saturday, March 1, 2014

How to create Java Struts Web application Project using Struts Framework & Java EE 7 in Netbeans IDE and with GlassFish Web Application Server

In this tutorial, you will learn how to create a java struts web application project in Netbeans IDE by using Java Struts MVC framework and Java ee technology. How to deploy and run struts web application in Glass-Fish Web-application-server. 

What is Apache Struts: Java Struts is an open-source MVC (Model View Controller) framework for modern Java EE web applications development. Struts framework favors convention over configuration, it is extensible using a plugin architecture and ships with REST, AJAX and JSON plugin support. Apache struts has much more support for developing large scale, complicated and stable business web applications.

Technologies and web development software used in this tutorial are
1-  Netbeans IDE 7.4
2- Java EE 7
3- Struts 1.3
4- JSP
5- XML
6- html

In the NetBeans IDE, Click on File menu -> New Project or press Ctrl + shift + N buttons to create a new project. After clicking a new window will open. 
In Categories section select Java Web and in project section select Web Application as showing in below snapshots. Clink next for next process

Find Java Struts tutorial to learn how to create Struts Java ee 7 web application project in NetBeans IDE and GlassFish Web application Server. Learn Java web application development using Struts Framework. Netbeans IDE Tutorial, Struts tutorial, Java Server, Java EE 7 tutorials, Struts framework tutorial, Strut, java framework, java struts, struts java, java web action, Struts java web application development

Here in Project name and location step, give Project name StrutsWebApplication, give Project location where project files will save like D:\NetBeansProjects, in project Folder, a folder with your project name will automatically generate.

Find Java Struts tutorial to learn how to create Struts Java ee 7 web application project in NetBeans IDE and GlassFish Web application Server. Learn Java web application development using Struts Framework. Netbeans IDE Tutorial, Struts tutorial, Java Server, Java EE 7 tutorials, Struts framework tutorial, Strut, java framework, java struts, struts java, java web action, Struts java web application development

In this Server and Setting step, select your required web server, Select GlassFish Server 4.0 for this tutorial.
In Java EE Version, you can select your required version like Java EE 6, Java EE 7. We will use Java EE 7 version.
In Context Path, a custom path can also give. By default, your /project name is default path which will show in browser web address. Select by default path and click next

Find Java Struts tutorial to learn how to create Struts Java ee 7 web application project in NetBeans IDE and GlassFish Web application Server. Learn Java web application development using Struts Framework. Netbeans IDE Tutorial, Struts tutorial, Java Server, Java EE 7 tutorials, Struts framework tutorial, Strut, java framework, java struts, struts java, java web action, Struts java web application development
Here in Framework option check the Struts 1.3. and in Struts configuration options, select default options. Here you can customized options with respect to your project requirement and designing. here you can also enable and add Struts TLDs library support. Clink Finsh to create project 

Find Java Struts tutorial to learn how to create Struts Java ee 7 web application project in NetBeans IDE and GlassFish Web application Server. Learn Java web application development using Struts Framework. Netbeans IDE Tutorial, Struts tutorial, Java Server, Java EE 7 tutorials, Struts framework tutorial, Strut, java framework, java struts, struts java, java web action, Struts java web application development


Find Java Struts tutorial to learn how to create Struts Java ee 7 web application project in NetBeans IDE and GlassFish Web application Server. Learn Java web application development using Struts Framework. Netbeans IDE Tutorial, Struts tutorial, Java Server, Java EE 7 tutorials, Struts framework tutorial, Strut, java framework, java struts, struts java, java web action, Struts java web application development, Java IDE, Java development softwares

Congratulation, you have successfully created a Java Struts Web application project in Netbeans IDE. All required project files like web.xml, struts-config.xml, validation.xml, validator-ruls.xml, title-defs.xml and two JSP pages index.jsp & welcomeStruts.jsp have been created as showing in below


Find Java Struts tutorial to learn how to create Struts Java ee 7 web application project in NetBeans IDE and GlassFish Web application Server. Learn Java web application development using Struts Framework. Netbeans IDE Tutorial, Struts tutorial, Java Server, Java EE 7 tutorials, Struts framework tutorial, Strut, java framework, java struts, struts java, java web action, Struts java web application development, Java IDE, Java development softwares, struts development tool, j2ee web development tool

These are by default created codes

welcomeStruts.jsp

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>

<html:html lang="true">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title><bean:message key="welcome.title"/></title>
        <html:base/>
    </head>
    <body style="background-color: white">
        
        <logic:notPresent name="org.apache.struts.action.MESSAGE" scope="application">
            <div  style="color: red">
                ERROR:  Application resources not loaded -- check servlet container
                logs for error messages.
            </div>
        </logic:notPresent>
        
        <h3><bean:message key="welcome.heading"/></h3>
        <p><bean:message key="welcome.message"/></p>
       
    </body>
</html:html>

index.jsp web page

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

<jsp:forward page="Welcome.do"/>


web.xml       Codes
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <servlet>
        <servlet-name>action</servlet-name>
        <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
        <init-param>
            <param-name>config</param-name>
            <param-value>/WEB-INF/struts-config.xml</param-value>
        </init-param>
        <init-param>
            <param-name>debug</param-name>
            <param-value>2</param-value>
        </init-param>
        <init-param>
            <param-name>detail</param-name>
            <param-value>2</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>action</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>


struts-config.xml

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd">


<struts-config>
    <form-beans>
    
    </form-beans>
    
    <global-exceptions>
    
    </global-exceptions>

    <global-forwards>
        <forward name="welcome"  path="/Welcome.do"/>
    </global-forwards>

    <action-mappings>
        <action path="/Welcome" forward="/welcomeStruts.jsp"/>
    </action-mappings>
    
    <controller processorClass="org.apache.struts.tiles.TilesRequestProcessor"/>

    <message-resources parameter="com/myapp/struts/ApplicationResource"/>    
    
    <!-- ========================= Tiles plugin ===============================-->
    <!--
    This plugin initialize Tiles definition factory. This later can takes some
    parameters explained here after. The plugin first read parameters from
    web.xml, thenoverload them with parameters defined here. All parameters
    are optional.
    The plugin should be declared in each struts-config file.
    - definitions-config: (optional)
    Specify configuration file names. There can be several comma
    separated file names (default: ?? )
    - moduleAware: (optional - struts1.1)
    Specify if the Tiles definition factory is module aware. If true
    (default), there will be one factory for each Struts module.
    If false, there will be one common factory for all module. In this
    later case, it is still needed to declare one plugin per module.
    The factory will be initialized with parameters found in the first
    initialized plugin (generally the one associated with the default
    module).
    true : One factory per module. (default)
    false : one single shared factory for all modules
    - definitions-parser-validate: (optional)
    Specify if xml parser should validate the Tiles configuration file.
    true : validate. DTD should be specified in file header (default)
    false : no validation

    Paths found in Tiles definitions are relative to the main context.
    -->
    <plug-in className="org.apache.struts.tiles.TilesPlugin" >
        <set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml" />      
        <set-property property="moduleAware" value="true" />
    </plug-in>
    
    <!-- ========================= Validator plugin ================================= -->
    <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
        <set-property
            property="pathnames"
            value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
    </plug-in>
  
</struts-config>


validation.xml file

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE form-validation PUBLIC
          "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1.3//EN"
          "http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd">

<form-validation>

<!--
     This is a minimal Validator form file with a couple of examples.
-->

    <global>

        <!-- An example global constant
        <constant>
            <constant-name>postalCode</constant-name>
            <constant-value>^\d{5}\d*$</constant-value>
        </constant>
        end example-->

    </global>

    <formset>

        <!-- An example form -->
        <form name="logonForm">
            <field
                property="username"
                depends="required">
                    <arg key="logonForm.username"/>
            </field>
            <field
                property="password"
                depends="required,mask">
                    <arg key="logonForm.password"/>
                    <var>
                        <var-name>mask</var-name>
                        <var-value>^[0-9a-zA-Z]*$</var-value>
                    </var>
            </field>
        </form>

    </formset>

    <!-- An example formset for another locale -->
    <formset language="fr">

        <constant>
            <constant-name>postalCode</constant-name>
            <constant-value>^[0-9a-zA-Z]*$</constant-value>
        </constant>

        <!-- An example form -->
        <form name="logonForm">
            <field
                property="username"
                depends="required">
                    <arg key="logonForm.username"/>
            </field>
            <field
                property="password"
                depends="required,mask">
                    <arg key="logonForm.password"/>
                    <var>
                        <var-name>mask</var-name>
                        <var-value>^[0-9a-zA-Z]*$</var-value>
                    </var>
            </field>
        </form>

   </formset>

</form-validation>


How to Deploy and Run Struts Java ee web application project in GlassFish web server

There are different procedure to deploy and run a web application project in web server in Netbeans IDE. You can first deploy the web application in web server and then run it. You can also directly select run option to deploy and run application, in this case Netbeans IDE automatically first deploy the application is web server and then run it and open the home web page in default browser. 

Right click on your project name in project section and click on Run as showing in below.  

Find Java Struts tutorial to learn how to create Struts Java ee 7 web application project in NetBeans IDE and GlassFish Web application Server. Learn Java web application development using Struts Framework. Netbeans IDE Tutorial, Struts tutorial, Java Server, Java EE 7 tutorials, Struts framework tutorial, Strut, java framework, java struts, struts java, java web action, Struts java web application development, Java IDE, Java development softwares, how to deploy struts web application is glassfish web application server, glass fish server
Deployment process is showing below


After deployment project in GlassFish web application server, GlassFish web server run the application and will open the home page in web browser which is welcomeStruts.jsp as showing below. 

You can also open the web application in web browser by given web address which is 

http://localhost:8080/StrutsWebApplication/

Find Java Struts tutorial to learn how to create Struts Java ee 7 web application project in NetBeans IDE and GlassFish Web application Server. Learn Java web application development using Struts Framework. Netbeans IDE Tutorial, Struts tutorial, Java Server, Java EE 7 tutorials, Struts framework tutorial, Strut, java framework, java struts, struts java, java web action, Struts java web application development, Java IDE, Java development softwares


Add following line <h1>Congratulation You have been Successfully created Struts Project </h1> in welcomeStruts.jsp file as showing below and save it.

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>

<html:html lang="true">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title><bean:message key="welcome.title"/></title>
        <html:base/>
    </head>
    <body style="background-color: white">
     
        <logic:notPresent name="org.apache.struts.action.MESSAGE" scope="application">
            <div  style="color: red">
                ERROR:  Application resources not loaded -- check servlet container
                logs for error messages.
            </div>
        </logic:notPresent>
     
        <h3><bean:message key="welcome.heading"/></h3>
        <p><bean:message key="welcome.message"/></p>
        <h1>Congratulation You have been Successfully created Struts Project </h1>
    </body>
</html:html>

Just refresh the your already open page. Now you can see your modify data.


Find Java Struts tutorial to learn how to create Struts Java ee 7 web application project in NetBeans IDE and GlassFish Web application Server. Learn Java web application development using Struts Framework. Netbeans IDE Tutorial, Struts tutorial, Java Server, Java EE 7 tutorials, Struts framework tutorial, Strut, java framework, java struts, struts java, java web action, Struts java web application development, Java IDE, Java development softwares


Congratulation you have successfully learned java ee web application development using java struts framework in Netbeans IDE and how to deploy it in Glassfish web application server.



34 comments:

  1. What a great post it is really a very nice posting and we are also work in web designing field in sri lanka and welcome you to visit our website.
    infoeminent

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. How to run web application directly from desktop by creating an icon

    ReplyDelete
  4. Remarkable !!! I have gone through whole the material and have checked some given links, tips, tricks and techniques in the blog. All material is helpful and supportive. This is one of the good tutorial about creating Java Struts Web Application Project using Struts Framework There are many ways by which it can be done but mostly development companies using the way that is described in the blog.

    ReplyDelete
  5. nice blog and we are providing SRUTS ONLINE TRAINING please visit this site

    ReplyDelete
  6. I would like to thank you for the efforts you have made in writing this article.
    Signature:
    download descargar facebook and download free descargar facebook apk and baixar whatsapp , descargar whatsapp

    ReplyDelete
  7. Thanks for your details and explanations..I want more information from your side..please include some valuable ideas..I Am working in buy used laptops in chennaishould you need for any other clarification please call in this number.044-421 27512.

    ReplyDelete
  8. Great info!!A website is a representation and the best performance of a company and possible to communicate with customers directly.
    Web Design Company Bangalore|Website Development Company Bangalore

    ReplyDelete
  9. Great Information about Php Website Developer. Find best PHP Web Development Company in Napgur. Visit AceZed.Com Today!

    ReplyDelete
  10. I am really aroused by this wonderful stuff. I always prefer to read the quality content and this thing I found in you post. Thanks for sharing
    Web Development Company Bangalore

    ReplyDelete
  11. Thanks for sharing as it is an excellent post would love to read your future post: Pls visit us : Web Designing Company Chennai

    ReplyDelete
  12. This comment has been removed by the author.

    ReplyDelete
  13. Your blog has given me that thing which I never expect to get from all over the websites. Nice post guys!

    regards,

    SEO melbourne

    ReplyDelete

  14. Very informative blog. Thank you for sharing it. We have taken a website development service from Webindia Master as per our business need which turn out to be very helpful in gaining new customer,enhancing revenue and brand image of my company.

    ReplyDelete
  15. Quuick is a WebSite Designing, IO and Seo Services Company in India.Call : 097056 22666,Mail Id : info@quuick.in

    ReplyDelete
  16. I like this blog.I have got some important suggestions from it. erp software solutions chennai.

    ReplyDelete
  17. This is a great post. I like this topic.This site has lots of advantage. It helps me in many ways.Thanks for posting this again.
    Web development Company in India Company | Web development Company Bangalore
    Web Development Company in Bangalore | Responsive Web development

    ReplyDelete
  18. great nice blog with informative thanks for sharing.!!!
    website designing

    ReplyDelete

Please enter relevant question and information.