특별한딸기이야기

요청 파라미터 처리(ActionForm이용) 본문

딸기 공부방/struts and spring

요청 파라미터 처리(ActionForm이용)

특별한녀석 2008. 11. 12. 14:10

struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
 "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
 "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
 <form-beans>
  <form-bean name="memberForm" type="com.tistory.special0strawberry.MemberForm"/>
 </form-beans>
 <action-mappings>
  <action path="/insertForm" forward="/InsertForm.jsp"/>
  <action path="/insert" type="com.tistory.special0strawberry.MemberInserAction" name="memberForm" scope="request" validate="false"/>
  
 </action-mappings>
</struts-config>

MemberForm class

package com.tistory.special0strawberry;

import org.apache.struts.action.ActionForm;

public class MemberForm extends ActionForm
{
 private String id;
 private String password;
 private String name;
 private String phone;
 
 public String getId() {
  return id;
 }
 public void setId(String id) {
  this.id = id;
 }
 public String getPassword() {
  return password;
 }
 public void setPassword(String password) {
  this.password = password;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public String getPhone() {
  return phone;
 }
 public void setPhone(String phone) {
  this.phone = phone;
 }
}
// getter/setter만 들어있는 class입니다만 ActionForm을 상속받았다는 것이 특징입니다.

MemberInserAction class
// MemberInsertAction으로 만들었어야 하는 건데... OTL

package com.tistory.special0strawberry;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.RequestUtils;

import com.tistory.special0strawberry.Member;

public class MemberInserAction extends Action
{
 @Override
 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response)
   throws Exception {
  // TODO Auto-generated method stub
  //return super.execute(mapping, form, request, response);
  
  /*
  //RequestUtil use
  Member member = new Member();
  
  RequestUtils.populate(member, request);
  */
  
  //ActionForm use
  MemberForm member = (MemberForm)form;
  
  System.out.println("member.id : " + member.getId());
  System.out.println("member.password : " + member.getPassword());
  System.out.println("member.name : " + member.getName());
  System.out.println("member.phone : " + member.getPhone());
  
  return null;
 }
}

// RequestUtil 사용법은 주석 처리

InsertForm.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
  <title>
   Insert Form
  </title>
 </head>
 <body>
  <form action="/Tytolee/insert.do" name="insert">
   Id :
   <input type="text" name="id"/>
   <br/>
   Password :
   <input type="password" name="password"/>
   <br/>
   Name :
   <input type="text" name="name"/>
   <br/>
   Phone :
   <input type="text" name="phone"/>
   <br/>
   <input type="submit" value="확인"/>
  </form>
 </body>
</html>

// control 부분이 바뀐 것이기 때문에 view가 바뀌지는 않았다.