티스토리 뷰

1. 게시판 프로젝트 기본 구조



  • bController 

  • bCommand - 실제로 작업하는 부분(Class: Content,Delete,List,Modify)

  • bDAO(database Access Object) object - 데이터베이스에 직접 접근하여 작업하는 역할

  • bDTO(database transfer Object) - Command역할


2. 각각의 java파일을 생성하고 Controller 제작

Controller의 역할 :클라이언트의 요청에 따른 전체적인 작업을 지휘하는 Controller 제작하기

<Controller 처리 구조>

자동으로 Spring파일 생성시 위의 코드는 자동으로 생성되고, 저 코드의 흐름에 따라서 Client->Dispatcher->Controller 로서 처리하게 된다. 이때, Controller.java파일에 @Controller Annotation을 시스템에서 확인한후 매칭되는 원리



2. Basic Code Review

web.xml

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 
 
 
    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
 
    <context-param>
 
        <param-name>contextConfigLocation</param-name>
 
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
 
    </context-param>
 
    
 
    <!-- Creates the Spring Container shared by all Servlets and Filters -->
 
    <listener>
 
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 
    </listener>
 
 
 
    <!-- Processes application requests -->
 
    <servlet>
 
        <servlet-name>appServlet</servlet-name>
 
디스패처 
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 
        <init-param>
 
            <param-name>contextConfigLocation</param-name>
 
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
 
servlet-context 부분으로 연결해주는 부분
        </init-param>
 
        <load-on-startup>1</load-on-startup>
 
    </servlet>
 
        
 
    <servlet-mapping>
 
        <servlet-name>appServlet</servlet-name>
 
        <url-pattern>/</url-pattern>
 
    </servlet-mapping>
 
 
 
</web-app>
cs

servlet-context.xml

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?xml version="1.0" encoding="UTF-8"?>
 
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
 
 
 
    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
 
    
 
    <!-- Enables the Spring MVC @Controller programming model -->
 
    <annotation-driven />
 
 
 
    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
 
    <resources mapping="/resources/**" location="/resources/" />
 
 
 
    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
 
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
 
        <beans:property name="prefix" value="/WEB-INF/views/" />
 
        <beans:property name="suffix" value=".jsp" />
 
    </beans:bean>
 
    
 
    <context:component-scan base-package="com.javalec.spring_pjt_board" />
 
    컴포넌트 스캔 구간
    
 
    
 
</beans:beans>
cs
코드를 보면 web.xml -> servlet-context.xml로 넘어 가는 부분을 볼 수 있음.


Controller.java

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package com.javalec.spring_pjt_board_controller;
 
 
 
import javax.servlet.http.HttpServletRequest;
 
 
 
import org.springframework.stereotype.Controller;
 
import org.springframework.ui.Model;
 
import org.springframework.web.bind.annotation.RequestMapping;
 
import org.springframework.web.bind.annotation.RequestMethod;
 
 
 
import com.javalec.spring_pjt_board_command.BCommand;
 
import com.javalec.spring_pjt_board_command.BContentCommand;
 
import com.javalec.spring_pjt_board_command.BDeleteCommand;
 
import com.javalec.spring_pjt_board_command.BListCommand;
 
import com.javalec.spring_pjt_board_command.BModifyCommand;
 
import com.javalec.spring_pjt_board_command.BReplyCommand;
 
import com.javalec.spring_pjt_board_command.BWriteCommand;
 
 
 
@Controller
 
public class BController {
 
    BCommand command;
 
    
 
    //1.  list로 요청이 들어왔을 경우
 
    @RequestMapping("/list")
 
    public String list(Model model) {
 
        System.out.println("list()");
 
        command = new BListCommand();
 
        command.execute(model);
 
        
 
        return "list";
 
        
 
    }
 
    // 2. write_view로 요청이 들어왔을 경
 
    @RequestMapping("/write_view")
 
    public String write_view(Model model) {
 
        System.out.println("write_view Call");
 
        
 
        return "wrtie_view";
 
    }
 
    
 
