Service Method and Returning Objects to screen - TheOpenCloudEngine/metaworks GitHub Wiki

Service Method λ₯Ό ν†΅ν•œ μ•‘μ…˜ μ‹€ν–‰

	@ServiceMethod(callByContent=true)
	public Object login(){
            System.out.println("User clicked login");
        }

Return 을 ν†΅ν•œ ν™”λ©΄ 좜λ ₯

λ©”νƒ€μ›μŠ€μ˜ λͺ¨λΈ 쀑심 ν”„λ‘œκ·Έλž˜λ° 기법은 객체λ₯Ό λ¦¬ν„΄ν•¨μœΌλ‘œμ„œ ν™”λ©΄ 좜λ ₯을 μœ λ„ν•œλ‹€. λ‹€μŒμ€ μ–΄λ– ν•œ μ•‘μ…˜μ— 따라 ν™”λ©΄μ˜ μ „ν™˜μ„ ν•˜λŠ” μ˜ˆμ œμ΄λ‹€.

예제

μ•„λž˜ μ˜ˆμ œλŠ” ServiceMethod 의 κ°„λ‹¨ν•œ μ‚¬μš© μ˜ˆμ œλ‘œμ„œ, Login.java λŠ” Main.java λ₯Ό λ¦¬ν„΄ν•¨μœΌλ‘œμ¨ 메인 ν™”λ©΄μœΌλ‘œμ˜ μ „ν™˜μ„ κ°€λŠ₯ν•˜κ²Œ ν•œλ‹€.

μ‹€ν–‰ λ™μ˜μƒ

Login.java

import org.metaworks.annotation.ServiceMethod;


public class Login {

	String name;
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
	String message;
		public String getMessage() {
			return message;
		}
		public void setMessage(String message) {
			this.message = message;
		}
	String password;
		public String getPassword() {
			return password;
		}
		public void setPassword(String password) {
			this.password = password;
		}
		
		
	@ServiceMethod(callByContent=true)
	public Object login(){
		
		if(getName().equals(getPassword()))
		
			return new Main();
		
		else{

			setMessage("μ•”ν˜Έκ°€ ν‹€λ ΈμŠ΅λ‹ˆλ‹€.");

			return this;
//			throw new RuntimeException("μ•”ν˜Έκ°€ ν‹€λ ΈμŠ΅λ‹ˆλ‹€.");
		}
	}
		
	

}

Main.java

import org.metaworks.annotation.ServiceMethod;


public class Main {
	
	public Main(){}
	
	@ServiceMethod(needToConfirm=true)
	public Login logout(){
		return new Login();
	}

}

μΊμŠ€νŒ…μ— μ˜ν•œ 리턴 객체의 μœ„μΉ˜ 작기

λ©”νƒ€μ›μŠ€μ˜ DDD κ°œλ…μ˜ ν™•μž₯으둜 λ¦¬ν„΄λ˜λŠ” 객체의 μœ„μΉ˜λŠ” 객체 상속 ꡬ쑰상 κ°€μž₯ μ μ ˆν•œ μœ„μΉ˜λ₯Ό μ°Ύμ•„κ°€κ²Œ λœλ‹€. λ‹€μŒμ˜ 예제λ₯Ό 보자.

Main.java

package org.metaworks.example.navigation;

public class Main {

    Menu menu;
		public Menu getMenu() {
			return menu;
		}
		public void setMenu(Menu menu) {
			this.menu = menu;
		}

	Content content;
		public Content getContent() {
			return content;
		}
		public void setContent(Content content) {
			this.content = content;
		}
	
	protected Main(){
		setMenu(new Menu());
		setContent(new Content());
	}

}

Menu.java

package org.metaworks.example.navigation;

public class Menu {
    
	protected Menu(){
		Selection selection1 = new Selection();
		selection1.setContentClassName("AContent");

		Selection selection2 = new Selection();
		selection2.setContentClassName("BContent");
		
		setSelections(new Selection[]{selection1, selection2});
	}
	
	Selection[] selections;
		public Selection[] getSelections() {
			return selections;
		}
		public void setSelections(Selection[] selections) {
			this.selections = selections;
		}

}

Selection.java

package org.metaworks.example.navigation;

import org.metaworks.annotation.Id;
import org.metaworks.annotation.ServiceMethod;

public class Selection {
    String contentClassName;
	@Id
		public String getContentClassName() {
			return contentClassName;
		}
		public void setContentClassName(String contentClassName) {
			this.contentClassName = contentClassName;
		}
		
	@ServiceMethod
	public Content select() throws InstantiationException, IllegalAccessException, ClassNotFoundException{
		return (Content) Class.forName("org.metaworks.example.navigation." + getContentClassName()).newInstance(); 
	}
	
}

AContent.java / BContent.java

package org.metaworks.example.navigation;

public class AContent extends Content{
    
	protected AContent(){
		setTitle("A content");
		setHtml("Hello, this content is A");
	}
}





package org.metaworks.example.navigation;

public class BContent extends Content{
    
	protected BContent(){
		setTitle("B content");
		setHtml("Hello, this content is B");
	}
}

μ‹€ν–‰ λ™μ˜μƒ

EJS λ₯Ό μž…νžŒ μ‹€ν–‰ λ™μ˜μƒ