Only Institute
to provide recording daily after class to students. you can practice whole day under faculty guidance. where test and mockup tests are taken regularly. provides all mobile nos of old students on website.

Struts 2.0 Basic



  • It is a framework used to develop MVC –based web application.
  • It provides Model, View & Controller.
  • It has rich UI (tag library).
  • In struts 2, action class is POJO (Plain Old Java Object) i.e. a simple java class. Here, you are not forced to implement any interface or inherit any class.
  • Struts 2 provide various types of tags such as UI tags, Data tags, and control tags.
Before going in deep let’s develop simple web application using Struts.


Required Files –

1.login.jsp
2.success.jsp
3.web.xml
4.struts.xml
5.LoginAction.java
6.FilterDispatcher.java(present in struts 2 jars)

Required Jars –

All struts jar...Below are the basic jars of struts not all...
struts2.0-2

Folder Structure (for deployment)
struts2.0-3



  • User sends a request to the server for requesting for some resource (i.e pages).

  • The FilterDispatcher looks at the request and then determines the appropriate Action.

  • Configured interceptors functionalities applies such as validation, file upload etc.

  • Selected action is executed to perform the requested operation.

  • Again, configured interceptors are applied to do any post-processing if required.

  • Finally the result is prepared by the view and returns the result to the user.

XML Files Overview

The web.xml file:
The web.xml configuration file is a J2EE configuration file that determines how elements of the HTTP request are processed by the servlet container. It is not strictly a Struts2 configuration file, but it is a file that needs to be configured for Struts2 to work.

As discussed earlier, this file provides an entry point for any web application. The entry point of Struts2 application will be a filter defined in deployment descriptor (web.xml). Hence we will define an entry of FilterDispatcher class in web.xml. The web.xml file needs to be created under the folder Web Content/WEB-INF.

The struts.xml file:
The first thing to note is the DOCTYPE. All struts configuration file need to have the correct doctype as shown in our little example. is the root tag element, under which we declare different packages using tags. Here allows separation and modularization of the configuration. This is very useful when you have a large project and project is divided into different modules.

Say, if your project has three domains - business_applicaiton, customer_application and staff_application, you could create three packages and store associated actions in the appropriate package. The package tag has the following attributes:





  • The constant tag along with name and value attributes will be used to override any of the following properties defined in default.properties, like we just set struts.devMode property. Settingstruts.devMode property allows us to see more debug messages in the log file.

  • We define action tags corresponds to every URL we want to access and we define a class with execute() method which will be accessed whenever we will access corresponding URL.

  • Results determine what gets returned to the browser after an action is executed. The string returned from the action should be the name of a result. Results are configured per-action as above, or as a "global" result, available to every action in a package. Results have optional name and type attributes. The default name value is "success".

  • Struts.xml file can grow big over time and so breaking it by packages is one way of modularizing it, but struts offers another way to modularize the struts.xml file. You could split the file into multiple xml files and import them in the following fashion.

Points about FilterDispatcher :-
1. It invokes at every request.

2. it receives incoming req. and maps appropriate action by looking into struts.xml
{Then
Action class is instantiate, i.e. constructor is called, then getters, setters and method.
}

Programs

Web.xml –

< ? xml version="1.0" encoding="UTF-8"? >
< web-app 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">

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

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

< / web-app >

Struts.xml-

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


< struts >
	< package name="mypkg" extends="struts-default" >
		< action name="log" class="com.arjun.action.LoginAction" method="m1" >
		< result name="success">success.jsp < / result >
		< / action >
	< / package >
< / struts >

Login.jsp


< html >
  < head > 
       < title > LoginPage < / title >  
  < / head > 
  < body >
   < form action="log.action" > < center >
   username < input type="text" name="un" > < br> < br >
   password < input type="text" name="pw" > < br > < br >
   < input type="submit" value="Login">
   < / center > 
   < / form >
  < / body >
< / html >


LoginAction.java –



package com.arjun.action;

public class LoginAction {
    private String un;
    private String pw;
    
	public String getPw() {
		System.out.println("getter of pw");
		return pw;
	}
	public void setPw(String pw) {
		System.out.println("setter of pw");
		this.pw = pw;
	}
	public String getUn() {
		System.out.println("getter of un");
		return un;
	}
	public void setUn(String un) {
		System.out.println("setter of un");
		this.un = un;
	}
	public LoginAction() {
		System.out.println("in a const of loginaction....");
	}

	public void execute(){
		System.out.println("in a execute()..");
		
	}
	
public String m1(){
		System.out.println("in a m1()....");
		return "success";
	}
}

Success.jsp –

< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
< html >
  < head >
    < title > success page < / title >
  < / head >
  < body >
    This is my JSP page. < br >
    < % String uname = (String)request.getAttribute("un"); % >
    < %=uname% >
  < / body >
< / html >


Login.jsp using Struts tag library –

< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
< %@taglib prefix="s" uri="/struts-tags" % >
< html >
  < head >
       < title > Login Page < / title >
  < / head >
  
  < body >
 < s:form action="log" > --bydefault post method 
 < s:textfield label="Username" name="un" / >
 < s:password label="Password" name="pw" / >
 < s:submit value="Login" / >
 < / s:form >
< / body >
< / html >

Success.jsp using Struts tag library –

< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
< %@ taglib prefix="s" uri="/struts-tags" % >
< html >
  < head >
    < title >success page< / title >
  < / head >
  < body >
  Welcome , < s:property value="un" / >
  your password is ,'< s:property value="pw" / >'
  < / body >
< / html >