2013년 7월 22일 월요일

(130722) 21일차 web.xml 외 4개 (Spring을 이용한 form 데이터 넘기기 테스트)

 - /WebContent/WEB-INF/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:jsp="http://java.sun.com/xml/ns/javaee/jsp" 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_3_0.xsd" id="WebApp_ID" version="3.0">
  <servlet>
    <servlet-name>helloworld</servlet-name>
    <servlet-class>
org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
  </servlet>

  <servlet-mapping>

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

</web-app>


 - /WebContent/WEB-INF/helloworld-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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<!-- Controller 작성 -->
<!-- HelloWorld.do가 들어오면 controller 클래스의 HelloWorld 실행 -->
<bean id="helloworld" name="/HelloWorld.do" class="controller.HelloWorld">
</bean>

<!-- Controller 실행 후 결과를 result할 jsp 페이지를 설정 -->
<bean id = "internalResolver"
 class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
 <property name = "viewClass">
  <value>org.springframework.web.servlet.view.JstlView</value>  
 </property>
 <property name="prefix">
  <value>/jsp/</value>
 </property>
 <property name="suffix">
  <value>.jsp</value>
 </property>
</bean>
</beans>


 - WebContent/jsp/formdata.jsp 소스

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<!-- HelloWorld.do라는 컨트롤러를 호출하라는 뜻 -->
<form action="../HelloWorld.do" method="post">
전송할 데이터 : <input type="text" name="hello">
<input type="submit" value="전송">
</form>
</body>
</html>


 - /src/controller/HelloWorld.java 소스
package controller;


import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.servlet.ModelAndView;

public class HelloWorld {

@RequestMapping("/HelloWorld") // RequestMapping이 있어야 호출이 됨
// RequestMapping에 의해서 사용자가 요청을 실행하는 순간 실행되는 CallBack 메소드
public ModelAndView printHello(String hello)
// model과 view를 동시에 지정 가능한 객체 선언
ModelAndView mav = new ModelAndView();

String data = hello;

// 결과를 보여줄 jsp 파일명을 설정 (helloworld.xml의 suffix에 .jsp를 설정해놨으므로 붙여주지 않아도 됨)
mav.setViewName("ResultHelloWorld");
// jsp로 떠넘길 데이터를 부착
mav.addObject("hellodata", data);

return mav; // bean 클래스에 선언한 ViewResolver 클래스를 호출
}
}


 - /WebContent/jsp/ResultHelloWorld.html 소스
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<center>
<h2> 결과 </h2>
<!-- HelloWorld.java에서 받은 값 출력 -->
${hellodata}
</center>
</body>
</html>


 - 결과