Restlet 2 + Spring 3 주해 방식 설정

Restlet 2 + Spring 3 설정 은 다음 과 같 습 니 다. 도움 이 되 셨 으 면 좋 겠 습 니 다.
Restlet: restlet - jee - 2.0.3 주 의 는 jee 버 전 입 니 다.
Spring : 3.0.4.RELEASE
web.xml

<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath*:spring/*.xml</param-value>
	</context-param>

<listener>
		<listener-class>
			org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>

<servlet>
		<servlet-name>restlet</servlet-name>
		<servlet-class> 
			org.restlet.ext.spring.RestletFrameworkServlet
		</servlet-class>
		<init-param>
			<param-name>targetRestletBeanName</param-name>
			<param-value>webApp</param-value>
		</init-param>
	</servlet>
	
	<servlet-mapping>
		<servlet-name>restlet</servlet-name>
		<url-pattern>/resource/*</url-pattern>
	</servlet-mapping>

applicationContext-restlet.xml

	<bean id="webApp" class="org.restlet.Application">
		<property name="root" ref="root" />
	</bean>
	
	<bean name="root" class="org.restlet.ext.spring.SpringBeanRouter"/>
	
	<alias name="indexResource" alias="/index.html"/>
	<alias name="gameTypeResource" alias="/admin/gametypes/{action}"/>

    <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
		<property name="templateLoaderPath" value="/WEB-INF/page/" />
		<property name="freemarkerSettings">
			<props>
				<prop key="template_update_delay">0</prop>
				<prop key="default_encoding">UTF-8</prop>
				<prop key="locale">zh_CN</prop>
			</props>
		</property>
	</bean>

IndexResource.java

@Service
@Scope("prototype")
public class IndexResource extends ServerResource {
	
	@Autowired
	protected FreeMarkerConfigurer freemarkerConfig;
	
	@Autowired
	private GameTypeManager gameTypeManager;
	
	@Get("html")
	public Representation index() {
		List<GameType> gameTypeList = gameTypeManager.findListByRoot(GameTypeAttribute.Standard);
		Set<FlashGame> hotGames = gameTypeManager.getGameTypeByName("    ").getGames();
		final Map<String, Object> dataModel = new TreeMap<String, Object>();
		dataModel.put("gameTypeList", gameTypeList);
		dataModel.put("hotGames", hotGames);
		return new TemplateRepresentation("index.html", freemarkerConfig.getConfiguration(), dataModel, MediaType.TEXT_HTML);
	}

}

GameTypeResource.xml

@Service
@Scope("prototype")
public class GameTypeResource extends ServerResource {
	
	@Autowired
	protected FreeMarkerConfigurer freemarkerConfig;
	
	@Autowired
	private GameTypeManager gameTypeManager;
	
	private String action;
	
	private int pageNum = 1;
	
	@Get("html")
	public Representation represent() {
		if (StringUtils.isNotEmpty((String) getRequest().getAttributes().get("action"))) {
			action = (String) getRequest().getAttributes().get("action");
		}
		if (StringUtils.isNotEmpty((String) getRequest().getAttributes().get("pageNum"))) {
			pageNum = Integer.valueOf(((String) getRequest().getAttributes().get("pageNum")));
		}
		if ("list".equals(action)) {
			return list();
		} else if ("add".equals(action)) {
			return add();
		} else {
			return list();
		}
	}
	
	public Representation list() {
		PageBean pageBean = new PageBean();
		pageBean.setPageNum(pageNum);
		pageBean.setMaxResults(100);
		List<GameType> gameTypes = gameTypeManager.getAll(pageBean);
		final Map<String, Object> dataModel = new TreeMap<String, Object>();
		dataModel.put("gameTypes", gameTypes);
		dataModel.put("pageNum", pageNum);
		return new TemplateRepresentation("admin/gametypes.html", freemarkerConfig.getConfiguration(), dataModel, MediaType.TEXT_HTML);
	}
	
	public Representation add() {
		List<GameType> gameTypes = gameTypeManager.findListByAttribute(GameTypeAttribute.Standard);
		final Map<String, Object> dataModel = new TreeMap<String, Object>();
		dataModel.put("gameTypes", gameTypes);
		return new TemplateRepresentation("admin/gametype_add.html", freemarkerConfig.getConfiguration(), dataModel, MediaType.TEXT_HTML);
	}
	
	@Post
	public Representation acceptRepresentation(Representation entity) throws ResourceException {
		Form form = new Form(entity);
		GameType gameType = new GameType();
		gameType.setName(form.getFirstValue("name"));
		if (!StringUtils.isEmpty(form.getFirstValue("parentId"))) {
			Long parentId = Long.valueOf(form.getFirstValue("parentId"));
			GameType parent = gameTypeManager.get(parentId);
			if (parent != null) {
				gameType.setParent(parent);
			}
		}
		gameType.setGameTypeAttribute(GameTypeAttribute.Standard);
		gameTypeManager.saveByName(gameType);
		return get();
	}
	
	@Put
	public Representation storeRepresentation(Representation entity) throws ResourceException {
		Form form = new Form(entity);
		GameType gameType = new GameType();
		gameType.setName(form.getFirstValue("name"));
		if (!StringUtils.isEmpty(form.getFirstValue("parentId"))) {
			Long parentId = Long.valueOf(form.getFirstValue("parentId"));
			GameType parent = gameTypeManager.get(parentId);
			if (parent != null) {
				gameType.setParent(parent);
			}
		}
		gameType.setGameTypeAttribute(GameTypeAttribute.Standard);
		gameTypeManager.saveByName(gameType);
		return get();
	}
	
	@Delete
	public Representation removeRepresentations(Representation entity) throws ResourceException {
		Form form = new Form(entity);
		if (!StringUtils.isEmpty(form.getFirstValue("id"))) {
			Long id = Long.valueOf(form.getFirstValue("id"));
			gameTypeManager.removeById(id);
		}
		return get();
	}

}

SpringBeanRouter 는 Spring 의 BeanNameUrl Handler Mapping 을 사용 하여 url 을 bean 으로 매 핑 합 니 다.
restlet 기반 주석 사용 하기 (@ Get  @Put @ Post @ Delete) 설정 은 ServerResource 를 직접 계승 해 야 합 니 다.
경로 의 일치 모델 에 대해 서 는 아직 연구 가 필요 하 다.

좋은 웹페이지 즐겨찾기