프로그래밍/Spring
intellij Spring MVC & Gradle 프로젝트 생성
은바재바
2020. 5. 22. 15:46
1. gradle 선택 -> java, web 체크
2. build.gradle에 소스 추가
1 2 3 4 5 | apply plugin: 'war' providedCompile 'javax.servlet:servlet-api:2.5' compile 'org.springframework:spring-webmvc:5.0.6.RELEASE' runtime 'javax.servlet:jstl:1.1.2' |
3. webapp 아래에 web.xml, dispatcher-servlet.xml, applicationContext.xml 파일 생성 & views폴더 생성 후 index.jsp파일 이동
<web.xml>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
< dispatcher-servlet.xml >
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?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" xmlns:mvc="http://www.springframework.org/schema/mvc" 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.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <mvc:annotation-driven /> <context:component-scan base-package="com.spring.controller" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean> </beans> |
<applicationContext.xml >
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
4. projectStructure에서 artifacts 추가
5. 톰캣 설정
6. index.jsp과 JdbcController.java 생성
< index.jsp >
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <%-- Created by IntelliJ IDEA. User: choej Date: 2020-05-22 Time: 오후 2:48 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>$Title$</title> </head> <body> ${msg} </body> </html> |
< JdbcController.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 | package com.spring.jdbc; import java.sql.*; public class JdbcController { public static void main(String[] args) { try { Connection conn = DriverManager.getConnection( "jdbc:mariadb://localhost:3306/project", "eunbajaeba", "qw0e7r2t2!" ); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery( "select * from t_member" ); while (rs.next()) { System.out.println(rs.getString(1)); } } catch (SQLException e) { e.printStackTrace(); } } } |