영보의 SystemOut.log

[Spring] Spring 데이터 그리드 리스트 출력하기 본문

Spring

[Spring] Spring 데이터 그리드 리스트 출력하기

영보로그 2020. 11. 3. 16:52
반응형

 

 

 

 

qh5944.tistory.com/77?category=419593

 

[Web] JSTL , CSS 이용하여 레시피 그리드 목록 출력-(1)

www.10000recipe.com/ 만개의레시피 www.10000recipe.com 만개의 레시피 사이트에서 데이터를 긁어와서 JSTL과 CSS, XML, Java로 레시피 그리드를 만들어 보겠습니다. MV 구조로 만든 프로그램 입니다. 사용한 라.

qh5944.tistory.com

일전에 이클립스 자바 코딩으로 올렸었던 위의 게시물을

더 간단한 스프링으로 작성해 보려고 한다.

 

 

 

MVC 모델을 이해를 돕기 위해 음식점으로 비교해보았다.

 

 

 

먼저, [현재 프로젝트 우클릭] - [Properties]

[Project Factes] , [Java Compiler]에서 Java 1.8버전으로 환경설정 했습니다.

 

 

 

 

 

 

# pom.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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.sist</groupId>
    <artifactId>web</artifactId>
    <name>OnLineSpringMVCStudy3</name>
    <packaging>war</packaging>
    <version>1.0.0-BUILD-SNAPSHOT</version>
    <properties>
      <!-- Spring 5 -->
      <java-version>1.8</java-version>
      <org.springframework-version>5.1.5.RELEASE</org.springframework-version>
      <org.aspectj-version>1.6.10</org.aspectj-version>
      <org.slf4j-version>1.6.6</org.slf4j-version>
   </properties>
   <repositories>
      <repository>
          <id>mesir-repo</id>
          <url>http://mesir.googlecode.com/svn/trunk/mavenrepo</url>
      </repository>
   </repositories>
   <dependencies>
      <!-- Spring -->
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-context</artifactId>
         <version>${org.springframework-version}</version>
         <exclusions>
            <!-- Exclude Commons Logging in favor of SLF4j -->
            <exclusion>
               <groupId>commons-logging</groupId>
               <artifactId>commons-logging</artifactId>
             </exclusion>
         </exclusions>
      </dependency>
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-webmvc</artifactId>
         <version>${org.springframework-version}</version>
      </dependency>
       <!-- Jsoup -->
       <dependency>
          <groupId>org.jsoup</groupId>
          <artifactId>jsoup</artifactId>
          <version>1.12.1</version>
      </dependency>   
      <!-- JSON -->
      <dependency>
          <groupId>com.googlecode.json-simple</groupId>
          <artifactId>json-simple</artifactId>
          <version>1.1.1</version>
      </dependency>
      <!-- JAXB -->
      <dependency>
          <groupId>javax.xml.bind</groupId>
          <artifactId>jaxb-api</artifactId>
          <version>2.3.1</version>
      </dependency>
      <!-- AspectJ -->
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-context-support</artifactId>
         <version>5.1.5.RELEASE</version>
      </dependency>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>5.1.5.RELEASE</version>
      </dependency>
      <!-- lombok -->
      <dependency>
          <groupId>org.projectlombok</groupId>
          <artifactId>lombok</artifactId>
          <version>1.18.8</version>
          <scope>provided</scope>
      </dependency>
      <!-- websocket -->
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-websocket</artifactId>
          <version>5.1.5.RELEASE</version>
      </dependency>
      
      <!-- <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-expression</artifactId>
         <version>5.1.5.RELEASE</version>
      </dependency> -->
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-jdbc</artifactId>
         <version>5.1.5.RELEASE</version>
      </dependency>
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-instrument</artifactId>
         <version>5.1.5.RELEASE</version>
      </dependency>
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-aspects</artifactId>
         <version>5.1.5.RELEASE</version>
      </dependency>
      <!-- <dependency>
         <groupId>com.oracle</groupId>
         <artifactId>ojdbc14</artifactId>
         <version>10.2.0.4.0</version>
      </dependency> -->
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-jms</artifactId>
         <version>5.1.5.RELEASE</version>
      </dependency>
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-orm</artifactId>
         <version>5.1.5.RELEASE</version>
      </dependency>
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-oxm</artifactId>
         <version>5.1.5.RELEASE</version>
      </dependency>
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-test</artifactId>
         <version>5.1.5.RELEASE</version>
      </dependency>
      <!-- <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-webmvc-portlet</artifactId>
         <version>5.1.5.RELEASE</version>
      </dependency> -->
      
      <dependency>
         <groupId>taglibs</groupId>
         <artifactId>standard</artifactId>
         <version>1.1.2</version>
      </dependency>
 
      <dependency>
         <groupId>org.nuiton.thirdparty</groupId>
         <artifactId>Rserve</artifactId>
         <version>1.7-3</version>
      </dependency>
      <dependency>
         <groupId>org.nuiton.thirdparty</groupId>
         <artifactId>REngine</artifactId>
         <version>1.7-3</version>
      </dependency>
      <!-- 
         @Select("<script>SELECT~~ <if test="">")
       -->
      <dependency>
         <groupId>org.mybatis</groupId>
         <artifactId>mybatis</artifactId>
         <version>3.2.8</version>
      </dependency>
      <dependency>
         <groupId>org.mybatis</groupId>
         <artifactId>mybatis-spring</artifactId>
         <version>1.2.0</version>
      </dependency>
      <dependency>
         <groupId>org.mongodb</groupId>
         <artifactId>mongo-java-driver</artifactId>
         <version>2.13.0-rc1</version>
      </dependency>
      <dependency>
         <groupId>org.nuiton.thirdparty</groupId>
         <artifactId>JRI</artifactId>
         <version>0.9-6</version>
      </dependency>
      <dependency>
         <groupId>com.github.lucarosellini.rJava</groupId>
         <artifactId>JRIEngine</artifactId>
         <version>0.9-7</version>
      </dependency>
      <dependency>
         <groupId>aopalliance</groupId>
         <artifactId>aopalliance</artifactId>
         <version>1.0</version>
      </dependency>
      <dependency>
         <groupId>asm</groupId>
         <artifactId>asm</artifactId>
         <version>3.3.1</version>
      </dependency>
      <dependency>
         <groupId>c3p0</groupId>
         <artifactId>c3p0</artifactId>
         <version>0.9.1.2</version>
      </dependency>
      <dependency>
         <groupId>cglib</groupId>
         <artifactId>cglib</artifactId>
         <version>3.1</version>
      </dependency>
      <dependency>
         <groupId>cglib</groupId>
         <artifactId>cglib-nodep</artifactId>
         <version>3.1</version>
      </dependency>
      <dependency>
         <groupId>commons-collections</groupId>
         <artifactId>commons-collections</artifactId>
         <version>3.2.1</version>
      </dependency>
      <dependency>
         <groupId>commons-dbcp</groupId>
         <artifactId>commons-dbcp</artifactId>
         <version>1.4</version>
      </dependency>
      <dependency>
         <groupId>commons-fileupload</groupId>
         <artifactId>commons-fileupload</artifactId>
         <version>1.3.1</version>
      </dependency>
      <dependency>
         <groupId>commons-io</groupId>
         <artifactId>commons-io</artifactId>
         <version>2.4</version>
      </dependency>
      <dependency>
         <groupId>commons-logging</groupId>
         <artifactId>commons-logging</artifactId>
         <version>1.2</version>
      </dependency>
      <dependency>
         <groupId>commons-pool</groupId>
         <artifactId>commons-pool</artifactId>
         <version>1.6</version>
      </dependency>
      <dependency>
         <groupId>org.javassist</groupId>
         <artifactId>javassist</artifactId>
         <version>3.18.2-GA</version>
      </dependency>
     
      <dependency>
         <groupId>org.nuiton.thirdparty</groupId>
         <artifactId>Rserve</artifactId>
         <version>1.7-3</version>
      </dependency>
      <dependency>
         <groupId>org.nuiton.thirdparty</groupId>
         <artifactId>REngine</artifactId>
         <version>1.7-3</version>
      </dependency>
      <dependency>
         <groupId>org.twitter4j</groupId>
         <artifactId>twitter4j-core</artifactId>
         <version>4.0.2</version>
      </dependency>
        <dependency>
         <groupId>org.twitter4j</groupId>
         <artifactId>twitter4j-stream</artifactId>
         <version>4.0.2</version>
      </dependency>
        <dependency>
         <groupId>javax.validation</groupId>
         <artifactId>validation-api</artifactId>
         <version>1.1.0.Final</version>
      </dependency>
       <!--  <dependency>
         <groupId>com.oracle</groupId>
         <artifactId>ojdbc14</artifactId>
         <version>10.2.0.4.0</version>
      </dependency> -->
        
      <!-- End -->   
      <!-- AspectJ -->
      <dependency>
         <groupId>org.aspectj</groupId>
         <artifactId>aspectjrt</artifactId>
         <version>${org.aspectj-version}</version>
      </dependency>   
       <!-- AOP -->
      <dependency>
         <groupId>org.aspectj</groupId>
         <artifactId>aspectjweaver</artifactId>
         <version>1.6.5</version>
      </dependency>
      <!-- AOP -->
      <dependency>
         <groupId>aopalliance</groupId>
         <artifactId>aopalliance</artifactId>
         <version>1.0</version>
      </dependency>
      <!-- AOP -->
      <dependency>
      <groupId>asm</groupId>
      <artifactId>asm</artifactId>
      <version>3.1</version>
   </dependency>
      <!-- tiles  -->
      <dependency>
         <groupId>org.apache.tiles</groupId>
         <artifactId>tiles-core</artifactId>
         <version>2.2.2</version>
      </dependency>
      <dependency>
         <groupId>org.apache.tiles</groupId>
         <artifactId>tiles-jsp</artifactId>
         <version>2.2.2</version>
      </dependency>
      <dependency>
         <groupId>org.apache.tiles</groupId>
         <artifactId>tiles-api</artifactId>
         <version>2.2.2</version>
      </dependency>
      
      <dependency>
         <groupId>org.apache.tiles</groupId>
         <artifactId>tiles-servlet</artifactId>
         <version>2.2.2</version>
      </dependency>
      
      <dependency>
         <groupId>commons-beanutils</groupId>
         <artifactId>commons-beanutils</artifactId>
         <version>1.9.2</version>
      </dependency>
      
      <dependency>
         <groupId>commons-digester</groupId>
         <artifactId>commons-digester</artifactId>
         <version>2.1</version>
      </dependency>
      
      <dependency>
         <groupId>org.antlr</groupId>
         <artifactId>antlr-runtime</artifactId>
         <version>3.5.2</version>
      </dependency>
      <!-- XML -->
      <dependency>
         <groupId>com.sun.xml.bind</groupId>
         <artifactId>jaxb-core</artifactId>
         <version>2.2.11</version>
      </dependency>
      <dependency>
         <groupId>com.sun.xml.bind</groupId>
         <artifactId>jaxb-impl</artifactId>
         <version>2.2.11</version>
      </dependency>
 
      <dependency>
         <groupId>javax.xml.bind</groupId>
         <artifactId>jaxb-api</artifactId>
         <version>2.2.11</version>
      </dependency>
      
      <!-- 
         jaxb-core
         jaxb-impl
         jaxb-api
         
         json-simple
         commons-lang
         ezmorph
       -->
      <!-- JSON -->
      <dependency>
         <groupId>com.googlecode.json-simple</groupId>
         <artifactId>json-simple</artifactId>
         <version>1.1.1</version>
      </dependency>
      <dependency>
         <groupId>commons-lang</groupId>
         <artifactId>commons-lang</artifactId>
         <version>2.6</version>
      </dependency>
      <dependency>
         <groupId>net.sf.ezmorph</groupId>
         <artifactId>ezmorph</artifactId>
         <version>1.0.6</version>
      </dependency>
      
      <!-- Logging -->
      <dependency>
         <groupId>org.slf4j</groupId>
         <artifactId>slf4j-api</artifactId>
         <version>${org.slf4j-version}</version>
      </dependency>
      <dependency>
         <groupId>org.slf4j</groupId>
         <artifactId>jcl-over-slf4j</artifactId>
         <version>${org.slf4j-version}</version>
         <scope>runtime</scope>
      </dependency>
      <dependency>
         <groupId>org.slf4j</groupId>
         <artifactId>slf4j-log4j12</artifactId>
         <version>${org.slf4j-version}</version>
         <scope>runtime</scope>
      </dependency>
      <dependency>
         <groupId>log4j</groupId>
         <artifactId>log4j</artifactId>
         <version>1.2.15</version>
         <exclusions>
            <exclusion>
               <groupId>javax.mail</groupId>
               <artifactId>mail</artifactId>
            </exclusion>
            <exclusion>
               <groupId>javax.jms</groupId>
               <artifactId>jms</artifactId>
            </exclusion>
            <exclusion>
               <groupId>com.sun.jdmk</groupId>
               <artifactId>jmxtools</artifactId>
            </exclusion>
            <exclusion>
               <groupId>com.sun.jmx</groupId>
               <artifactId>jmxri</artifactId>
            </exclusion>
         </exclusions>
         <scope>runtime</scope>
      </dependency>
 
      <!-- @Inject -->
      <dependency>
         <groupId>javax.inject</groupId>
         <artifactId>javax.inject</artifactId>
         <version>1</version>
      </dependency>
            
      <!-- Servlet -->
      <dependency>
         <groupId>javax.servlet</groupId>
         <artifactId>servlet-api</artifactId>
         <version>2.5</version>
         <scope>provided</scope>
      </dependency>
      <dependency>
         <groupId>javax.servlet.jsp</groupId>
         <artifactId>jsp-api</artifactId>
         <version>2.1</version>
         <scope>provided</scope>
      </dependency>
      <dependency>
         <groupId>javax.servlet</groupId>
         <artifactId>jstl</artifactId>
         <version>1.2</version>
      </dependency>
   
      <!-- Test -->
      <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>4.7</version>
         <scope>test</scope>
      </dependency>        
   </dependencies>
 
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <additionalProjectnatures>
                        <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
                    </additionalProjectnatures>
                    <additionalBuildcommands>
                        <buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
                    </additionalBuildcommands>
                    <downloadSources>true</downloadSources>
                    <downloadJavadocs>true</downloadJavadocs>
                </configuration>
            </plugin>
            <plugin>
                <!-- Spring 5 -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <compilerArgument>-Xlint:all</compilerArgument>
                    <showWarnings>true</showWarnings>
                    <showDeprecation>true</showDeprecation>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <configuration>
                    <mainClass>org.test.int1.Main</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
     
