Tutorial_Create Struts 2 Application in Eclipse(4)

时间:2025-05-13

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

…… 此处隐藏:1093字,全部文档内容请下载后查看。喜欢就下载吧 ……
Tutorial_Create Struts 2 Application in Eclipse(4).doc 将本文的Word文档下载到电脑

精彩图片

热门精选

大家正在看

× 游客快捷下载通道(下载后可以自由复制和排版)

限时特价:7 元/份 原价:20元

支付方式:

开通VIP包月会员 特价:29元/月

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219