바로 전에 만든 첫번째 DynamicWeb Project를 기반으로

폼 전송이 되는 예제를 만든다.

간단히 form을 하나 만들고 전송버튼을 눌렀을 때
폼의 내용을 전송하는 예제이다.

1. 해당 파일로 접속하도록 index.jsp 수정
WebContent/index.jsp
<jsp:forward page="contacts.html"></jsp:forward>

2. 연락처 추가 page
WebContent/WEB-INF/jsp/contact.jsp

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
    <title>Spring 3 MVC Address book </title>
</head>
<body>
<h2>주소록</h2>

<form:form method="post" action="addContact.nobang">
    <table>
        <tr>
            <td><form:label path="firstname">Firstname </form:label>
            <td><form:input path="firstname"/></td>
        </tr>
        <tr>
            <td><form:label path="lastname">Lastname </form:label>
            <td><form:input path="lastname"/></td>
        </tr>
        <tr>
            <td><form:label path="email">Email</form:label>
            <td><form:input path="email"/></td>
        </tr>
        <tr>
            <td><form:label path="mobile">Mobile</form:label>
            <td><form:input path="mobile"/></td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="submit" value="Add Contact"/>
            </td>
        </tr>
    </table>

</form:form>

tag lib로 각 항목을 전송
나중에 해당 page는 addContact.nobang 으로 전송됨
(pattern으로 *.nobang을 인식함)


3. java source
form에서 넘어오는 data를 담을 객체 정의

package com.nobang.spring3.form;

public class Contact {
    private String firstname;
    private String lastname;
    private String email;
    private String mobile;
    public String getFirstname() {
        return firstname;
    }
    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }
    public String getLastname() {
        return lastname;
    }
    public void setLastname(String lastname) {
        this.lastname = lastname;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getMobile() {
        return mobile;
    }
    public void setMobile(String mobile) {
        this.mobile = mobile;
    } 
}


Controller 정의
package com.nobang.spring3.controller;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.nobang.spring3.form.Contact;


@Controller
@SessionAttributes
public class ContactController {

    @RequestMapping(value="/addContact", method = RequestMethod.POST)
    public String addContact(@ModelAttribute("contact") Contact contact, BindingResult result){
        System.out.println(contact.getFirstname() + contact.getLastname() );
        System.out.println(contact.getEmail() + " : " + contact.getMobile());
        return "redirect:contacts.nobang";
    }
   
    @RequestMapping("/contacts")
    public ModelAndView showContacts(){
        return new ModelAndView("contact", "command", new Contact());
    }
}


프로젝트를 실행한 뒤
form에 내용을 채우고
전송버튼을 누르면
Console에 로그가 찍힘.
728x90
BLOG main image
"그게 뭐 어쨌다는 거냐?" 늘 누가 나에게 나에대한 말을할 때면 이말을 기억해라. by nobang

카테고리

nobang이야기 (1933)
Life With Gopro (7)
Life With Mini (79)
Diary (971)
너 그거 아니(do you know) (162)
난 그래 (159)
Study (290)
속지말자 (10)
Project (34)
Poem (15)
Song (0)
Photo (113)
낙서장 (45)
일정 (0)
C.A.P.i (2)
PodCast (0)
nobang (27)
고한친구들 (4)
recieve (0)
History (0)
android_app (2)

최근에 올라온 글

최근에 달린 댓글

최근에 받은 트랙백

Total :
Today : Yesterday :