Struts is a java framework for web implements the MVC design pattern.
Struts facilitates the work of page redirection, form submitting, data validation and much more.

a common scenario is a login page
login.jsp that contains two inputs user name and password
a simple java class/bean that maps this information ( has two properties username and password)
an action servlet that receives the login page data and put them into the bean
then make a specific action (Logining) after that redirecting to another page (success page or return to the login page if error happened)

In struts this operation is automated.
you don’t need to have the request processor part, struts will do it for you
you only provide struts with the view and the business and it control the whole life cycle

MVC is Model View Controller
The Model part role is to deal with data like getting it from database
The View part role is to deal with the view (web page)
The controller part role is to receive the request from the user, communicate with the Model part to get the data, Fetching it to the view part and deliver the view part back to the user

its aim is to separate these three parts to be easy after that to replace one part without affecting the other two parts

to begin using struts

you may start with ready made blank struts project called struts-blank after you download sample struts apps from download link
if you prefer to build your own struts project you should include struts libraries into your project

you can download the latest from this URL
http://struts.apache.org/download.cgi

project startup
in a new java project put struts libraries inside the WEB-INF/lib

Create a new XML file called struts-config inside WEB-INF
you can start with this structure (taken from struts-blank project)








    
    

    
    



    
        
    



    
        
        
        
    



    
            
            
        

    
    

    


we will start with a simple login page



	Login


	
User name


Password


Struts needs to be aware of the fields in your form to collect data from it instead of you
you can do this by modifying your form elements to use Struts HTML and Bean Tag libraries

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

after modification your form would be like this


	
	





we would change text input tags to be html:text and password input tag to be html:password
also we would make use of the tag bean:message

Resource file
resource file is a simple .properties file located in your source project
to enable it, you should
1. Refer to it in struts-config

    

2.write your text values inside it in the form key=value

label.user.name=User name
label.user.pass=Password

3. in the JSP pages write the key to get the value in runtime

	

It’s a good concept to separate these values from your view to be able to modify it easily, also it has many other advantages we would see shortly.

Our form is now struts friendly, Struts would take care of the values written in every html:* input field and pass it to a java bean/class that has the same attributes

We now would think about our bean, It Should contains two properties name and password the same as the value written in the property field in html:text, html:password tags

public class LoginForm
{
	private String name;
	private String pass;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPass() {
		return pass;
	}
	public void setPass(String pass) {
		this.pass = pass;
	}
}

to convert this simple bean into struts bean (called Action Form)
we should extend org.apache.struts.action.ActionForm

package com.example.forms
import org.apache.struts.action.ActionForm;

public class LoginForm extends ActionForm
{
	private String name;
	private String pass;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPass() {
		return pass;
	}
	public void setPass(String pass) {
		this.pass = pass;
	}
}

Now we should think about the action that would be done when submitting the form
It’s a simple java class that would extend the class org.apache.struts.action.Action and would implement the method execute

package com.example.actions;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import com.example.forms.LoginForm;

public class LoginAction extends Action {
	
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		LoginForm loginForm = (LoginForm) form;
		
		return null;
	}
}

the method execute receives a reference of
ActionForm form : it’s the form data sent from the form of login page
HttpServletRequest request, HttpServletResponse response : request and response of the current call
ActionMapping mapping: we would talk about that later.