手机版

Tutorial_Create Struts 2 Application in Eclipse(4)

发布时间:2021-06-08   来源:未知    
字号:

<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.

Tutorial_Create Struts 2 Application in Eclipse(4).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
×
二维码
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)