이번에도 기본적인 MVC모델로 Velocity toolbox를 이용하여 Person 모델에서 이름과 전화번호를 받아 벨로시티 파일에 뿌련주는 코드를 작성해 보았다.
* Model
package net.telbook.model;
public class Person{
String name;
String tel;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
}
String name;
String tel;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
}
- Model 똑같다..
* Controller
package net.telbook.controller;
import java.io.IOException;
import java.util.ArrayList;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.telbook.model.Person;
public class TelBookController extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("euc-kr");
try {
ArrayList<Person> personList = new ArrayList<Person>();
Person lenny = new Person();
lenny.setName("lenny");
lenny.setTel("6603");
personList.add(lenny);
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("euc-kr");
try {
ArrayList<Person> personList = new ArrayList<Person>();
Person lenny = new Person();
lenny.setName("lenny");
lenny.setTel("6603");
personList.add(lenny);
Person cafeteria = new Person();
cafeteria.setName("Cafeteria");
cafeteria.setTel("0804");
personList.add(cafeteria);
request.setAttribute("personList", personList);
RequestDispatcher rd = request.getRequestDispatcher("telbook.vm");
rd.forward(request, response);
cafeteria.setName("Cafeteria");
cafeteria.setTel("0804");
personList.add(cafeteria);
request.setAttribute("personList", personList);
RequestDispatcher rd = request.getRequestDispatcher("telbook.vm");
rd.forward(request, response);
} catch (Exception e) {
e.printStackTrace();
}
}
}
e.printStackTrace();
}
}
}
- controller.. toolbox를 사용하고 제일 많이 변한 부분이다.
여기에서 벨로시티를 설정한 부분은 없다.
단 Attribute를 세팅하고 telbook.vm으로 Dispacher해줬을 뿐..
* View
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>TelBook View</title>
</head>
<body>
#macro(tablerows $name $tel)
<tr><td>$name</td><td>$tel</td></tr>
#end
<html>
<head>
<title>TelBook View</title>
</head>
<body>
#macro(tablerows $name $tel)
<tr><td>$name</td><td>$tel</td></tr>
#end
<h2>TelBook</h2>
$date.get('yyyy년 m월 d일')
<table border='1'>
#tablerows("이름", "전화번호")
#foreach($person in $personList)
#tablerows($person.name, $person.tel)
#end
</table>
</body>
</html>
$date.get('yyyy년 m월 d일')
<table border='1'>
#tablerows("이름", "전화번호")
#foreach($person in $personList)
#tablerows($person.name, $person.tel)
#end
</table>
</body>
</html>
이전 부분과 달라진 것은 $date.get('yyyy년 m월 d일') 하나 - 이건 toolbox에서 설정해 준 걸로 가져다 쓰면 되는 것이다
* 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">
<display-name>TelBook</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>TelBookController</servlet-name>
<servlet-class>net.telbook.controller.TelBookController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TelBookController</servlet-name>
<url-pattern>/telbook.do</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Velocity</servlet-name>
<servlet-class>org.apache.velocity.tools.view.servlet.VelocityViewServlet</servlet-class>
<init-param>
<param-name>org.apache.velocity.toolbox</param-name>
<param-value>/WEB-INF/toolbox.xml</param-value>
</init-param>
<init-param>
<param-name>org.apache.velocity.velocity.properties</param-name>
<param-value>/WEB-INF/velocity.properties</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Velocity</servlet-name>
<url-pattern>*.vm</url-pattern>
</servlet-mapping>
</web-app>
<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">
<display-name>TelBook</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>TelBookController</servlet-name>
<servlet-class>net.telbook.controller.TelBookController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TelBookController</servlet-name>
<url-pattern>/telbook.do</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Velocity</servlet-name>
<servlet-class>org.apache.velocity.tools.view.servlet.VelocityViewServlet</servlet-class>
<init-param>
<param-name>org.apache.velocity.toolbox</param-name>
<param-value>/WEB-INF/toolbox.xml</param-value>
</init-param>
<init-param>
<param-name>org.apache.velocity.velocity.properties</param-name>
<param-value>/WEB-INF/velocity.properties</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Velocity</servlet-name>
<url-pattern>*.vm</url-pattern>
</servlet-mapping>
</web-app>
web.xml 새로운 것이 있나?
내가 만들어 놓지도 않은 서블릿이 하나 있구나.. Velocity 이건 VelocityViewServlet을 직접 가져다 쓴다.
혹시나 해서 소스를 까본다면 HttpServlet을 상속받고 온갖 doGet과 doPost 를 구현해 놓으며 (사실 안에는 doRequest(request, response) 한줄밖에 없지만..) VelocityContext를 생성하는 코드가 보일것이다.
* velocity.properties
runtime.log = velocity_telbook.log
resource.loader = file
file.resource.loader.class = org.apache.velocity.tools.view.servlet.WebappLoader
file.resource.loader.cache = false
file.resource.loader.modificationCheckInterval = 0
file.resource.loader.path=/WEB-INF/view/
resource.loader = file
file.resource.loader.class = org.apache.velocity.tools.view.servlet.WebappLoader
file.resource.loader.cache = false
file.resource.loader.modificationCheckInterval = 0
file.resource.loader.path=/WEB-INF/view/
input.encoding = EUC-KR
output.encoding = EUC-KR
output.encoding = EUC-KR
velocity.properties
다른 것들은 이전에 써 놨으니..
file.resource.loader.class = org.apache.velocity.tools.view.servlet.WebappLoader:
이 부분이 바뀌었다. 이걸 쓰면
file.resource.loader.path 에 상대경로 설정이 가능하다는..
* toolbox.xml
<?xml version="1.0"?>
<toolbox>
<tool>
<key>date</key>
<scope>application</scope>
<class>org.apache.velocity.tools.generic.DateTool</class>
</tool>
<tool>
<key>math</key>
<scope>application</scope>
<class>org.apache.velocity.tools.generic.MathTool</class>
</tool>
<tool>
<key>number</key>
<scope>application</scope>
<class>org.apache.velocity.tools.generic.NumberTool</class>
</tool>
<tool>
<key>render</key>
<scope>application</scope>
<class>org.apache.velocity.tools.generic.RenderTool</class>
</tool>
</toolbox>
<toolbox>
<tool>
<key>date</key>
<scope>application</scope>
<class>org.apache.velocity.tools.generic.DateTool</class>
</tool>
<tool>
<key>math</key>
<scope>application</scope>
<class>org.apache.velocity.tools.generic.MathTool</class>
</tool>
<tool>
<key>number</key>
<scope>application</scope>
<class>org.apache.velocity.tools.generic.NumberTool</class>
</tool>
<tool>
<key>render</key>
<scope>application</scope>
<class>org.apache.velocity.tools.generic.RenderTool</class>
</tool>
</toolbox>
toolbox 여기에 아까 telbook.vm에서 사용된 $date의 정체가 있다.
date 말고도 여러가지 툴이 있지만.. 다음 기회에 시간이 있다며.. ㅋ
* 실행결과
이번에는 날짜가 찍힌 결과가 나왔다.
그런데? 날짜가 이상하다.. 어디 날짜를 받아온건지 원..
저것도 다음에 찾아보도록~ ㅎ
'Computer' 카테고리의 다른 글
[Ubuntu] 한글 입력 및 인코딩 (0) | 2008.07.30 |
---|---|
Maven (0) | 2008.06.23 |
[Linux] Screen (0) | 2008.05.22 |
Class to Java 역컴파일러 JAD (4) | 2008.05.20 |
Servlet & Velocity 연동 (0) | 2008.04.25 |