Restlet2 + Spring3 注解方式配置

阅读更多
Restlet2 + Spring3 配置如下,希望能对大家有用
Restlet : restlet-jee-2.0.3 注意是jee版本
Spring : 3.0.4.RELEASE

web.xml

		contextConfigLocation
		classpath*:spring/*.xml
	


		
			org.springframework.web.context.ContextLoaderListener
		
	


		restlet
		 
			org.restlet.ext.spring.RestletFrameworkServlet
		
		
			targetRestletBeanName
			webApp
		
	
	
	
		restlet
		/resource/*
	


applicationContext-restlet.xml
	
		
	
	
	
	
	
	

    
		
		
			
				0
				UTF-8
				zh_CN
			
		
	


IndexResource.java
@Service
@Scope("prototype")
public class IndexResource extends ServerResource {
	
	@Autowired
	protected FreeMarkerConfigurer freemarkerConfig;
	
	@Autowired
	private GameTypeManager gameTypeManager;
	
	@Get("html")
	public Representation index() {
		List gameTypeList = gameTypeManager.findListByRoot(GameTypeAttribute.Standard);
		Set hotGames = gameTypeManager.getGameTypeByName("推荐游戏").getGames();
		final Map dataModel = new TreeMap();
		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 gameTypes = gameTypeManager.getAll(pageBean);
		final Map dataModel = new TreeMap();
		dataModel.put("gameTypes", gameTypes);
		dataModel.put("pageNum", pageNum);
		return new TemplateRepresentation("admin/gametypes.html", freemarkerConfig.getConfiguration(), dataModel, MediaType.TEXT_HTML);
	}
	
	public Representation add() {
		List gameTypes = gameTypeManager.findListByAttribute(GameTypeAttribute.Standard);
		final Map dataModel = new TreeMap();
		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的BeanNameUrlHandlerMapping,将url映射成bean
对于要使用基于restlet注解(@Get  @Put @Post @Delete)配置,一定要直接继承ServerResource
对于路径的匹配模式还需要研究下

你可能感兴趣的:(Spring,游戏,prototype,EXT,XML)