本篇简单记录一下,本人所用的工程中juzuweb的使用,更详细的参考官网手册是最好的。
工程的依赖库如下面的gradle文件
plugins{
id "war"
id "org.akhikhl.gretty" version '2.0.0'
}
eclipse.wtp.facet{
facet name: 'jst.web', version: '3.0'
facet name: 'jst.java', version: '1.8'
}
def juzu_version = "1.1.3"
description = 'XXX portlet components'
dependencies{
compile group:"org.juzu",name:"juzu-core",version:"1.1.3"
compile group: 'org.juzu', name: 'juzu-plugins-servlet', version: "${juzu_version}"
compile group: 'org.juzu', name: 'juzu-plugins-portlet', version: "${juzu_version}"
compile group: 'org.juzu', name: 'juzu-plugins-less4j', version: "${juzu_version}"
compile group:"org.juzu",name:"juzu-plugins-jackson",version:"${juzu_version}"
compile group: 'javax.portlet',name:'portlet-api',version:'2.0'
compile group:"javax.inject",name:"javax.inject",version:"1"
compile group:"com.google.inject",name:"guice",version:"4.1.0"
providedCompile group:"javax.servlet",name:"javax.servlet-api",version:"3.0.1"
compile project(path: ':config', configuration: 'archives')//本工程中封装的与后台midpoint交互jar包
compile group: 'com.evolveum.midpoint.model', name: 'model-client', version:'3.6'
compile group: 'com.evolveum.midpoint.model', name: 'model-client', version:'3.6'
compile group: 'org.exoplatform.kernel', name: 'exo.kernel.commons', version:'2.6.0'
compile group: 'org.exoplatform.kernel', name: 'exo.kernel.container', version:'2.6.0'
compile group: 'org.exoplatform.core', name: 'exo.core.component.organization.api', version:'2.7.0'
compile files('libs/taobao-sdk-java.jar')//主要使用的是短信发送功能
}
从gradle文件我们可以看出,本工程使用了juzuweb框架、midpoint组件和exo portltet平台。
本篇重点阐述juzuweb框架。
juzuweb是用于前台的编写,整个的目录结构如下图:
上图中我们知道.gtmpl定义了web页面展示的效果(就是写html的地方)
xxcontroller类定义了url的访问方式(get、post)和返回结果(字符串、网页等)
controller类中的 package-info.java主要从全局定义 访问的方式、页面采用的js包等等,我们先从poackage-info.java的源码开始看:
@juzu.Application
@juzu.plugin.servlet.Servlet(value="/about/*",resourceBundle="org.messages")
@Scripts({
@Script(id = "jquery", value = "/js/jquery.min.js"),
@Script(id = "bootstrap", value = "/js/bootstrap.min.js",depends="jquery"),
@Script(id = "layer", value = "/js/layer.min.js",depends="jquery"),
@Script(id = "layer.ext", value = "/js/layer.ext.js",depends="jquery"),
@Script(id = "sweetalert", value = "/js/sweetalert.min.js",depends="jquery"),
@Script(id = "jqgrid", value = "/js/jquery.jqGrid.min.js",depends="jquery"),
@Script(id = "jquery-ui", value = "/js/jquery-ui-1.10.4.min.js",depends="jquery"),
@Script(id = "jqgrid-locale-cn", value = "/js/jqgrid/i18n/grid.locale-cn.js",depends="jquery"),
@Script(id = "jquery.validate", value = "/js/plugins/validate/jquery.validate.min.js",depends="jquery"),
@Script(id = "content", value = "/js/content.min.js",depends={"jquery", "jquery.validate"})
})
@Stylesheets({
@Stylesheet("/css/bootstrap.min.css"),
@Stylesheet("/css/font-awesome.min.css"),
@Stylesheet("/css/animate.min.css"),
@Stylesheet("/css/sweetalert.css"),
@Stylesheet("/css/style.min.css"),
@Stylesheet("/css/ui.jqgrid.css"),
@Stylesheet("/css/site.css"),
@Stylesheet("/css/web-icons.css"),
@Stylesheet("/js/skin/layer.css"),
@Stylesheet("/js/skin/layer.ext.css")
})
@Assets("*")
package org.xxx.about;
import juzu.plugin.asset.Assets;
import juzu.plugin.asset.Stylesheet;
import juzu.plugin.asset.Stylesheets;
import juzu.plugin.asset.Script;
import juzu.plugin.asset.Scripts;
import juzu.plugin.less4j.Less;
从package-info.java中我们可以看出
1、@juzu.Application,@juzu.plugin.servlet.Servlet这两个标注 声明了 servlet访问路径 为 http://ip:port/about/xxx,和工程中使用到的 message文件路径
2、@Scripts、@Script、@Stylesheets、@Stylesheet表明使用的js和css文件地址
3、@Assets表明上面上面的js、css文件会引入到该包about下的所有类
下面我们看看contorller类如何定义
package org.portlet.about;
import juzu.Resource;
import org.apache.commons.beanutils.MethodUtils;
import org.apache.http.HttpRequest;
import org.json.JSONException;
import org.json.JSONObject;
import org.xml.sax.SAXException;
import org.portlet.BaseController;
import com.google.inject.Inject;
import juzu.Action;
import juzu.HttpMethod;
import juzu.MimeType;
import juzu.Path;
import juzu.PropertyMap;
import juzu.PropertyType;
import juzu.Response;
import juzu.Response.Content;
import juzu.Route;
import juzu.View;
import juzu.plugin.ajax.Ajax;
import juzu.template.Template;
public class AboutController extends BaseController {
AboutBaseBean bean;
@Inject
@Path("index.gtmpl")
Template index;
@Inject
@Path("index2.gtmpl")
Template index2;
public AboutController() {
super();
bean = new AboutBaseBean();
try{
bean.setService(this.getExoService());
}catch(Exception e){
e.printStackTrace();
}
}
@View @Route("index")
public Response.Content index() throws ParserConfigurationException, SAXException{
return index.ok().withHeaderTag("<meta charset=\"utf-8\"/>");
}
@View @Route("index2")
public Response.Content index2(String email){
return index2.with().set("result", 'index2').ok().withHeader("content-type","text/html; charset=utf-8");
}
@Resource() @Route("searchUserQuickAction")
@MimeType.JSON
public Response.Content searchUserQuickAction(String search,Integer rows,Integer page,String sidx,String sord){
return Response.ok("[{\"stuNo\":1,\"userName\":\"张三\",\"sex\":\"女\",\"phone\":\"12300000000\",\"email\":\"123@163.com\",\"disableUser\":\"\",\"unlockUser\":\"\",\"resetPassword\":\"\"},{\"stuNo\":1,\"userName\":\"张三\",\"sex\":\"女\",\"phone\":\"12300000000\",\"email\":\"123@163.com\",\"disableUser\":\"\",\"unlockUser\":\"\",\"resetPassword\":\"\"},{\"stuNo\":1,\"userName\":\"张三\",\"sex\":\"女\",\"phone\":\"12300000000\",\"email\":\"123@163.com\",\"disableUser\":\"\",\"unlockUser\":\"\",\"resetPassword\":\"\"}]").withHeader("content-type","application/json; charset=utf-8");
// return Response.ok(bean.findUserByName(search,rows==null?10:rows.intValue(),page==null?1:page.intValue(),sidx,sord));
}
}
从上述的标注可以看出
1、@Inject、@Path(“index.gtmpl”)表明页面gtmpl在java代码中对应的变量
2、@View @Route(“index”)表示url路径为http://ip:port/about/index,在浏览器中输入该url,即可出来index页面
3、@Resource() @Route(“searchUserQuickAction”) @MimeType.JSON,表明访问url http://ip:port/about/searchUserQuickAction 返回的是 json串
4、默认情况下通过url访问的返回字符编码是java工程的编码,可能会出现乱码的现象,解决该问题可以通过.withHeader(“content-type”,”text/html; charset=utf-8”)或
index.ok().withHeaderTag(“”);设置返回页面或字符串的编码来纠正。
5、index2.with().set(“result”, ‘index2’).ok().withHeader(“content-type”,”text/html; charset=utf-8”);我们可以看出我们也可以通过设置变量result让gtmpl页面输出。
.gtmpl页面源码如下:
<h1>html content ${result} </h1>
<div class="gray-bg" style="width:100%;height:100%;">
<div class="jqGrid_wrapper">
<table id="table_list_1"></table>
<div id="pager_list_1"></div>
</div>
</div>
<script>
$(document).ready(function() {
$.jgrid.defaults.styleUI = "Bootstrap";
loadData();
});
function loadData(){
$("#table_list_1").jqGrid({
url: "@{searchUser()}",
datatype: "json",
autowidth: true,
height: 'auto',
shrinkToFit: true,
rowNum: 10,
rowList: [10, 20, 40],
colNames: ["&{index.stuNo}", "&{index.userName}","&{index.sex}", "&{index.phone}", "&{index.email}", "&{index.disable}", "&{index.unlock}", "&{index.resetPassword}"],
colModel: [{
name: "stuNo",
index: "stuNo",
width: 60,
sorttype: "int"
},
{
name: "userName",
index: "userName",
width: 80,
align: "left"
},
{
name: "sex",
index: "sex",
width: 80,
align: "left"
},
{
name: "phone",
index: "phone",
width: 80,
align: "left"
},
{
name: "email",
index: "email",
width: 80,
align: "left"
},
{
name: "disable",
index: "disable",
width: 30,
align: "center",
formatter: function (cellvalue, options, rowObject) {
if(rowObject.disable)
var op = "<img onclick='disableUser("+ options.rowId + ")' title='disableUser' src='../assets/org/portlet/disable.png' style='padding:0px 5px;'>";
else
var op="&{index.disableTip}";
return op;
}
},
{
name: "unlock",
index: "unlock",
width: 30,
align: "center",
formatter: function (cellvalue, options, rowObject) {
if(rowObject.unlock)
var op = "<img onclick='unlockUser("+ options.rowId + ")' title='unlock' src='../assets/org/portlet/unlock.png' style='padding:0px 5px;'>";
else
var op = "&{index.unlockTip}";
return op;
}
},
{
name: "resetPassword",
index: "resetPassword",
width: 30,
align: "center",
formatter: function (cellvalue, options, rowObject) {
var op = "<img onclick='resetPassword("+options.rowId+")' title='resetPwd' src='../assets/org/portlet/resetPassword.png' style='padding:0px 5px;'>";
return op;
}
}],
pager: "#pager_list_1",
caption:"&{index.caption}",
viewrecords: true,
hidegrid: false
});
$(window).bind("resize",
function() {
var width = $(".jqGrid_wrapper").width();
$("#table_list_1").setGridWidth(width);
}
);
}
</script>
从源码中我们可以看到,相应变量的使用方式
1、${result} 表示 使用的是java中的变量
2、&{index.caption}表明使用的是 message中定义的值
message.properties文件如下:
index.caption=学生列表
index.stuNo=学号
index.userName=姓名
....
其余父类源码如下
package org.portlet;
import org.IdentityManagementService;
import org.IdentityManagementServiceFactory;
public class BaseController {
private static IdentityManagementService idmService=null;
public static IdentityManagementService getIdmService(){
if(idmService==null){
idmService = IdentityManagementServiceFactory.getInstance();
}
return idmService;
}
}
package org.portlet;
import java.security.PrivilegedAction;
import org.exoplatform.commons.utils.SecurityHelper;
import org.exoplatform.container.PortalContainer;
import org.exoplatform.container.RootContainer;
import org.exoplatform.container.configuration.ConfigurationManager;
import org.exoplatform.container.configuration.ConfigurationManagerImpl;
import org.exoplatform.container.util.ContainerUtil;
import org.exoplatform.test.MockConfigurationManagerImpl;
import org.exoplatform.test.MockServletContext;
public class IdentityManagementServiceFactory {
public static IdentityManagementService getInstance(){
PortalContainer pcontainer = PortalContainer.getInstance();
if(pcontainer==null){
try
{
RootContainer root = RootContainer.getInstance();
MockServletContext scontext = new MockServletContext(PortalContainer.DEFAULT_PORTAL_CONTAINER_NAME);
pcontainer = new PortalContainer(root, scontext);
final PortalContainer currentPortalContainer = pcontainer;
SecurityHelper.doPrivilegedAction(new PrivilegedAction<Void>()
{
public Void run()
{
PortalContainer.setInstance(currentPortalContainer);
return null;
}
});
final ConfigurationManagerImpl cService = new MockConfigurationManagerImpl(scontext);
cService.addConfiguration(ContainerUtil.getConfigurationURL("conf/portal/configuration.xml"));
cService.addConfiguration(ContainerUtil.getConfigurationURL("conf/portal/test-configuration.xml"));
cService.processRemoveConfiguration();
SecurityHelper.doPrivilegedAction(new PrivilegedAction<Void>()
{
public Void run()
{
currentPortalContainer.registerComponentInstance(ConfigurationManager.class, cService);
//registerComponentInstance(PortalContainer.DEFAULT_PORTAL_CONTAINER_NAME, currentPortalContainer);
currentPortalContainer.start(true);
//onStartupComplete();
return null;
}
});
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
IdentityManagementService service = (IdentityManagementService) pcontainer
.getComponentInstanceOfType(IdentityManagementService.class);
return service;
}
}
package org.portlet;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.xml.sax.SAXException;
import com.evolveum.midpoint.xml.ns._public.common.common_3.OrgType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ResourceType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.RoleType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ServiceType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.UserType;
import com.evolveum.midpoint.xml.ns._public.common.fault_3.FaultMessage;
public interface IdentityManagementService {
public String getServiceDescription();
public UserType getUser(String oid) throws SAXException, IOException, FaultMessage;
}
本文介绍了一个使用JuzuWeb框架的工程案例,包括其在实际项目中的应用、配置及核心功能实现方法。
2261

被折叠的 条评论
为什么被折叠?



