아래 링크의 자료를 참고 하셔도 되고, 핵심 요소들만 간추렸습니다.
orginal src : http://zjhzxhz.com/2014/04/spring-4-mvc-hello-world-tutorial-using-maven/
프로젝트 생성 -> 설정 파일 -> Controller -> JSP순서로 생각하시면 됩니다.
> 사용된 버젼:
-spring 4.0.4.RELEASE
-JDK1.8
-Tomcat 7.0.53
-Maven 3.2.1
-Eclipse Java EE IDE / Eclipse Kepler
> 프로젝트 셋업하기 (절차 )
-Maven Project선택
-Web, org.apache.maven.archetypes, maven-archetype-webapp 선택
-GroupId : com.happystudio / -Artifact id : spring
-Version : 0.0.1-SNAPSHOT 입력 및 선택
<에러발생시>
* ~~ "..HttpServlet" was not found on the Java Build Path 에러는
http://stackoverflow.com/questions/22756153/httpservlet-was-not-found 참조
>>> pom.xml |
< modelVersion >4.0.0</ modelVersion > < groupId >com.happystudio</ groupId > < artifactId >spring</ artifactId > < packaging >war</ packaging > < version >0.0.1-SNAPSHOT</ version > < name >Spring MVC Tutorial</ name > < properties > < spring.version >4.0.4.RELEASE</ spring.version > </ properties > < dependencies > < 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 > < dependency > < groupId >junit</ groupId > < artifactId >junit</ artifactId > < version >4.11</ version > < scope >test</ scope > </ dependency > </ dependencies > < build > < finalName >Spring MVC Tutorial</ finalName > </ build > </ project > |
>>> dispatcher-servlet.xml ( put in webapp/WEB-INF/ ) |
xsi:schemaLocation="http://www.springframework.org/schema/beans < context:component-scan base-package = "com.happystudio.spring.controller" /> < bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver" > < property name = "prefix" > < value >/WEB-INF/views/</ value > </ property > < property name = "suffix" > < value >.jsp</ value > </ property > </ bean > </ beans > |
>>> web.xml |
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee < display-name >Spring MVC Tutorial</ display-name > < servlet > < servlet-name >dispatcher</ servlet-name > < servlet-class > org.springframework.web.servlet.DispatcherServlet </ servlet-class > < load-on-startup >1</ load-on-startup > </ servlet > < servlet-mapping > < servlet-name >dispatcher</ servlet-name > < url-pattern >/</ url-pattern > </ servlet-mapping > < context-param > < param-name >contextConfigLocation</ param-name > < param-value >/WEB-INF/dispatcher-servlet.xml</ param-value > </ context-param > < listener > < listener-class > org.springframework.web.context.ContextLoaderListener </ listener-class > </ listener > </ web-app > |
> Controller만들기 : src/main/java/밑에 com.happystudio.spring.controller 패키지 생성
import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @Controller public class HelloWorldController { @RequestMapping ( "/hello" ) public String hello( @RequestParam (value = "name" , false , defaultValue = "World" ) String name, Model model) { model.addAttribute( "name" , name); return "helloworld" ; } } |
> JSP 만들기 :
<! doctype html> < html lang = "en" > < head > < meta charset = "UTF-8" > < title >Spring MVC Tutorial</ title > </ head > < body > < h1 >Hello : ${name}</ h1 > </ body > </ html > |
> 빌드 :
We can generate a War file and deploy that to a web server to test the application. In eclipse , right click the project and Click Run As –> Maven Install. This will build the project and create a war file in the target folder. In the case of this example the file will be spring.war
> 실행 (테스트) :
You can get an greeting by visiting http://localhost:8080/spring/hello/?name=HAp
'0.일반개발' 카테고리의 다른 글
Exit Code=13 (0) | 2015.06.12 |
---|---|
Log4j 사용하기 ( in MVC controller ) (0) | 2015.06.09 |
HttpServlet not found (0) | 2015.06.05 |
Spring Boot 프로젝트 생성 예제 (0) | 2015.05.13 |
Spring MVC (0) | 2015.04.29 |