3. 간단한 예제, 두번째

두번째로 간단한 요청을 처리하는 어플리케이션을 작성해 보자.

다음과 같은 소스를 작성한다.

  1. /src/web/controller/SimpleAction.java
  2. package web.controller;
  3.  
  4. import net.kldp.beat.annotation.Result;
  5.  
  6. @Result(name="success", value="/simple.jsp")
  7. public class SimpleAction {
  8. private String name;

  9. private int age;

  10.  

  11. public void setName(String name) {

  12.     this.name = name;

  13. }

  14. public String getName() {

  15.     return name;

    }

  16. public void setAge(int age) {

  17.     this.age = age;

  18. }

  19. public int getAge() {

  20.     return age;

  21. }

  22.    public String execute() {
  23.        return "success"; 
  24.    } 
  25. }

 

그리고 컨트롤러의 결과를 표시할 simple.jsp 페이지를 작성하자.

  1. /WebRoot/simple.jsp

     <%@ page language="java" pageEncoding="UTF-8"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <title>Simple Message!</title>
      </head>
      
      <body>
        Your name is ${name} and age is {$age} years old. <br>
      </body>
    </html>

 

그리고 다음의 URL을 실행하자. http://localhost:8080/myapp/simple.action?name=Kent&age=60 그러면 다음과 같은 결과를 확인할 수 있을 것이다.

Your name is Kent and age is 60 years old.

모든 컨트롤러는 일반적인 setter를 구현함으로써 요청 파라미터를 주입받을 수 있다. 요청 파라미터는 컨트롤러의 프로퍼티 이름에 따라서 주입되며, 숫자나 문자열, 날짜등의 타입은 변환되어 주입된다. 또한 @Result 어노테이션을 작성함으로써 컨트롤러 처리후 디스패치될 뷰의 경로에 대한 설정을 할 수 있다. 컨트롤러의 execute메서드가 리턴하는 문자열에 따라서 Result 어노테이션의 name과 일치되는 뷰로 이동될 것이다.

< 이전 | 다음 >