Tutorial_Create Struts 2 Application in Eclipse(6)
发布时间:2021-06-08
发布时间:2021-06-08
mapped with LoginAction depending on the outcome of execute() method. If execute() method returnssuccess, user will be redirected to Welcome.jsp else to Login.jsp.
Also note that a constant is specified with name struts.custom.i18n.resources. This constant specify theresource bundle file that we created in above steps. We just have to specify name of resource bundle filewithout extension (ApplicationResources without .properties).
Our LoginAction contains the method execute() which is the default method getting called by Sturts2. If thename of method is different, e.g. authenticate(); then we should specify the method name in<action> tag.<action name="login" method="authenticate" class="net.viralpatel.struts2.LoginAction">
Almost Done
We are almost done with the application. You may want to run the application now and see the resultyourself. I assume you have already configured Tomcat in eclipse. All you need to do:
Open Server view from Windows -> Show View -> Server. Right click in this view and select New -> Serverand add your server details.
To run the project, right click on Project name from Project Explorer and select Run as -> Run on Server(Shortcut: Alt+Shift+X, R)
But there is one small problem. Our application runs perfectly fine at this point. But when user enters wrongcredential, she is redirected to Login page. But no error message is displayed. User does not know whatjust happened. A good application always show proper error messages to user. So we must display anerror message Invalid Username/Password. Please try again when user authentication is failed.
Final Touch
To add this functionality first we will add the error message in our ResourceBundle file.
Open ApplicationResources.properties and add an entry for error.login in it. The final
ApplicationResources.properties will look like:
ername= Usernamelabel.password= Passwordlabel.login= Loginerror.login= Invalid Username/Password. Please try again.
Also we need to add logic in LoginAction to add error message if user is not authenticated. But there is oneproblem. Our error message is specified in ApplicationResources.properties file. We must specify keyerror.login in LoginAction and the message should be displayed on JSP page.
For this we must implement com.opensymphony.xwork2.TextProvider interface which provides
methodgetText(). This method returns String value from resource bundle file. We just have to pass thekey value as argument to getText() method. The TextProvider interface defines several method that wemust implement in order to get hold on getText() method. But we don’t want to spoil our code by adding allthose methods which we do not intend to use. There is a good way of dealing with this problem.
Struts2 comes with a very useful class com.opensymphony.xwork2.ActionSupport. We just have to extendour LoginAction class with this class and directly use methods such as getText(), addActionErrors() etc.
下一篇:胰头癌并发症