首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

jeecms的自定义的伪静态的兑现分析

2012-10-07 
jeecms的自定义的伪静态的实现分析????????? jeecms是一个ssh+freemarker的优秀的cms系统,最近下载了一个

jeecms的自定义的伪静态的实现分析

????????? jeecms是一个ssh+freemarker的优秀的cms系统,最近下载了一个版本装了一下,很不错,但是除了那一些自定义的freemarker标签外,还有些头大的事,就是很难查清楚它的自定义的伪静态的实现, htm是怎样对映到action上去的,最近花了几天看了一下,有点发现,记录下来.

???????

????????? 如下图, 点击一个article的标题,会弹出一个像是静态的url, 这个显然是要提交到一个action上的,但是怎么实现的呢?

????????

http://localhost:8080/JEECMS/movie/38.htm

?
jeecms的自定义的伪静态的兑现分析
?

??? 查了一下每个aticle的静态页面,它的url都是有一定规则的,channel名加上article的id号,如果要分页还要加上一些附加的_*之类的url.

?

以下分析一下其大体的实现流程:

????首先看一下它的struts2的全局配置:

struts.action.extension=do,htm,jspa,jspx,php,asp,aspxstruts.objectFactory=springstruts.enable.DynamicMethodInvocation=falsestruts.devMode=truestruts.locale=zh_CNstruts.i18n.encoding=GBKstruts.ui.theme=simple#struts.custom.i18n.resources=i18nstruts.ui.templateDir=/WEB-INF/template#struts.multipart.saveDir=temp/struts.multipart.maxSize=8388608struts.enable.SlashesInActionNames=true

?第一个配置说明surfix 以do, html, jspa,jspx,php,asp,aspx结尾的url都被视为struts2的action处理,那么显然这个

htm结尾的是要当做action处理的了。

?

下面看一下这个url是如何对应到一个action的.

在com.jeecms.cms的下面有一个struts-core-front.xml的配置文件,定义如下:

这个<action name="**" name="code"><?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN""http://struts.apache.org/dtds/struts-2.1.dtd"><struts><!--动态系统--><package name="core.front.dynamic" namespace="" extends="empty-default"><action name="**" namespace="/front/attachment" extends="inde-default"><action name="Com_*" method="{1}" name="code">package com.jeecms.common.struts2.interceptor;import javax.servlet.http.HttpServletRequest;import org.apache.commons.lang.StringUtils;import org.apache.commons.lang.math.NumberUtils;import org.apache.struts2.StrutsStatics;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;/** * URL地址拦截器 * * 针对页面伪静态地址,需要传入多个参数 * * @author liufang * */public class UrlInterceptor extends MethodFilterInterceptor {private static final long serialVersionUID = 1L;@Overrideprotected String doIntercept(ActionInvocation invocation) throws Exception {Object action = invocation.getAction();//check the name space.String namespace = invocation.getProxy().getNamespace(); String actionname =invocation.getProxy().getActionName();if (!StringUtils.isBlank(namespace)) { if ("/".equals(namespace.trim())) { } else { namespace += "/"; } } System.out.println("namespace is "+namespace+"\n actionname is"+actionname);//=============ActionContext ctx = invocation.getInvocationContext();HttpServletRequest req = (HttpServletRequest) ctx.get(StrutsStatics.HTTP_REQUEST);if (action instanceof UrlAware) {System.out.println("UrlInterceptor active..");UrlAware aware = (UrlAware) action;String url = req.getRequestURL().toString();System.out.println("request url is:"+url);int pointIndex = url.indexOf('.', url.lastIndexOf('/'));if (pointIndex == -1) {url += "index.do";pointIndex = url.indexOf('.', url.lastIndexOf('/'));}String paramStr = req.getQueryString();if (paramStr != null && !paramStr.trim().equals("")) {url += "?" + paramStr;}aware.setWholeUrl(url);int lineIndex = url.indexOf('_', url.lastIndexOf('/'));int mlineIndex = url.indexOf('-', url.lastIndexOf('/'));// 前路径结束点int preIndex = 0;if (lineIndex != -1) {// 有下划线(有分页)preIndex = lineIndex;} else if (mlineIndex != -1) {// 有中划线(有定制参数)preIndex = mlineIndex;} else {// 什么都没有preIndex = pointIndex;}aware.setPageLink(url.substring(0, preIndex));// 后路径开始点int suffixIndex = 0;if (mlineIndex != -1) {// 有中划线suffixIndex = mlineIndex;} else {suffixIndex = pointIndex;}aware.setPageSuffix(url.substring(suffixIndex));// 取页数和附加参数if (preIndex == suffixIndex) {// 没有分页,为第一页。aware.setPageNo(1);} else {// 去掉下划线"_"。String page = url.substring(preIndex + 1, suffixIndex);aware.setPageNo(NumberUtils.toInt(page, 1));}// 路径参数String pathParm = ctx.getName();lineIndex = pathParm.indexOf("_");mlineIndex = pathParm.indexOf("-");if (lineIndex != -1) {pathParm = pathParm.substring(0, lineIndex);} else if (mlineIndex != -1) {pathParm = pathParm.substring(0, mlineIndex);}aware.setPathParams(pathParm.split("/"));// 附加参数if (mlineIndex != -1) {String otherParam = ctx.getName().substring(mlineIndex + 1);aware.setOtherParams(otherParam.split("-"));}//System.out.println(TypeUtil.typeToString("UrlAware--:\n",aware));}//----------test the url interceptor------//else{System.out.println("UrlInterceptor ignored..");}return invocation.invoke();}}

