Struts2 Example, Struts2 Tutorials, Struts Program

Struts2 Example
Apache Struts 2 is simple framework for java MVC pattern. Struts2 is more easier and efficient than previous struts 1.x framework. It is more easier to use and implementation of struts2 based applications.

No more ActionForms in struts2 framework.

POJO Actions, No need to implement all action classes. Execute method can be use in any POJO class.

AJAX support, this struts 2 implement Ajax supports

Flexible, this struts2 is more flexible than previous struts framework.

Validation is easier to implement and more controllable in this framework.
Struts2 having separate validation xml for each action class. Validator xml automatically read by framework.

Struts-config.xml is become very simple and less code needs to write in struts.xml file.

In this struts2 example, we have login jsp page and submit to action class and validate form with validator. This is simple example of struts2 with login. If login form fails to with validation, it will return back to login form again and show error to user. Error string we can take from properties file where we define key of error.

Struts2 tutorials will explain how to use struts in login jsp and validate form.

8 steps to learn struts2

1. index.jsp login form jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    <title>Login page</title>
</head>

<body>
<s:form action="Login">
    <s:textfield key="username_txt"/>
    <s:password key="password_txt" />
    <s:submit/>
</s:form>
</body>
</html>

2. logged.jsp successfully after login form

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    <title>Successfully logged In</title>
</head>

<body>
<h1>Successfully logged in struts2</h1>
</body>
</html>

3. web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Struts Blank</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.jsp</welcome-file>
    </welcome-file-list>

</web-app>

4. mypackage.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="mypackage" namespace="/" extends="struts-default">

        <action name="Login" class="com.struts2.LoginAction">
            <result name="input">/index.jsp</result>
            <result type="redirectAction">/logged.jsp</result>
        </action>

        <!-- Add actions here -->
    </package>
</struts>

5. 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.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="false" />

    <include file="mypackage.xml"/>

    <!-- Add packages here -->

</struts>

6. LoginAction.java

package com.struts2;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport{

    private static final long serialVersionUID = 1L;

    public String execute() throws Exception {

        boolean errorflag = false;

        if (isInvalid(getUsername_txt()))
        {
            errorflag = true;
        }

        if(errorflag==false)
        {
            return INPUT;
        }

        return SUCCESS;
    }

    private boolean isInvalid(String value) {
            return (value != null || value.length() > 0);
    }

    private String username_txt;
    private String password_txt;

    public String getPassword_txt() {
        return password_txt;
    }
    public void setPassword_txt(String password_txt) {
        this.password_txt = password_txt;
    }
    public String getUsername_txt() {
        return username_txt;
    }
    public void setUsername_txt(String username_txt) {
        this.username_txt = username_txt;
    }
}

7. LoginAction-validation.xml

<!DOCTYPE validators PUBLIC
        "-//OpenSymphony Group//XWork Validator 1.0.2//EN"
        "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">

<validators>
    <field name="username_txt">
        <field-validator type="requiredstring">
            <message key="requiredstring"/>
        </field-validator>
    </field>
    <field name="password_txt">
        <field-validator type="requiredstring">
            <message key="requiredstring"/>
        </field-validator>
    </field>
</validators>

8. package.properties

requiredstring = ${getText(fieldName)} is required.
password_txt = Password
username_txt = User Name

Validation failed

File structure of Struts2

Keep file in this structure

download source code of struts2

Tags:

Bookmark  

 

Leave a Reply

Security Code:

 

  Random Post