티스토리 뷰
728x90
컨트롤러 클래스에서 입력받은 객체 전달
modelMap.addAttribute("userinfo", new UserInfo()); 내용 추가
package com.iot.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import com.iot.db.domain.UserInfo; import com.iot.db.service.UserInfoService; @Controller public class IotController { @Autowired private UserInfoService service; @RequestMapping(value = "/hello_world.iot", method = RequestMethod.GET) public String hello_world(ModelMap modelMap) throws Exception { // 사용자 목록 출력 List<UserInfo> userlist = service.listUserInfoFormData(); modelMap.addAttribute("userlist", userlist); // 사용자 추가용 객체 설정 modelMap.addAttribute("userinfo", new UserInfo()); return "/hello_world"; } }
JSP에 Form 테그 추가
참고 : 쉼, 그리고 망설임 없는 마침표. :: [toby의스프링] 13장 - 스프링 @MVC #3
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="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" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> Hello World <h1>사용자 조회</h1> <table border="1"> <c:forEach items="${userlist}" var="data" varStatus="loop"> <tr> <td>${data.user_id}</td> <td>${data.password}</td> <td>${data.name}</td> <td>${data.nickname}</td> </tr> </c:forEach> </table> <h1>사용자 추가</h1> <form:form id="dataForm" name="dataForm" modelAttribute="userinfo" action="user_add.iot" method="post"> <table border="1"> <tr> <td>사용자ID</td> <td> <form:input path="user_id"/> </td> </tr> <tr> <td>암호</td> <td> <form:input path="password"/> </td> </tr> <tr> <td>사용자이름</td> <td> <form:input path="name"/> </td> </tr> <tr> <td>별명</td> <td> <form:input path="nickname"/> </td> </tr> </table> <br/> <input type="submit"/> </form:form> </body> </html>
컨트롤러 클래스에 사용자 추가 메소드 생성
UserInfo 객체를 인자로 받음
// 사용자 추가 @RequestMapping(value = "/user_add.iot", method = RequestMethod.POST) public String user_add(UserInfo userinfo, ModelMap modelMap) throws Exception { service.insertUserInfo(userinfo); // 사용자 목록 출력 List<UserInfo> userlist = service.listUserInfoFormData(); modelMap.addAttribute("userlist", userlist); // 사용자 추가용 객체 설정 modelMap.addAttribute("userinfo", new UserInfo()); return "/hello_world"; }
서비스 클래스에 메소드 추가
public void insertUserInfo(UserInfo userInfo) { userInfoMapper.insertUserInfo(userInfo); }
Mapper 인터페이스에 메소드 추가
public void insertUserInfo(UserInfo userInfo);
Mapper XML에 Insert질의문 추가
<!-- insertUserInfo --> <insert id="insertUserInfo" parameterType="com.iot.db.domain.UserInfo" statementType="PREPARED"> insert into user_info( <trim suffixOverrides=","> <if test="user_id != null">user_id ,</if> <if test="password != null">password ,</if> <if test="name != null">name ,</if> <if test="nickname != null">nickname ,</if> </trim> ) values ( <trim suffixOverrides=","> <if test="user_id != null">#{user_id, jdbcType=VARCHAR} ,</if> <if test="password != null">#{password, jdbcType=VARCHAR} ,</if> <if test="name != null">#{name, jdbcType=VARCHAR} ,</if> <if test="nickname != null">#{nickname, jdbcType=VARCHAR} ,</if> </trim> ) </insert>
최종 IotController.java
package com.iot.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import com.iot.db.domain.UserInfo; import com.iot.db.service.UserInfoService; @Controller public class IotController { @Autowired private UserInfoService service; @RequestMapping(value = "/hello_world.iot", method = RequestMethod.GET) public String hello_world(ModelMap modelMap) throws Exception { // 사용자 목록 출력 List<UserInfo> userlist = service.listUserInfoFormData(); modelMap.addAttribute("userlist", userlist); // 사용자 추가용 객체 설정 modelMap.addAttribute("userinfo", new UserInfo()); return "/hello_world"; } // 사용자 추가 @RequestMapping(value = "/user_add.iot", method = RequestMethod.POST) public String user_add(UserInfo userinfo, ModelMap modelMap) throws Exception { service.insertUserInfo(userinfo); // 사용자 목록 출력 List<UserInfo> userlist = service.listUserInfoFormData(); modelMap.addAttribute("userlist", userlist); // 사용자 추가용 객체 설정 modelMap.addAttribute("userinfo", new UserInfo()); return "/hello_world"; } }
최종 UserInfoMapper.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.iot.db.mapper.UserInfoMapper"> <!-- insertUserInfo --> <insert id="insertUserInfo" parameterType="com.iot.db.domain.UserInfo" statementType="PREPARED"> insert into user_info( <trim suffixOverrides=","> <if test="user_id != null">user_id ,</if> <if test="password != null">password ,</if> <if test="name != null">name ,</if> <if test="nickname != null">nickname ,</if> </trim> ) values ( <trim suffixOverrides=","> <if test="user_id != null">#{user_id, jdbcType=VARCHAR} ,</if> <if test="password != null">#{password, jdbcType=VARCHAR} ,</if> <if test="name != null">#{name, jdbcType=VARCHAR} ,</if> <if test="nickname != null">#{nickname, jdbcType=VARCHAR} ,</if> </trim> ) </insert> <!-- listUserInfo --> <select id="listUserInfo" parameterType="map" resultType="com.iot.db.domain.UserInfo"> select * from user_info </select> </mapper>
댓글
300x250
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- oracle
- 송주경
- Java
- Delphi
- KOBA
- NDK
- Mac
- ffmpeg
- Spring
- Delphi Tip
- ble
- flex
- 일본여행
- ubuntu
- 전예희
- koba2010
- JavaScript
- 튜닝쇼 2008
- 서울오토살롱
- sas2009
- Linux
- 동경
- BPI-M4
- android
- Xcode
- 레이싱모델 익스트림 포토 페스티벌
- SAS
- Spring MVC
- 지스타2007
- MySQL
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
글 보관함