2013년 11월 20일 수요일

스프링3.X MVC HelloWorld, Spring Framework3.2 강좌[스프링 MVC, Spring MVC Framework] , JAVA Spring

스프링3.X MVC HelloWorld, Spring Framework3.2 강좌[스프링  MVC, Spring MVC Framework]
 
1. 이클립스   File >> New >> Project >> Spring Project >> Spring MVC Project 
    프로젝트이름을 SpringHello라고 하자.

2. topLevelPackage는 com.onjprogramming.helloworld라고 하자.

   마지막 helloworld가 web app의 이름이 된다.
즉 나중에 실행시 브라우저에서 http://localhost:8080/helloworld 라고 하는 것이다.
입력 후 Next를 클릭

3. 이제 이클립스에 SprignHello 라는 프로젝트가 생긴 것을 확인 할 수 있을 것이다.

4. HelloWorldController.java

package com.onjprogramming.helloworld.controller;     
import org.springframework.stereotype.Controller;    
import org.springframework.web.bind.annotation.RequestMapping;    
import org.springframework.web.servlet.ModelAndView;      
  
@Controller
public class HelloWorldController {       
  
    @RequestMapping("/hello")       
    public ModelAndView helloWorld() {              
  
        String message = "Hello, Spring 3.X!";            
        return new ModelAndView("hello", "message", message);        
    }    
}
 
5. /WEB-INF/jsp/hello.jsp
 
<html>   
<head> 
     <title>Spring 3.X MVC Hello World</title> 
 </head> 
 <body> 
     ${message}  
 </body> 
</html>

6. /WEB-INF/web.xml
 
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>springhello</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springhello</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>

7. /WEB-INF/springhello-servlet.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" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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=“com.onjprogramming.helloworld.controller" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
 <property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
 <property name="prefix" value="/WEB-INF/jsp/" />
 <property name="suffix" value=".jsp" />
</bean>
</beans>
 

댓글 없음:

댓글 쓰기