Restlet2 + Spring3 配置如下,希望能对大家有用
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的BeanNameUrlHandlerMapping,将url映射成bean
对于要使用基于restlet注解(@Get @Put @Post @Delete)配置,一定要直接继承ServerResource
对于路径的匹配模式还需要研究下