1. Spring MVC Project 생성 (* gradle로 프로젝트 생성 하였다면 해당 부분은 패스 하셔도 됩니다.)
1 2 3 | dependencies { org.springframework:spring-webmvc:3.2.2.RELEASE //추가 } |
* 해당 폴더를 참고 하시고 취향대로 생성 하시면 됩니다.
3-1. WebConfig.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 97 98 99 100 101 102 | package com.spring.www.config; import org.springframework.context.MessageSource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.support.ReloadableResourceBundleMessageSource; import org.springframework.util.StringUtils; import org.springframework.web.servlet.LocaleResolver; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.i18n.CookieLocaleResolver; import org.springframework.web.servlet.i18n.LocaleChangeInterceptor; import org.springframework.web.servlet.view.InternalResourceViewResolver; import org.springframework.web.servlet.view.JstlView; @Configuration @EnableWebMvc @ComponentScan(basePackages = { "com.spring.www.controller" }) public class WebConfig extends WebMvcConfigurerAdapter { /** * CSS / JavaScript / Image 등의 정적 리소스를 처리해주는 핸들러를 등록 */ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/resources/**").addResourceLocations("/resources/"); } /** * 인터셉터 (요청을 가로챔) */ @Override public void addInterceptors(InterceptorRegistry registry) { LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor(); localeChangeInterceptor.setParamName("lang"); registry.addInterceptor(localeChangeInterceptor); } /** * locale resolver * * @return */ @Bean public LocaleResolver localeResolver() { CookieLocaleResolver cookieLocaleResolver = new CookieLocaleResolver(); cookieLocaleResolver.setDefaultLocale(StringUtils.parseLocaleString("en")); return cookieLocaleResolver; } /** * JSP를 뷰로 사용하는 뷰 리졸버 등록 */ @Bean public ViewResolver viewResolver() { InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); viewResolver.setViewClass(JstlView.class); viewResolver.setPrefix("/WEB-INF/views/"); viewResolver.setSuffix(".jsp"); return viewResolver; } @Override public void addViewControllers(org.springframework.web.servlet.config.annotation.ViewControllerRegistry registry) { // 특별히 controller 를 타지 않아도 되는 뷰만 있는 경우 등록 // ex) 디자인만 입힌 것들. registry.addViewController("/simpleView").setViewName("/simpleView"); // 404 오류가 발생했을때 보여줄 뷰를 등록 // registry.addViewController("/page-not-found").setViewName("errors/404"); } /** * message source 들을 등록함 * * @return */ @Bean public MessageSource messageSource() { ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource(); messageSource.setBasenames("classpath:messages/messages", "classpath:messages/validation"); // if true, the key of the message will be displayed if the key is not // found, instead of throwing a NoSuchMessageException messageSource.setUseCodeAsDefaultMessage(true); messageSource.setDefaultEncoding("UTF-8"); // # -1 : never reload, 0 always reload messageSource.setCacheSeconds(0); return messageSource; } } |
3-2. WebInitializer.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 | package com.spring.www.config; import javax.servlet.Filter; import org.springframework.web.filter.CharacterEncodingFilter; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class<?>[] getRootConfigClasses() { return null; } @Override protected Class<?>[] getServletConfigClasses() { return new Class<?>[] { WebConfig.class }; } @Override protected String[] getServletMappings() { return new String[] { "/" }; } @Override protected Filter[] getServletFilters() { CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter(); characterEncodingFilter.setEncoding("UTF-8"); return new Filter[] { characterEncodingFilter}; } } |
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 | package com.spring.www.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; /** * Handles requests for the application home page. */ @Controller public class HomeController { /** * Simply selects the home view to render by returning its name. */ @RequestMapping(value = "/", method = RequestMethod.GET) public String home(Model model) { String name = "Psyken"; model.addAttribute("name", name ); return "home"; } } |
1 2 3 4 5 6 7 8 9 10 11 | <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page session="false" %> <html> <head> <title>Home</title> </head> <body> <P> My name is ${name}. </P> </body> </html> |
7-2. 왼쪽의 해당 프로젝트를 선택하고 Add로 추가 시킵니다.
7-4. Modules 탭에서 해당 Web Modules를 선택하고 Edit버튼을 누릅니다.
7-5. Path의 기존 경로를 지우고 / 만 남겨 놓고 저장합니다.
7-6. Servers쪽의 Play 버튼을 눌려서 서버를 구동 시킵니다.
7-7. http://localhost:8080/ 눌러서 웹페이지에서 확인 하셔서 해당 페이지가 보인다면 성공.
java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory (0) | 2014.11.04 |
---|---|
4탄. STS 에서 gradle 로 war 배포 (2) | 2014.03.11 |
3탄. MyBatis 설정 (0) | 2014.03.10 |
1탄. gradle 로 이클립스 Spring MVC 프로젝트 생성 (1.11버전 기준) (4) | 2014.03.07 |
Gradle 설치 (1.11버전 기준) (0) | 2014.03.07 |
댓글 영역