Case 1. Use Jersey to develop new resources based on iPortal |
Developing a new resource based on iPortal means developing a new resource independent of existed resources in iPortal. This case will demonstrate how to develop a news resource (/iportal/helloworld/news). The sample code used is located at: %SuperMap iPortal_HOME%\samples\code\CustomPortal\Custom_Portal.Case 1—Use Jersey to develop new resources based on iPortal.
Before developing new resources, please build the iPortal customized development environment.
Step 1: Open the database file under the path Custom_Portal\WebContent\WEB-INF\iportaldata\iportal.db in the demo code project, create a new news_info table, and enter the news resource information into the table. The fields in the table are as follows:
Field |
Type |
id |
INTEGER |
newsTitle |
TEXT |
addUser |
TEXT |
addTime |
INTEGER |
modifyUser |
TEXT |
modifyTime |
TEXT |
content |
TEXT |
commentInfo |
TEXT |
newsIcon |
TEXT |
Step 2: Add and implement the DAO layer interface. The NewsInfoDao.java in the example (located in the src> com.supermap.iportal.web.custom.dao package) is the DAO layer definition, and NewsInfoDaoImpl.java (located in the src> com.supermap.iportal.web.custom.dao.impl package) Below) is the specific implementation of the DAO interface.Since the sample project uses the SQLite database by default, the Mapper mapping file corresponding to the DAO interface has been added in the src> com.supermap.iportal.web.database.sqlite package. If you choose other databases, you need to add the corresponding database Package (for example, the corresponding package name of the MySQL database is com.supermap.iportal.web.database.mysql) and create a corresponding Mapper file. The example uses the DAO layer to query the database and obtain news information.
Step 3: Open the IportalServletHandler.java file (located in the src> com.supermap.server.host.webapp.handlers package), the ImportalServletHandler is a route filter, and the code structure is as follows:
/**
* helloworld resource
*
*/
@Override
public void handle(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException{
String path = getPathInfo(req);
if(path.startsWith("/helloworld")){
WebAppRequestDispatcher dispatcher = new WebAppRequestDispatcher("/helloworld", this.iportalServlet);
dispatcher.forward(req,res);
}else{
return;
}
this.setHandleFinished(req);
}
Here, the root entry path of the customized new resource is set to "/helloworld", which can be set according to the needs of users.
Step 4: Open the ImportalHelloWorldResource.Java file (located in the src> com.supermap.iportal.web.custom.rest.resource.impl package). This file is the root entry of the customized new resource. When requesting http://localhost:8091/iportal/helloworld will enter the ImportalHelloWorldResource.Java entry resource. The code structure is as follows:
@Path("/")
public class IportalHelloWorldResource extends IportalPagesJaxrsResourceBase{
public IportalHelloWorldResource(@Context ServletConfig servletConfig) {
super(servletConfig);
}
public IportalHelloWorldResource(){
}
/**
* news resource
* @return
*/
@Path("/news")
public IportalNewsResource getIportalCustomResource(){
return new IportalNewsResource();
}
}
Among them, the resource pointed to by "/news" is the new sub-resource ImportalNewsResource, which will be assigned to the resource ImportalNewsResource when sending a http://localhost:8091/iportal/helloworld/news request, and the default GET request will be assigned to the ImportalNewsResource resource. The code structure of the ImportalNewsResource.java is as follows:
/**
* news resource
*
*/
public class IportalNewsResource extends IportalPagesJaxrsResourceBase {
NewsInfoComponent newsInfoComponent = (NewsInfoComponent) beanFactory.getBean("newsInfoComponent");
/**
* news module information
* @param request
* @return
*/
@GET
@Template(name = "newsInfo.ftl")
public NewsInfo getResource(@Context HttpServletRequest request) {
NewsInfo newsInfo = new NewsInfo();
int id=1;
newsInfo = newsInfoComponent.getNewsInfo(id);
if (newsInfo == null) {
throw new HttpException(Status.CLIENT_ERROR_NOT_FOUND, "news data with id 1 was not found");
}
return newsInfo;
}
}
The explanation of the bold code is as follows:
@GET: Define the request method as a GET request, which supports expressions in HTML, json, rjson, and other formats.
@Template(name = "newsInfo.ftl"): Define the template file as newInfo.ftl, which is located in the Custom_Portal\WebContent\WEB-INF\classes\templates path. Examples of template files are as follows:
<!DOCTYPE html>
<html>
<head>
<#include "iportalCommonMeta_zh_CN.ftl">
<title></title>
<#include "iportalCommonLink_zh_CN.ftl">
</head>
<body>
<#include "iportalHeader_zh_CN.ftl">
<div class="container">
<div class="menu">
News BETA
<div class="text_menu">${resource.content.newsTitle}</div>
</div>
</div>
<#include "iportalFooter_zh_CN.ftl">
<script type="text/javascript" src="${simpleRootPath}/static/jquery/jquery-1.10.2.min.js" ></script>
<script type="text/javascript" src="${simpleRootPath}/static/bootstrap/js/bootstrap.min.js" ></script>
<script type="text/javascript" src="${simpleRootPath}/static/js/underscore-min.1.4.4.js"></script>
<script type="text/javascript" src="${simpleRootPath}/static/js/backbone-min.js"></script>
<script type="text/javascript" src="${simpleRootPath}/static/js/iportalHeader.js"></script>
</body>
</html>
Visit localhost:8091/iportal/helloworld/news.json, the returned json format is as follows: