1. 객체로 데이터 받기
뷰 :
<form action="insert.action">
<div align="center">
<table border="1">
<tr>
<td>id</td>
<td><input type="text" name="id"></td>
</tr>
<tr>
<td>name</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>mail</td>
<td><input type="text" name="mail"></td>
</tr>
<tr>
<td>text</td>
<td><textarea rows="10" cols="50" name="text">
</textarea></td>
</tr>
<tr align="center">
<td colspan="2"><input type="submit" value="Submit"> <input
type="reset" value="Reset"></td>
</tr>
</table>
</div>
액션 :
public class InsertAction implements Action , ModelDriven<GuestBook>,Preparable
{
private GuestBook gb = null;
//실행 순서
//prepare -> getModel -> execute
@Override
public void prepare() throws Exception
{ //객체 준비
// TODO Auto-generated method stub
this.gb = new GuestBook();
}
@Override
public GuestBook getModel()
{ //view에서 입력 받은 값을 저장할 객체를 반환
// TODO Auto-generated method stub
return gb;
}
@Override
public String execute() throws Exception
{ // 실행
// TODO Auto-generated method stub
GuestBookDAO dao = GuestBookDAO.getInstance();
dao.insert(gb);
return SUCCESS;
}
}
2. set으로 데이터 받기
뷰 :
<a href="read.action?num=${gb.num}">
액션:
public class ReadAction implements Action
{
private GuestBook gb = null;
private int num = 0;
public void setNum(int num)
{
this.num = num;
System.out.println(num);
}
@Override
public String execute() throws Exception
{
// TODO Auto-generated method stub
GuestBookDAO dao = GuestBookDAO.getInstance();
gb = dao.read(this.num);
System.out.print(gb.getId());
return SUCCESS;
}
public GuestBook getGb()
{
return gb;
}
}
'나는 엔지니어 > 스트럿츠' 카테고리의 다른 글
struts.xml --- 액션 나누기 (0) | 2012.07.13 |
---|---|
스트럿츠 연동시 한글이 깨질때 (0) | 2012.07.12 |
MVC2 모델 사용하기 프레임워크 이용하기 (0) | 2012.07.12 |