上一篇 | 下一篇

实战JBuilder8 + Struts,一个简单的例

发布: 2008-6-26 08:52 | 作者: admin | 来源: | 查看: 3次

实战JBuilder8 + Struts,一个简单的例子 liuxiaowei(原作)

关键字 JBuilder8 Struts

实战JBuilder8 + Struts,一个简单的例子

刘晓巍:liuxiaowei2000@sina.com

一. 内容介绍

本文说明了使用JBuilder 8 建立一个简单的Struts应用步骤,主要以步骤为主,理论说明较少。希望能够对大家有所帮助,也希望大家多多批评!

运行环境:

Windows 2000 Server(SP3)

j2sdk1.4.1_01

jakarta-struts-1.0.2(JBuilder8自带)

二. 实战

1 建立Web Application

1.1 选择File-》New Project建立一个名为StrutsDemo新的项目,如下图所示:

图1

1.2 单击Next,去掉Required Libraries中的项,其它不变,单击Finish即可。

图2

1.3 选择Project-》Project Properties,按下图设置Server页,单击OK即可:

图3

1.4 选择File-》New-》Web页-》Web Application,单击OK。

图4

1.5 按下图填写后,单击OK即可。

图5

2 建立ActionForm

2.1 选择File-》New-》Web页-》ActionForm,单击OK。

图6

2.2 按下图填写后,单击OK即可。

图7

2.3 单击Add按钮,加入name和password字段的定义后,单击Next即可。

图8

2.4 按下图填写后,单击Finish即可。

图9

2.5 编写ActionForm代码。

一般在ActionForm里进行简单的有效性检查,按如下填写 LoginActionForm的validate()函数:

public ActionErrors validate(ActionMapping actionMapping, HttpServletRequest httpServletRequest) {

ActionErrors errors = new ActionErrors();

if ((name == null) || (name.length() < 1))

errors.add("name", new ActionError("error.username.required"));

if ((password == null) || (password.length() < 1))

errors.add("password", new ActionError("error.password.required"));

return errors;

}

3 建立Action

3.1 选择File-》New-》Web页-》Action,单击OK。

图10

3.2 按下图填写后,单击Next即可。

图11

3.3 下图填写后,单击Finish即可。

图12

3.4 编写Action代码。

一般情况下在Action中编写逻辑,存取数据库等操作。按如下填写loginAction的perform()函数:

public ActionForward perform(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {

LoginActionForm form = (LoginActionForm) actionForm;

if (form.getName().equals("liuxiaowei")&&(form.getPassword().equals("liuxiaowei")))

{

// Save our logged-in user in the session

HttpSession session = httpServletRequest.getSession();

session.setAttribute("UserName", form.getName());

return (actionMapping.findForward("success"))

字号: | 推荐给好友

31/3123>

评分:0

我来说两句