4月 7th, 2008at 14:04

Tags: ,

Struts2.0.9でFormの入力情報をActionで使う

画面のFormに入力された値をAction内で取得してみる。

JSPにFormを記述

Formタグを使ってFormを記述。

 <s:form action="userreg">   <s:textfield name="userName" />   <s:password name="password" />   <s:submit value="登録" /> </s:form>

Action内でFormの値を取得

Actionではアクセッサを用意するだけでDispatcherがバインドしてくれる。Formクラスがなくなって、ActionがFormの動作もしてくれるらしい。

private String userName;private String password;

@Overridepublic String execute() throws Exception {

    AppLogger.debug("UserRegAction.execute");

    //パラメタからVOを作成する    UserVO userVO = createUserVO();

    AppLogger.debug(userVO.getUserName());    AppLogger.debug(userVO.getPassword());

    //ユーザ登録処理

    return "success";}

private UserVO createUserVO(){    UserVO vo = new UserVO();    vo.setUserName(this.userName);    vo.setPassword(this.password);    return vo;}

その他

アクセッサがなくてもエラーにはならない。