by-nc-sa     개발자, DBA가 함께 만들어가는 구루비 지식창고!

ModelAttribute 실습




ModelAttribute 실습

  • @ModelAttribute를 사용하여 모델객체를 파라미터로 받는 예제이다.

Emp 모델 객체 생성

  • com.oracleclub.study.mvc.model 패키지를 생성한다.
  • 사원 정보를 가지는 Emp.java 모델 객체를 생성한다
com.oracleclub.study.mvc.model.Emp.java
package com.oracleclub.study.mvc.model;

import java.util.Date;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

/**
 * 
 * emp 테이블 모델객체
 * 
 * @author : oramaster
 * 
 */
public class Emp {

    private int empno;
    private int mgr;
    private int sal;
    private int deptno;
    private String ename;
    private String job;
    private Date hiredate;

    public Date getHiredate() {
        return hiredate;
    }

    public void setHiredate(Date hiredate) {
        this.hiredate = hiredate;
    }

    public int getEmpno() {
        return empno;
    }

    public void setEmpno(int empno) {
        this.empno = empno;
    }

    public int getMgr() {
        return mgr;
    }

    public void setMgr(int mgr) {
        this.mgr = mgr;
    }

    public int getSal() {
        return sal;
    }

    public void setSal(int sal) {
        this.sal = sal;
    }

    public String getEname() {
        return ename;
    }

    public void setEname(String ename) {
        this.ename = ename;
    }

    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
    }

    public int getDeptno() {
        return deptno;
    }

    public void setDeptno(int deptno) {
        this.deptno = deptno;
    }

    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }
}
 

EmpRegisterController 생성

  • 사원정보를 저장하는 EmpRegisterController를 생성한다.
  • 사원정보 입력 폼으로 이용하는 form 메소드와 정보를 저장하는 역할을 하는 register 두 개의 메소드가 있다.
com.oracleclub.study.mvc.controller.EmpRegisterController.java
package com.oracleclub.study.mvc.controller;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
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.servlet.ModelAndView;

import com.oracleclub.study.mvc.model.Emp;

/**
 * 사원정보 저장을 처리
 * 
 * @author : gurubee
 * 
 */
@Controller
@RequestMapping(value = "/emp")
public class EmpRegisterController {
    private static final Log LOG = LogFactory.getLog(EmpRegisterController.class);

    @RequestMapping(value = "/form", method = RequestMethod.GET)
    public String form() {
        return "emp/form";
    }

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public ModelAndView register(@ModelAttribute Emp emp) {

        LOG.debug(" #### 사원정보 : " + emp);

        ModelAndView mv = new ModelAndView();
        mv.setViewName("emp/view");

        return mv;
    }
}  
  

form.jsp 생성

  • 사원 정보를 입력받는 form.jsp 파일을 아래 경로에 생성한다.
/WEB-INF/pages/emp/form.jsp
<%@ page language="java" isELIgnored="false" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c"   uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="ko">
<head>
<meta http-equiv="Content-type" content="text/html; charset=UTF-8">
<style>
.error {
    color: #ff0000;
}
.errorblock{
    color: #000;
    background-color: #ffEEEE;
    border: 3px solid #ff0000;
    padding:8px;
    margin:16px;
}
</style>
<script type="text/javascript" language="JavaScript">
    function showMessage(msg){
        if(msg != ''){
            alert(msg); 
        }   
    }

    function list(){
        location.href="/emp/list";
    }
</script>
</head>
<body >

<form:form commandName="emp" name="form" method="post" action="/emp/register">
<h2> 사원정보 등록 화면 </h2>
  <table width="280" border="0">
    <tr>
        <td align="left"> empno : </td>
        <td>
            <input type="text" id="empno" name="empno" value="${emp.empno}" maxlength="20" size="20"/>
            <br><form:errors path="empno" cssClass="error" />
        </td>
    </tr>            
    <tr>
        <td align="left"> ename : </td>
        <td>
            <input type="text" id="ename" name="ename" value="${emp.ename}" maxlength="20" size="20"/>
            <br><form:errors path="ename" cssClass="error" />
        </td>
    </tr>
    <tr>
        <td align="left"> job : </td>
        <td><input type="text" id="job" name="job" value="${emp.job}" maxlength="20" size="20"/></td>
    </tr>
    <tr>
        <td align="left"> sal : </td>
        <td>
            <input type="text" id="sal" name="sal" value="${emp.sal}" maxlength="20" size="20"/>
            <br><form:errors path="sal" cssClass="error" />
        </td>
    </tr>
    <tr>
        <td align="left"> deptno : </td>
        <td>
            <select name="deptno">
                <option value="10" <c:if test="${emp.deptno == 10}">selected</c:if>>10</option>
                <option value="20" <c:if test="${emp.deptno == 20}">selected</c:if>>20</option>
                <option value="30" <c:if test="${emp.deptno == 30}">selected</c:if>>30</option>
            </select>
        </td>
    </tr>
    <tr height="10"><td></td></tr>
    <tr>
        <td align="center" colspan="2">
            <input type="submit" value="저장" /> 
            <input type="button" value="취소" onclick="list()"/>
        </td>
    </tr>
  </table>
  
</form:form>
</body>
</html>
 

view.jsp 생성

  • 사원 상세정보를 보여주는 view.jsp 파일을 아래 경로에 생성한다.
/WEB-INF/pages/emp/view.jsp
<%@ page language="java" isELIgnored="false" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c"   uri="http://java.sun.com/jsp/jstl/core" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="ko">
<head>
<meta http-equiv="Content-type" content="text/html; charset=UTF-8">

<script type="text/javascript" language="JavaScript">
    function showMessage(msg){
        if(msg != ''){
            alert(msg); 
        }   
    }

    function insertForm(){
        location.href="/emp/form";
    }
        
    function listAct(){
        location.href="/emp/list";
    }
</script>
</head>
<body >

<form name="form" method="post" action="/emp/form">
<h2> 사원정보 상세 화면 </h2>
  <table width="280" border="0">
    <tr>
        <td align="left"> empno : </td>
        <td>${emp.empno}</td>
    </tr>            
    <tr>
        <td align="left"> ename : </td>
        <td>${emp.ename}</td>
    </tr>
    <tr>
        <td align="left"> job : </td>
        <td>${emp.job}</td>
    </tr>
    <tr>
        <td align="left"> sal : </td>
        <td>${emp.sal}</td>
    </tr>
    <tr>
        <td align="left"> deptno : </td>
        <td>${emp.deptno}</td>
    </tr>
    <tr height="10"><td></td></tr>
    <tr>
        <td align="center" colspan="2">
            <input type="button" value="등록" onclick="insertForm()" /> 
            <input type="button" value="목록" onclick="listAct()" />
        </td>
    </tr>
  </table>
  
</form>
</body>
</html> 
 

테스트

  • 저장 버튼을 클릭하면 아래와 같이 사원정보가 출력되는 것을 확인 할 수 있다.

문서정보

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.