    // 3. 데이터 처리를 해주기 위한 Mapping(HttpServeltReqeust를 받는
 
    // 왜? 위에서 처리한 write_view의 폼을 받기 위해
 
    // 여기서 회원가입을 다 처리한다음에 이어질것을 list화면으로 redirect 하기위함
 
    // addAttribute 란 모델안에 "reqeust"라는 이름으로 모델에 추가후 jsp파일에서 ${reqeust} 형식으로 사용하기위
 
    @RequestMapping("/write")
 
    public String write(HttpServletRequest request,Model model) {
 
        System.out.println("write()");
 
        model.addAttribute("request",request);
 
        command = new BWriteCommand();
 
        command.execute(model);
 
        return "redirect:list";
 
        
 
    }
 
    
 
    // 4. 이제 list 가 보여지는 화면으로 나왔으므로, 이제 그화면에서 글을 클릭하여 그해당데이터로 이동해야 하는
 
    // 그경우를 만들어 준다.
 
    
 
    public String content_view(HttpServletRequest request,Model model) {
 
        System.out.println("content_view()");
 
        model.addAttribute("reqeust",request);
 
        command = new BContentCommand();
 
        command.execute(model);
 
        
 
        
 
        
 
        return "content_view";
 
        
 
    
 
    }
 
    // 5. Modify부분 처리
 
    
 
    @RequestMapping(method = RequestMethod.POST,value = "/modify")
 
    public String modify(HttpServletRequest request,Model model) {
 
        System.out.println("modify()");
 
        model.addAttribute("request",request);
 
        command = new BModifyCommand();
 
        command.execute(model);
 
        return "redirect:list";
 
        
 
        
 
    }
 
    
 
    //6. 답변 보는 부분
 
    
 
    @RequestMapping("/reply_view")
 
    public String reply_view(HttpServletRequest request,Model model) {
 
        System.out.println("reply_view()");
 
        model.addAttribute("reqeust",request);
 
        command = new BReplyCommand();
 
        command.execute(model);
 
        
 
        return "/reply_view";
 
    }
 
    //7.답변응답 부분
 
    
 
    @RequestMapping("/relply")
 
    public String reply(HttpServletRequest reqeust,Model model) {
 
        System.out.println("reply()");
 
        model.addAttribute("reqeust",reqeust);
 
        command = new BReplyCommand();
 
        command.execute(model);
 
        
 
        return "redirect:list";
 
    }
 
    
 
    // 8. 삭제 부
 
    @RequestMapping("/delete")
 
    public String delete(HttpServletRequest request,Model model) {
 
        System.out.println("Delete()");
 
        
 
        model.addAttribute("request",request);
 
        command = new BDeleteCommand();
 
        command.execute(model);
 
        
 
        return "redirect:list";
 
        
 
        
 
    }
 
    
 
 
 
}
 
cs

여기까지 Controller까지 완성되었으므로 다음 단계는 -> List Page만들기

가장먼저, 우선 List의 Command 와 DAO , DTO 부분을 설계한다.

**중요 : Controller-> Command 처리 -> DAO 호출 및 작업 -> DTO에서 작업후 -> DTO객체를 Model Object에 넣는다.

ListCommand.java

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.javalec.spring_pjt_board_command;
 
 
import java.util.ArrayList;
 
 
 
import org.springframework.ui.Model;
 
 
 
import com.javalec.spring_pjt_board_dao.BDao;
 
import com.javalec.spring_pjt_board_dto.BDto;
 
 
 
public class BListCommand implements BCommand {
 
 
 
    //JSP 페이지에다가 DTO 객체를넘겨서 DTO객체 부터 데이터가 이쁘게 뿌릴수 있게해야
 
    @Override
 
    public void execute(Model model) {
 
        // TODO Auto-generated method stub
 
        BDao dao = new BDao();
 
        ArrayList<BDto>dtos = dao.list();
 
        model.addAttribute("list",dtos);
 
        
 
 
 
    }
 
 
 
}
 
 
cs

