December 31, 2013

Struts application in java


Java struts login page application source code


This application is for beginner. copy paste the code to respective folder.  The application shows you how login  with struts work.

Login page

Login.jsp

Java struts login page application source code


<%@ 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">
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>

<title> Java struts web application source code </title>
</head>
<body>

<h2>Sample web application</h2>

<s:actionerror />

<s:form action="login.action" method="post">
    <s:textfield name="username" key="label.username" />
    <s:password name="password" key="label.password" />
    <s:submit method="execute" key="label.login" align="center" />
</s:form>

</body>
</html>

This jsp teach you how struts tag works

<%@ taglib prefix="s" uri="/struts-tags"%> adding a struts tag to jsp page

<s:form action="login.action" method="post">  as like form in html. once you submit the form the corresponding login action in struts.xml is called.

key="label.login" will be defined in application.properties

Welcome page.jsp   ( Second page)


<%@ 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>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title> struts JSP jquery application</title>
</head>
<body>
<h2> Welcome to java lazy blog, Java web application </h2>
</body>
</html>


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>Struts2 application</display-name>

    <filter>
        <filter-name>StrutsNew</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>

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

    <welcome-file-list>
        <welcome-file>Login.jsp</welcome-file>
    </welcome-file-list>

</web-app>

---

    <filter>
        <filter-name>StrutsNew</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>

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

Through this filter class in struts.xml we are re directing each every url [ <url-pattern>/*</url-pattern>]  through filterdispatcher <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>



Jar file needed. you have to put this in lib folder






LoginAction.java inside src folder


package com.blazy.strutsSpringapplication;
/*
* Action class in struts
*/
public class LoginAction {
   
    private String username = null;
    private String password = null;
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
   
    public String execute()
    {
        if(username.equals("u") & (password.equals("p")))
           // code for validation
        {
            System.out.println(" Struts frame work example download "+username + password);
        return "success";
        }
        else
            return "error";
       
    }

}

--

public class LoginAction  implements ......

ApplicationResources.properties file inside resources folder


label.username = username
label.password = password
label.login = Login
error.login = Invalid user try again later



Struts.xml file  inside resource folder


<?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="login" class="com.org.strutsNew.LoginAction" converter="" method="save">
            <result name="success">Welcome.jsp</result>
            <result name="error">Login.jsp</result>
        </action>
     </package>
</struts>

we have created the login class above
<action name="login" class="com.org.strutsNew.LoginAction" converter="" method="save">

This is only a small program. please try this.

Wish you a Happy New year

+Deepu A.B.


Similar posts

  1. Java spring model view and controller - hello world example



https://javabelazy.blogspot.in

No comments:

Post a Comment

Your feedback may help others !!!

Facebook comments