[ jsp ] 스프링 시작하기 , 설치 환경 설정 Spring for Web MVC model2 - 스프링을 이용한 MVC모델2 방식 > jsp&servlet

본문 바로가기

사이트 내 전체검색

jsp&servlet

[ jsp ] 스프링 시작하기 , 설치 환경 설정 Spring for Web MVC model2 - 스프링을 이용한 MVC모…

작성일 21-02-19 12:05

페이지 정보

작성자 웹지기 조회 4,807회 댓글 0건

본문

Spring for Web MVC model2  - 스프링을 이용한 MVC모델2 방식 설정 방법

1) 프로젝트 생성

eclipse > File > New > Spring > Spring Legacy Project > Next >

> Project name : 프로젝트명, Templates : Spring MVC Project > kr.ms.smartcar (접속경로 설정 - root 폴더)( kr.ms.smartcar=> localhost:포트/smartcar)


2) pom.xml 설정 

 프로젝트명/pom.xml

 - maven을 이용해서 필요한 파일을 다운로드 해주는 프로그램 설정

 -  /dependencies 검색해서 그 윗부분에 아래의 코드를 넣어준다.

    그리고 아래쪽에 </dependencies> 이부분이 오게 해주면 된다.

     오류를 방지하기 위해 dependencies 사이에서 하단쪽에 넣는게 좋다.


        <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->

        <dependency>

            <groupId>com.google.code.gson</groupId>

            <artifactId>gson</artifactId>

            <version>2.8.5</version>

        </dependency>


        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->

        <dependency>

            <groupId>org.mybatis</groupId>

            <artifactId>mybatis</artifactId>

            <version>3.4.4</version>

        </dependency>


        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->

        <dependency>

            <groupId>org.mybatis</groupId>

            <artifactId>mybatis-spring</artifactId>

            <version>1.3.2</version>

        </dependency>


        <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-jdbc</artifactId>

            <version>3.1.1.RELEASE</version>

        </dependency>


이제 plugins 의 안쪽에서

           <groupId>org.apache.maven.plugins</groupId>

                <artifactId>maven-compiler-plugin</artifactId>

                <version>2.5.1</version>

                <configuration>

                    <source>1.6</source>

                    <target>1.6</target>

부분에서 

<source>1.8</source>

로 수정


3) db생성 확인

 프로젝트명/패키지(/src/main/java/) 패키지에서 kr.ms.db 패키지 생성

  query.sql 파일을 만들어서 db 등록 쿼리 등록

  test.sql 파일을 만들어서 db등록 내용을 테스트


4) lib 폴더 생성

프로젝트명/src/main/webapp/WEB-INF/ 폴더에 lib 폴더 생성

.jar등의 라이브러리 복사 붙여넣기


5) web.xml 설정

프로젝트명/src/main/webapp/WEB-INF/web.xml

</listener> 아래쪽에 UTF-8 코딩 추가

    <filter>

        <filter-name>encodingFilter</filter-name>

        <filter-class>org.springframework.web.filter.CharacterEncodingFilter

        </filter-class>

        <init-param>

            <param-name>encoding</param-name>

            <param-value>UTF-8</param-value>

        </init-param>

    </filter>


    <filter-mapping>

        <filter-name>encodingFilter</filter-name>

        <url-pattern>/*</url-pattern>

    </filter-mapping>


mapping 주소 설정 - /로 설정된 부분을 *.do 로 변경(이부분은 변경하지 않고 /로 놓고 사용해도 무방하다.

    <servlet-mapping>

        <servlet-name>appServlet</servlet-name>

        <url-pattern>/</url-pattern>

    </servlet-mapping>


6) mybatis 설정

 프로젝트명/src/main/webapp/WEB-INF/ 폴더에 mybatis 폴더 생성

 config.xml 파일 생성

<?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="kr.ms.model.MemberVO" alias="MemberVO"/>

    </typeAliases>

</configuration>

추가적으로 VO 파일들이 생길 때마다 

<typeAlias type="kr.ms.model.MemberVO" alias="MemberVO"/>

이부분을 수정해서 계속 추가 해주면 된다.


 db.properties 파일 생성

 각 db 환경에 맞는 내용을 넣어준다.

driver=oracle.jdbc.driver.OracleDriver

url=jdbc:oracle:thin:@127.0.0.1:1521:XE

username=hr

password=hr


프로젝트명/패키지(src/main/java)/ 패지에서 kr.ms.mybatis 패키지 생성

 매핑파일 생성

  - MemberMapper.xml 등등(이부분에는 controller에서 사용할 qeury 구문이 들어간다.

<mapper namespace="kr.ms.mybatis.MemberMapper"> 이부분에서 자신의 패키지에 맞는 설정이 필요하다.

 또 본인의 데이터 베이스에 맞는 설정으로 모두 변경해 주어서 저장을 해주면 된다.

<?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="kr.ms.mybatis.MemberMapper">

    <select id="memberList" resultType="MemberVO">

        SELECT * FROM ms_member 

        ORDER BY msm_no DESC

    </select>

    <insert id="memberInsert" parameterType="MemberVO">

         INSERT INTO ms_member 

         VALUES 

         (msm_no_seq.nextval, #{msm_id}, #{msm_password}, #{msm_name}, #{msm_phone}, #{msm_email}, #{msm_level_no}, #{msm_point}, #{msm_car}, #{msm_denied})

    </insert>

    <delete id="memberDelete" parameterType="Integer">

         DELETE FROM ms_member 

         WHERE 

         num=#{msm_no}

    </delete>

    <select id="memberContent" resultType="MemberVO" parameterType="Integer">

        SELECT * FROM ms_member 

        WHERE

        num=#{msm_no}

    </select>

    <update id="memberUpdate" parameterType="MemberVO">

         UPDATE ms_member 

         SET 

         msm_name=#{msm_name}, msm_phone=#{msm_phone}, msm_email=#{msm_email}

         WHERE 

         msm_no=#{msm_no}

    </update>

</mapper>


7)  root-context.xml, servlet-context.xml 설정

 프로젝트명/src/main/webapp/WEB-INF/spring/root-context.xml

 아래 내용을 붙여넣기 하고 kr.smhrd.model 이부분이 패키지 이므로

본인의 model 패키지에 맞게 수정

<?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:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd 

    http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

<!-- Root Context: defines shared resources visible to all other web components -->

    <bean id="SqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">

        <property name="dataSource" ref="dataSource" />

        <property name="configLocation" value="/WEB-INF/mybatis/config.xml"/>

        <property name="mapperLocations" value="classpath:kr/ms/mybatis/*.xml" />

    </bean>


    <bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">

        <property name="driverClass" value="${driver}"  />

        <property name="url" value="${url}" />

        <property name="username" value="${username}" />

        <property name="password" value="${password}" />

    </bean>

    

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >

        <property name="locations" value="/WEB-INF/mybatis/db.properties" />

    </bean>

    

    <context:component-scan base-package="kr.ms.model" />

</beans>


프로젝트명/src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml

본인의 설정에 맞게 되었는지 확인

<?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 https://www.springframework.org/schema/mvc/spring-mvc.xsd

http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context https://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="kr.ms.smartcar" />

</beans:beans>




추천0

비추천 0

댓글목록

등록된 댓글이 없습니다.

전체 24건 1 페이지

이미지 목록

게시물 검색
Copyright © 즐거운 코딩 생활 ( funyphp ). All rights reserved.
PC 버전으로 보기