초리의 블로그

Spring 시작 및 maven 설정 - controller에서 jsp로 값 넘기기까지 본문

카테고리 없음

Spring 시작 및 maven 설정 - controller에서 jsp로 값 넘기기까지

초리84 2016. 2. 26. 00:11

Spring 시작하기

Controller를 통해서 jsp 화면까지 호출


intelliJ -Project 생성



Maven 선택 후, SDK와  archetype 선택



GroupId와 ArtifactId를 정해주자.



Maven 셋팅하는 부분.

전 그냥 디폴트 그대로 사용했어요.



Project Name까지 지어주면 끝



기본적으로 생성되는 파일들



pom.xml 설정

<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 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.kobisApiStudy.chori</groupId>
<artifactId>kobisApiStudy</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>kobisApiStudy Maven Webapp</name>
<url>http://maven.apache.org</url>

<properties>
<spring.version>4.2.2.RELEASE</spring.version>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>

<build>
<finalName>springstudy</finalName>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<mode>war</mode>

<port>8080</port>

<path>/</path>

<charset>UTF-8</charset>

<uriEncoding>UTF-8</uriEncoding>

</configuration>
</plugin>
</plugins>
</build>
</project>

spring 버전은 통일하기 때문에 properties로 관리해요.

maven에서 tomcat을 실행시키기 위해 plugins를 추가했어요.



Maven Reimport

pom.xml 파일에서 오른쪽 버튼을 클릭하면 나옵니다.



web.xml 설정

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/spring/context-*.xml</param-value>
</context-param>

<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/spring/*-servlet.xml</param-value>
</init-param>
</servlet>

<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>



Run Configuration 설정


Run Configuration 창에서 + 버튼을 누르고 Maven을 선택


이름 지어주고, Command line에 입력하면 끝!

Working directory는 알아서 입력 되있음




실행!! console에 이렇게 뜨면 tomcat 실행 성공!



index.jsp



실행해보자!!

성공!!



web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/spring/context-*.xml</param-value>
</context-param>

<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/spring/*-servlet.xml</param-value>
</init-param>
</servlet>

<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>



main 밑에 java 폴더를 생성하고, Source Root로 설정



패키지명과 파일명은 원하는데로~



Controller

package springstudy.startstudy.web;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
* Created by HeeChul on 2016. 2. 25..
*/

@Controller
@RequestMapping("/springstudy/startstudy")
public class StartStudyController {
@RequestMapping("/helloWorld")
public void helloWorld(Model model) {
model.addAttribute("name", "Chori");
model.addAttribute("hello", "Hello Spring Study!!!");
}
}



resources 밑에 spring 폴더를 만들고, core-servlet.xml 생성



core-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:component-scan base-package="springstudy">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository" />
</context:component-scan>

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/" />
<property name="suffix" value=".jsp" />
</bean>

</beans>



controller의 RequestMapping과 동일하게

webapp 밑에 jsp 폴더를  만들고, springstudy 폴더를 만든 후, startstudy 폴더 생성

메소드의 RequestMapping과 동일하게 helloWorld.jsp 생성!




helloWorld.jsp

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Chori Spring Study</title>
</head>
<body>
<h2>Welcom!!</h2>
<h2>
${name}, ${hello}
</h2>
</body>
</html>

controller에서 model의 attribute에 추가했던 name과 hello를 el태그를 사용하여 출력.



서버를 재기동 한뒤,

RequestMapping에 설정한 대로 주소창에 호출!!

성공!!!


설명은 나중에...



Comments