论坛首页 Java版 Struts

WebWork2&Struts1.1(1):ForumBean

浏览 3360 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
最后更新时间:2005-06-16
以一个简单的创建用户的页面为例,
WebWork2版:
页面:
[code:1]
<html>
    <head><title>create user</title></head>
<body>
<form method=post action="/admin/createUser.action">
        <table>
        <tr>
        <td>用户名</td>
        <td><input type="text" name="user.name"></td>
        </tr>
<tr>
        <td>电了邮件</td>
        <td><input type="text" name="user.email"></td>
        </tr>
        <tr>
        <td>密码</td>
        <td><input type="password" name="user.password"></td>
        </tr>
        </table>
<input type="submit" value="提交">
<input type="reset" value="清空">
        </form>
</body>
</html>
[/code:1]
Action:
[code:1]
public class CreateUserAction implements Action {
    private User user = new User();
    private UserService userService;

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public UserService getUserService() {
        if (userService == null) {
            userService = (UserService) Application.getInstance().getContainer().getComponent("userService");
        }
        return userService;
    }
   
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public String execute() throws Exception {
        try {
            getUserService().saveUser(user);
        } catch (UserAlreadyExistsException uaee) {
            addActionError("register.userAlreadyExists");
            return ERROR;
        } catch (EmailAlreadyExistsException eaee) {
            addActionError("register.emailAlreadyExists");
            return ERROR;
        }
        return SUCCESS;
    }
}[/code:1]
   
最后更新时间:2005-06-16
Struts1.1版:
页面:
[code:1]
<html>
    <head><title>create user</title></head>
<body>
<form method=post action="/admin/createUser.action">
        <table>
        <tr>
        <td>用户名</td>
        <td><input type="text" name="name"></td>
        </tr>
<tr>
        <td>电了邮件</td>
        <td><input type="text" name="email"></td>
        </tr>
        <tr>
        <td>密码</td>
        <td><input type="password" name="password"></td>
        </tr>
        </table>
<input type="submit" value="提交">
<input type="reset" value="清空">
        </form>
</body>
</html>
[/code:1]
ForumBean:
[code:1]
public ForumBean extends ActionForm {
    private String name;
    private String email;
    private String password;
    ...
    (getter,setter)
}
[/code:1]
Action:
[code:1]
public class CreateUserAction extends Action {
    private UserService userService;

    public UserService getUserService() {
        if (userService == null) {
            userService = (UserService) Application.getInstance().getContainer().getComponent("userService");
        }
        return userService;
    }
   
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
User user = new User();
BeanUtils.copyProperties(user, form);
        try {
            getUserService().saveUser(user);
        } catch (UserAlreadyExistsException uaee) {
            return mapping.findForward("error");
        } catch (EmailAlreadyExistsException eaee) {
            return mapping.findForward("error");
        }
        return return mapping.findForward("success");;
    }
}
[/code:1]
唉!甩也甩不掉的ForumBean,要不是借助于apache的BeanUtils,会更烦。
   
0 请登录后投票
最后更新时间:2005-06-16
struts可以借助RequestUtils甩掉FormBean
   
0 请登录后投票
最后更新时间:2005-06-16
怎么甩?欢迎给代码.
   
0 请登录后投票
最后更新时间:2005-06-16
z_jordon 写道
怎么甩?欢迎给代码.

为什么摔,你不用不得了!struts遗留系统太多,用户群也多。
慢慢得,看吧。
   
0 请登录后投票
最后更新时间:2005-06-16
能甩为什么不甩,能少写点代码不好吗?
   
0 请登录后投票
最后更新时间:2005-06-16
RequestUtils?楼上的可否把代码贴出来看看...
   
0 请登录后投票
最后更新时间:2005-06-17
无聊神灯 写道
struts可以借助RequestUtils甩掉FormBean


相反,我想,也可以甩掉Action,但不想甩掉FormBean。
我也想甩掉Webwork的Action,但正在想。
   
0 请登录后投票
最后更新时间:2005-06-17
Action都不要了?那你控制页面跳转的些逻揖放在那一层?
   
0 请登录后投票
最后更新时间:2005-06-17
按你这么说把FormBean甩掉岂不是不要表单提取了?
   
0 请登录后投票
论坛首页 Java版 Struts

跳转论坛:
JavaEye推荐