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 를 직접 계승 해 야 합 니 다.
경로 의 일치 모델 에 대해 서 는 아직 연구 가 필요 하 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
thymeleaf로 HTML 페이지를 동적으로 만듭니다 (spring + gradle)지난번에는 에서 화면에 HTML을 표시했습니다. 이번에는 화면을 동적으로 움직여보고 싶기 때문에 입력한 문자를 화면에 표시시키고 싶습니다. 초보자의 비망록이므로 이상한 점 등 있으면 지적 받을 수 있으면 기쁩니다! ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.