<filter><filter-name>struts2</filter-name><filter-class> org.apache.struts2.dispatcher.FilterDispatcher</filter-class></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping><welcome-file-list><welcome-file>Login.jsp</welcome-file></welcome-file-list></web-app>
The above code in web.xml will map Struts2 filter with url/*. The default url mapping for struts2 application will be/*.action. Also note that we have define Login.jsp as welcome file. Note: The FilterDispatcher filter is deprecated since Struts version 2.1.3. If you are using latest version of Struts2 (> 2.1.3) use StrutsPrepareAndExecuteFilter class instead.<filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dis
patcher.ng.filter.StrutsPrepareAndExecuteFilter</</filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping>
The Action ClassWe will need an Action class that will authenticate our user and holds the value for username and password. For this we will create a package net.viralpatel.struts2 in the source folder. This package will contain the action file.
Create a class called LoginAction in net.viralpatel.struts2 package with following content. package net.viralpatel.struts2; public class LoginAction{ private String username; private String password; public String execute(){ if (ername.equals("admin")&& this.password.equals("admin123")){ return"success";} else{ return"error";
} } public String getUsername() { return username; } public void setUsername(String username) { ername = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; }}
Note that, above action class contains two fields, username and password which will hold the values fromform and also contains an execute() method that will authenticate the user. In this simple example, we arechecking if username is admin and password is admin123.
Also note that unlike Action class in Struts1, Struts2 action class is a simple POJO class with requiredattributes and method.
The execute() method returns a String value which will determine the result page. Also, in Struts2 thename of the method is not fixed. In this example we have define method execute(). You may want to definea method authenticate() instead.
The ResourceBundle
ResourceBundle is very useful Java entity that helps in putting the static content away from the source file.Most of the application define a resource bundle file such as ApplicationResources.properties file whichcontains static messages such as Username or Password and include this with the application.
ResourceBundle comes handy when we want to add Internationalization (I18N) support to an application.We will define an ApplicationResources.properties file for our application. This property file should bepresent in WEB-INF/classes folders when the source is compiled. Thus we will create a source foldercalled resources and put the ApplicationResources.properties file in it.
To create a source folder, right click on your project in Project Explorer and select New -> Source Folder.