cs

- 환경설정과 라이브러리를 pom.xml에서 추가해줍니다.

 

 

 

#RecipeVO

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
package com.sist.dao;
 
public class RecipeVO {
    private int no;
    private String title;
    private String poster;
    private String chef;
    private String link;
    public int getNo() {
        return no;
    }
    public void setNo(int no) {
        this.no = no;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getPoster() {
        return poster;
    }
    public void setPoster(String poster) {
        this.poster = poster;
    }
    public String getChef() {
        return chef;
    }
    public void setChef(String chef) {
        this.chef = chef;
    }
    public String getLink() {
        return link;
    }
    public void setLink(String link) {
        this.link = link;
    }
}
cs

- 미리 Oracle에 생성해놓은 데이터를 VO에서 이클립스와 연동하기 위해 getter/setter로 캡슐화 코딩을 해줍니다.

 

#recipe-mapper.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
<?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.sist.dao.recipe-mapper">
  <select id="recipeListData" resultType="RecipeVO" parameterType="hashmap">
    SELECT no,title,chef,poster,num 
    FROM (SELECT no,title,chef,poster,rownum as num 
    FROM (SELECT no,title,chef,poster 
    FROM recipe))
    WHERE num BETWEEN #{start} AND #{end}
  </select>
  <!-- 총페이지  -->
  <select id="recipeTotalPage" resultType="int">
    SELECT CEIL(COUNT(*)/20.0) FROM recipe
  </select>
  <!-- chef 데이터 -->
  <select id="recipeChefData" resultType="RecipeVO" parameterType="hashmap">
    SELECT no,title,chef,poster,num 
    FROM (SELECT no,title,chef,poster,rownum as num 
    FROM (SELECT no,title,chef,poster 
    FROM recipe WHERE chef=#{chef}))
    WHERE num BETWEEN #{start} AND #{end}
  </select>
  <select id="recipeChefTotalPage" resultType="int" parameterType="string">
    SELECT CEIL(COUNT(*)/20.0) FROM recipe
    WHERE chef=#{chef}
  </select>
</mapper>
cs

- mapper : SQL문장이 어디에 있는지 확인하기 위해서 사용
- XML을 씀으로써 귀찮은 코드들이 줄고 사용자 정의기 때문에 데이터 전달이 더 쉬워집니다
  추가로 Connection, PreParedStatement, ResultSet 등등을 MyBatis에서 해주기 때문에 유지보수에도 용이합니다.

 

 

# RecipeDAO

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.sist.dao;
 
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.*;
// 스프링에 메모리 할당 요청 
@Repository
public class RecipeDAO extends SqlSessionDaoSupport{
    @Autowired
    public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
        // TODO Auto-generated method stub
        super.setSqlSessionFactory(sqlSessionFactory);
    }
    
    public List<RecipeVO> recipeListData(Map map)
    {
        /*
         *   getSqlSession() => 1. session=ssf.openSession()
         *                      2. session.close()
         */
        return  getSqlSession().selectList("recipeListData",map);
    }
    
    public int recipeTotalPage()
    {
        return getSqlSession().selectOne("recipeTotalPage");
    }
    
    public List<RecipeVO> recipeChefData(Map map)
    {
        return getSqlSession().selectList("recipeChefData",map);
    }
  
    public int recipeChefTotalPage(String chef)
    {
        return getSqlSession().selectOne("recipeChefTotalPage", chef);
    }
}
cs

- DAO : xml 파싱 후 결과값을 받는 위치로 DAO에서 메소드를 호출해준다.

- Config.xml에 등록되어 있는 모든 XML파일을 읽는다.

- XML을 파싱하면 마이바티스 라이브러리가 필요한 데이터를 읽어간다. => 저장 후 사용

 

 

# RecipeController.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
package com.sist.web;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import com.sist.dao.RecipeDAO;
import com.sist.dao.RecipeVO;
import java.util.*;
 
@Controller
public class RecipeController {
    @Autowired
    private RecipeDAO dao;
    
    // 사용자가 요청이 있을 때 처리 
    // 웹 => 사용자 요청 (URL주소)  ==> 서버주소(X) ==> URI  ==> .do
    @RequestMapping("recipe/list.do")
    // 사용자 요청 처리 
    public String recipe_list(String page,Model model)
    {
        if(page==null)
            page="1";
        int curpage=Integer.parseInt(page);
        // 데이터 받기 => 스프링에서 메모리 할당=>한개만 저장 (재사용) = 싱글턴
        int rowSize=20;
        int start=(rowSize*curpage)-(rowSize-1);
        int end=rowSize*curpage;
        // Map에서 채워서 => DAO로 전송 
        Map map=new HashMap();
        map.put("start", start);
        map.put("end", end);
        List<RecipeVO> list=dao.recipeListData(map);
        for(RecipeVO vo:list)
        {
            String title=vo.getTitle();
            if(title.length()>22)
            {
                title=title.substring(0,22);
                title+="...";
            }
            vo.setTitle(title);
        }
        // 총페이지 
        int totalpage=dao.recipeTotalPage();
        
        // 전송 
        model.addAttribute("curpage", curpage);
        model.addAttribute("totalpage", totalpage);
        model.addAttribute("list", list);
        return "recipe/list";// forward => list.jsp=>request를 전송 
    }
    /*
     *    @RequestMapping() => 통합 (GET,POST)
     *    @GetMapping => Get방식으로 전송 
     *                   <a> ,sendRedirect() , location.href
     *    @PostMapping => ajax , form
     */
    @GetMapping("recipe/chef.do")
    public String recipe_chef(String page,String chef,Model model)
    {
        // DB연동
        if(page==null)
            page="1";
        int curpage=Integer.parseInt(page);
        int rowSize=20;
        int start=(rowSize*curpage)-(rowSize-1);
        int end=rowSize*curpage;
        Map map=new HashMap();
        map.put("start", start);
        map.put("end", end);
        map.put("chef", chef);
        List<RecipeVO> list=dao.recipeChefData(map);
        for(RecipeVO vo:list)
        {
            String title=vo.getTitle();
            if(title.length()>22)
            {
                title=title.substring(0,22);
                title+="...";
            }
            vo.setTitle(title);
        }
        // 총페이지 
        int totalpage=dao.recipeChefTotalPage(chef);
        
        // 전송 
        model.addAttribute("curpage", curpage);
        model.addAttribute("totalpage", totalpage);
        model.addAttribute("chef", chef);
        model.addAttribute("list", list);
        return "recipe/chef";
    }
    
}
cs

 

 

# application-context.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:c="http://www.springframework.org/schema/c"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="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-4.3.xsd">
    <!-- 
         사용자가 만든 클래스 등록 = 패키지 단위로 등록 
     -->
     <context:component-scan base-package="com.sist.*"/>
</beans>
cs

 

 

 

# application-datasource.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
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:c="http://www.springframework.org/schema/c"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">
    <!-- DataBase 관련 : JDBC , ORM (MyBatis , Hibernate) , Spring-Data(MongoDB:NoSQL) -->
    <!-- MyBatis연결 -->
    <!-- properties 파일 읽기  -->
    <util:properties id="db" location="/WEB-INF/config/db.properties"/>
    <!-- 데이터베이스 연결 정보를 모아서  -->
    <bean id="ds" class="org.apache.commons.dbcp.BasicDataSource"
      p:driverClassName="#{db['driver']}"
      p:url="#{db['url']}"
      p:username="#{db['username']}"
      p:password="#{db['password']}"
      p:maxActive="#{db['maxActive']}"
      p:maxIdle="#{db['maxIdle']}"
      p:maxWait="#{db['maxWait']}"
    />
    <!-- 마이바티스로 전송 -->
    <bean id="ssf" class="org.mybatis.spring.SqlSessionFactoryBean"
     p:dataSource-ref="ds"
     p:configLocation="/WEB-INF/config/Config.xml"
    />
</beans>
 
cs

 

 

 

# application-web.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:c="http://www.springframework.org/schema/c"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
     <bean id="viewResolver"
         class="org.springframework.web.servlet.view.InternalResourceViewResolver"
         p:prefix="/"
         p:suffix=".jsp"
      />
</beans>
 
cs

 - 1. JSP 찾기(ViewResolver) - 2. 파일 업로드(MultipartResolver ) -3. Tiles(TilesView => include (템플릿 제작))

 - ViewResolver : JSP찾기 request를 전송
                    경로명 지정 : p:prefix="/"
                    확장자 지정 : p:suffix=".jsp"

 - Model => return

    return "main/main" => main/main.jsp

    prefix + return값 + suffix 

 

 

 

# Config.xml       

1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
   PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
   "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
  <typeAliases>
    <typeAlias type="com.sist.dao.RecipeVO" alias="RecipeVO"/>
  </typeAliases>
  <mappers>
    <mapper resource="com/sist/dao/recipe-mapper.xml"/>
  </mappers>
</configuration>
cs

 

# db.properties

1
2
3
4
5
6
7
driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@211.238.142.195:1521:XE
username=hr
password=happy
maxActive=20
maxIdle=10
maxWait=-1
cs

 - 일반 파일을 추가해서 경로/IP/Oracle id/pw 

 

 

 

# list.jsp

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
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body>
  <div class="container">
    <div class="row">
      <h3 class="text-center">레시피 목록</h3>
      <h5 class="text-right">
        ${curpage } page / ${totalpage } pages
      </h5>
      <c:forEach var="vo" items="${list }">
        <div class="col-md-3">
          <div class="thumbnail">
            <a href="#">
              <img src="${vo.poster }" alt="Lights" style="width:100%">
            </a>
              <div class="caption">
                <p style="font-size: 8pt">${vo.title }</p>
                <p style="font-size: 8pt">
                 <a href="chef.do?chef=${vo.chef }">${vo.chef }</a>
                </p>
              </div>
            
          </div>
        </div>
      </c:forEach>
    </div>
    <div class="row">
       <ul class="pager">
          <li class="previous"><a href="list.do?page=${curpage>1?curpage-1:curpage }">이전</a></li>
          <li class="next"><a href="list.do?page=${curpage<totalpage?curpage+1:curpage }">다음</a></li>
        </ul>
    </div>
  </div>
</body>
</html>
 
cs

- 출력 화면입니다

 

 

 

www.w3schools.com/bootstrap/tryit.asp?filename=trybs_img_thumbnail2&stacked=h

 

Tryit Editor v3.6

Bootstrap Example Image Gallery The .thumbnail class can be used to display an image gallery. The .caption class adds proper padding and a dark grey color to text inside thumbnails. Click on the images to enlarge them. Lorem ipsum donec id elit non mi port

www.w3schools.com

css코드는 여기서 가져왔습니다

반응형