?

?

??
jeecms的自定义的伪静态的兑现分析
?

?

package com.jeecms.cms.action;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Scope;import org.springframework.stereotype.Controller;import com.jeecms.common.struts2.interceptor.DomainNameAware;import com.jeecms.common.struts2.interceptor.UrlAware;import com.jeecms.core.Constants;import com.jeecms.core.entity.Website;import com.jeecms.core.manager.WebsiteMng;import com.opensymphony.xwork2.Action;@Scope("prototype")@Controller("core.dynamicSystemAct")public class DynamicSystemAct implements Action, DomainNameAware, UrlAware {public String execute() {System.out.println("comming into DynamicSystemAct");Website web = websiteMng.getWebsite(domainName);if (web == null) {// 检查别名web = websiteMng.getByAlias(domainName);if (web != null) {redirectUrl = web.getWebUrl();return Constants.REDIRECT;} else {return Constants.WEBSITE_NOT_FOUND;}}String sys = web.getCurrentSystem();namespace = "/jeedynamic/" + sys;actionName = "Page";System.out.println("DynamicSystemAct namespace is "+namespace+"\n actionname is"+actionName);return SUCCESS;}private String namespace;private String actionName;private String pageName;private String pathName;private String domainName;private String[] pathParams;private String[] otherParams;private String wholeUrl;private String pageLink;private String pageSuffix;private int pageNo = 1;private String redirectUrl;@Autowiredprivate WebsiteMng websiteMng;public void setDomainName(String domainName) {this.domainName = domainName;}public void setPathParams(String[] pathParams) {this.pathParams = pathParams;}public void setPageNo(int pageNo) {this.pageNo = pageNo;}public void setPageLink(String pageLink) {this.pageLink = pageLink;}public void setPageSuffix(String pageSuffix) {this.pageSuffix = pageSuffix;}public void setWholeUrl(String wholeUrl) {this.wholeUrl = wholeUrl;}public String getNamespace() {return namespace;}public void setNamespace(String namespace) {this.namespace = namespace;}public String getActionName() {return actionName;}public void setActionName(String actionName) {this.actionName = actionName;}public String getPageName() {return pageName;}public void setPageName(String pageName) {this.pageName = pageName;}public String getPathName() {return pathName;}public void setPathName(String pathName) {this.pathName = pathName;}public String getDomainName() {return domainName;}public String[] getPathParams() {return pathParams;}public String getWholeUrl() {return wholeUrl;}public String getPageLink() {return pageLink;}public String getPageSuffix() {return pageSuffix;}public int getPageNo() {return pageNo;}public String getRedirectUrl() {return redirectUrl;}public void setRedirectUrl(String redirectUrl) {this.redirectUrl = redirectUrl;}public String[] getOtherParams() {return otherParams;}public void setOtherParams(String[] otherParams) {this.otherParams = otherParams;}}

?
综合上面的信息,再查page这个action,在发现com.jeecms.cms.struts-font.xml中发现了它的定义.

?

<package name="cms.front.page" namespace="/jeedynamic/jeecms" extends="empty-default"><action name="Page" type="pageCache">${tplPath}</result><interceptor-ref name="exception"/><interceptor-ref name="chain"/></action></package>

? 由此最终指向了一个模板.真是山路十八弯啊.

?

?

1 楼 ningzhisheng 2010-07-26   最近也在学习jeecms,谢谢分享! 2 楼 a123456603 2011-03-02   有没有qq的联系一下 3 楼 wkcgy 2011-04-17   楼主辛苦了!!

热点排行