按照同样的方法创建main.jsp. 由于我们只是简单的在主界面中显示成功登陆的信息,因此在创建的时候,我们选择的是第一个缺省的模板.
JSP文件创建后,我们可以根据自己的需要来修改其中的内容. 对于index.jsp,我们修改Form的action属性和提交参数的名字, 并增加了标签,可参考下面的代码:
<html:form action="/login.do" method="post" focus="login">
<center><html:errors/>center>
<table border="0">
<tr>
<td>Name:td>
<td><html:text property="name" />td>
tr>
<tr>
<td>Password:td>
<td><html:password property="password" />td>
tr>
<tr>
<td colspan="2" align="center"><html:submit />td>
tr>
table>
html:form>
对于main.jsp,我们修改中的输出信息,提示用户登陆成功,代码如下:
<body>
You have logged in!<br>
body>
保存两个JSP文件和struts-config.xml文件.
n 创建ActionForm
在我们的登陆程序中需要用户提交用户名和密码,因此我们需要创建一个ActionForm Bean来保存提交的HTML表单数据.
将源代码窗口中的标签切换到struts-config.xml,右上角的Outline窗口内容如下:

选中其中的form-beans,右键单击,在弹出的菜单中选择New Form. 在Form Bean创建向导对话框中,输入Use case的名字login,然后在下方的Form Properties中增加name和password两个属性.


n 创建Action
在完成上面的创建后,我们接下来创建Action类. 在源代码窗口中切换到struts-config.xml文件,点击左侧工具栏中的
. 在弹出的创建向导中输入Use case名字为Login.
在对话框的下部Optional Details中,点击Name行末的Browse按钮,选中我们刚才创建的loginForm.

点击Input Source行末的Browse按钮,在弹出的对话框中选中index.jsp.

将Optional Details标签切换到Forwards,点击Add按钮增加一个局部的Forward.

这里的success是我们给Forward的名字,你也可以使用其它你喜欢的名字. 增加后如下:

完成新建Action的设置后如下图:

点击Finish按钮后,MyEclipse会为我们创建一个名叫LoginAction.java的文件,我们可以根据自己的需要来实现具体的验证逻辑. 这里,我们仅仅通过登陆的名字是否为hello来判断. 如果不是提示登陆失败. 代码参考如下:
ActionMessages errors=new ActionMessages();
LoginForm loginForm = (LoginForm) form;
String username=loginForm.getName();
if(username.equals("hello")) {
return mapping.findForward("success");
} else {
errors.add("username",new ActionMessage("error.msg"));
saveErrors(request,errors);
return (new ActionForward(mapping.getInput()));
}
从上面的代码中我们可以看到,当用户登陆失败后会使用一个键值为error.msg的ActionMessage. 因此,我们在Package Explore中找到ApplicationResources.properties文件并双击打开,输入以下内容并保存.
error.msg=Login Failed
在完成了上述所有的工作后,我们可以看到图示化的struts-config.xml如下:

n 部署和测试应用程序
这里StrutsDemo的部署同其他的Web应用程序一样,我们就不在赘述了,具体的方法可以参阅MyEclipse系列文章的第二篇Web项目的开发.
部署成功后,在IE的地址栏中输入http://localhost:8080/hello/index.jsp并回车.

我们首先验证登陆失败的场景. 当我们输入的用户名不是hello时,应返回到登陆页面并提示登陆失败. 在Name中输入abcd,点击Submit按钮.

我们可以看到又回到了登陆页面,并且出现了Login Failed的提示信息. 修改用户名为hello,然后点击Submit按钮.

完.