BDao.java

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
29
30
31
32
33
34
35
36
37
38
39
40
package com.javalec.spring_pjt_board_dao;
 
 
import java.util.ArrayList;
 
 
 
import com.javalec.spring_pjt_board_dto.BDto;
 
 
 
//database에 접근해서 작업 하는구
 
public class BDao {
 
 
 
    public ArrayList<BDto> list() {
 
        // TODO Auto-generated method stub
 
        ArrayList<BDto> dtos = null;
 
        
 
        
 
        
 
        return dtos;
 
        
 
        
 
    }
 
 
 
}
cs

데이터들을 ArrayList에서 처리하기위해서 타입은 BDto로서 처리해준다. Dao에서 작업을 끝내고 Dto에서 데이터들을 객체화 시켜서 가져온다. 그이후에 Dto객체들을 Model 에다가 넣어준다.


BDto.java


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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package com.javalec.spring_pjt_board_dto;
 
// Database의 데이터->객체로 바꿔주는 부분
 
 
 
import java.sql.Timestamp;
 
 
 
public class BDto {
 
    
 
    
 
    int bId;
 
    String bName;
 
    String bTitle;
 
    String bContent;
 
    Timestamp bDate;
 
    int bHit;
 
    int bGroup;
 
    int bStep;
 
    int bIndent;
 
    
 
 
 
    // 파라미터 없는 Structure
 
    public BDto() {
 
        // TODO Auto-generated constructor stub
 
    }
 
    
 
    // 파라미터 있는 Structrue
 
    public BDto(int bId,String bName,String bTitle,String bContent,Timestamp bDate,int bHit,int bGroup,
 
            int bStep,int bIndent) {
 
        // TODO Auto-generated constructor stub
 
        this.bId = bId;
 
        this.bName = bName;
 
        this.bTitle = bTitle;
 
        this.bDate = bDate;
 
        this.bContent = bContent;
 
        this.bHit = bHit;
 
        this.bGroup = bGroup;
 
        this.bStep = bStep;
 
        this.bIndent = bIndent;
 
        
 
    }
 
 
 
    public int getbId() {
 
        return bId;
 
    }
 
 
 
    public void setbId(int bId) {
 
        this.bId = bId;
 
    }
 
 
 
    public String getbName() {
 
        return bName;
 
    }
 
 
 
    public void setbName(String bName) {
 
        this.bName = bName;
 
    }
 
 
 
    public String getbTitle() {
 
        return bTitle;
 
    }
 
 
 
    public void setbTitle(String bTitle) {
 
        this.bTitle = bTitle;
 
    }
 
 
 
    public String getbContent() {
 
        return bContent;
 
    }
 
 
 
    public void setbContent(String bContent) {
 
        this.bContent = bContent;
 
    }
 
 
 
    public Timestamp getbDate() {
 
        return bDate;
 
    }
 
 
 
    public void setbDate(Timestamp bDate) {
 
        this.bDate = bDate;
 
    }
 
 
 
    public int getbHit() {
 
        return bHit;
 
    }
 
 
 
    public void setbHit(int bHit) {
 
        this.bHit = bHit;
 
    }
 
 
 
    public int getbGroup() {
 
        return bGroup;
 
    }
 
 
 
    public void setbGroup(int bGroup) {
 
        this.bGroup = bGroup;
 
    }
 
 
 
    public int getbStep() {
 
        return bStep;
 
    }
 
 
 
    public void setbStep(int bStep) {
 
        this.bStep = bStep;
 
    }
 
 
 
    public int getbIndent() {
 
        return bIndent;
 
    }
 
 
 
    public void setbIndent(int bIndent) {
 
        this.bIndent = bIndent;
 
    }
 
    
cs


}

게시판의 기본설계가 종료되었고, 이제 처음부터 흐름을 파악해서 나머지것들도 구현을 하면 될것같다.







공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/02   »
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
글 보관함