1,架構html
2,技術架構前端
3,應用的啓動過程java
4,用戶的訪問過程(前端爲靜態的html頁面,這裏主要是後端)web
5,高級的地方,經過泛型,抽象了整個的MVC訪問過程,對增刪改查只寫了一份代碼,每個新增的業務只需增長几個類,繼承複用泛型類便可。apache
<!-- 建立遊戲產品 -->後端
<action name="add" class="com.sz7road.web.action.admin.gameproduct.GameProductAction" method="modify">session
<param name="operation">add</param>架構
<result name="input" type="tiles">adminGameProductAdd</result>app
<result name="success" type="redirectAction">list</result>less
<interceptor-ref name="globalInterceptorStack"></interceptor-ref>
</action>
<!-- 編輯遊戲產品 -->
<action name="edit" class="com.sz7road.web.action.admin.gameproduct.GameProductAction" method="modify">
<param name="operation">edit</param>
<result name="success" type="redirectAction">list</result>
<result name="input" type="tiles">adminGameProductEdit</result>
<interceptor-ref name="globalInterceptorStack"></interceptor-ref>
</action>
<!-- 刪除遊戲產品 -->
<action name="del" class="com.sz7road.web.action.admin.gameproduct.GameProductAction" method="modify">
<param name="operation">del</param>
<result name="input" type="redirectAction">list</result>
<result name="success" type="redirectAction">list</result>
<interceptor-ref name="globalInterceptorStack"></interceptor-ref>
</action>
<!-- 查詢遊戲產品 -->
<action name="list" class="com.sz7road.web.action.admin.gameproduct.GameProductAction" method="list">
<param name="operation">list</param>
<result name="input" type="tiles">adminHomePage</result>
<result name="success" type="tiles">adminGameProductList</result>
<interceptor-ref name="globalInterceptorStack"></interceptor-ref>
</action>
public String modify() throws Exception {
EditType editType = EditType.getEnumType(getOperation());
if (null == editType) {
addActionError("operation參數非法");
return ActionSupport.INPUT;
}
boolean result = false;
try {
result = getService().modifyItem(editType, getT(), getDeleteIdArray());
} catch (Exception e) {
e.printStackTrace();
return ActionSupport.ERROR;
}
if (result) {
msg = getText("operation.success");
return ActionSupport.SUCCESS;
} else {
return ActionSupport.ERROR;
}
}
public String list() throws Exception {
paginationResult = new PaginationResult<T>();
dataList = getService().searchByPage(getPager());
final int total = getService().getTotalCount();
paginationResult.setTotal(total);
paginationResult.setResultList(dataList);
requestMap.put(AppConstant.PAGE_MAP_OBJECT, paginationResult);
return ActionSupport.SUCCESS;
}
@Override
public List<T> listByPage(Pager pager) {
boolean result = false;
final Session session = getCurrentSession();
Transaction transaction = session.beginTransaction();
List<T> list = null;
try {
final int pageSize = pager.getPageSize();
final int startPage = (pager.getPageIndex() - 1) * pageSize;
final StringBuffer queryStringBuffer = new StringBuffer(" from " + domainObjectName + " as " + instanceName );
if(!Strings.isNullOrEmpty(pager.getSearchField()))
{
queryStringBuffer.append(" where "+instanceName + "." + pager.getSearchField() + " ").append(pager.getSearchOperation()+ " '").append(pager.getSearchValue()+"'");
}
queryStringBuffer.append(" order by ").append(instanceName + "." + pager.getSortField() + " " + pager.getSortOrder());
// queryStringBuffer.append(" limit ").append(startPage).append(" , " + pageSize);
Query query = session.createQuery(queryStringBuffer.toString());
query.setFirstResult(startPage);
query.setMaxResults(pageSize);
list = query.list();
transaction.commit();
} catch (Exception ex) {
ex.printStackTrace();
transaction.rollback();
} finally {
return list;
}
}
public boolean update(final T entity) {
boolean result = false;
final Session session = getCurrentSession();
Transaction transaction = session.beginTransaction();
try {
if(entity.getId()>0){
session.update(entity);
result=true;
} else{
result = session.save(entity) != null;
}
transaction.commit();
} catch (Exception ex) {
ex.printStackTrace();
transaction.rollback();
} finally {
return result;
}
}
6,tiles和基本展現控件包的使用
指定好定義tiles的配置文件,對象工廠,以及監聽器便可
<context-param>
<param-name>org.apache.tiles.CONTAINER_FACTORY</param-name>
<param-value>org.apache.struts2.tiles.StrutsTilesContainerFactory</param-value>
</context-param>
<context-param>
<param-name>org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG</param-name><param-value>
/WEB-INF/tiles/tiles.xml,
/WEB-INF/tiles/tiles-adminlogin.xml,
/WEB-INF/tiles/tiles-adminPermission.xml,
/WEB-INF/tiles/tiles-adminUser.xml,
/WEB-INF/tiles/tiles-adminEvent.xml,
/WEB-INF/tiles/tiles-adminAgent.xml,
/WEB-INF/tiles/tiles-adminRecruit.xml,
/WEB-INF/tiles/tiles-adminCompanyInfo.xml,
/WEB-INF/tiles/tiles-adminCategory.xml,
/WEB-INF/tiles/tiles-adminAt7road.xml,
/WEB-INF/tiles/tiles-adminGameProduct.xml,
/WEB-INF/tiles/tiles-adminCarousel.xml
</param-value>
</context-param>
<listener>
<listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class>
</listener>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tiles-definitions PUBLIC "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN" "http://tiles.apache.org/dtds/tiles-config_2_0.dtd">
<tiles-definitions>
<!--首頁-->
<definition name="adminDefaultLayout" template="/WEB-INF/web/common/admin/admin_layout.jsp">
<put-attribute name="titleKey" value="PageTitle" type="string"/>
<put-attribute name="header" value="/WEB-INF/web/common/admin/admin_header.jsp"/>
<put-attribute name="leftbar" value="/WEB-INF/web/common/admin/admin_left.jsp"/>
<put-attribute name="content" value=""/>
<put-attribute name="footer" value="/WEB-INF/web/common/admin/admin_footer.jsp"/>
</definition>
</tiles-definitions>模版定製
<tiles-definitions>
<definition name="adminCategoryAdd" extends="adminDefaultLayout">
<put-attribute name="titleKey" value="admin.categoryManage.add"/>
<put-attribute name="content" value="/WEB-INF/web/admin/category/admin_createCategory.jsp" />
</definition>個性實例
主要看展現數據的控件的使用:
<pg:pager items="<%=paginationResult.getTotal()%>"
url='<%=request.getContextPath()+"/gameProduct/list.action"%>'
index="center"
maxPageItems='<%= Integer.parseInt(SystemConfig.getProperty("admin.search.page.size")) %>'
maxIndexPages='<%= Integer.parseInt(SystemConfig.getProperty("admin.search.max.index.pages")) %>'
isOffset="true" export="offset,currentPageNumber=pageNumber" scope="request">
<display:table name="requestScope.paginationResult.resultList" class="tablelist"
defaultsort="0" cellpadding="0" cellspacing="0" style="width:99%"
requestURI="" id="gameProduct">
<%--<display:column property="id" titleKey="admin.id" style="width:30px;"/>--%>
<display:column property="orderNum" titleKey="admin.orderNum"/> //序號
<display:column property="gameName" maxLength="5" titleKey="admin.gameProduct.gameName"/> //遊戲名稱
<display:column property="gameTitle" maxLength="5" titleKey="admin.gameProduct.gameTitle"/> //遊戲標題
<display:column property="gameWebsiteUrl" maxLength="15" titleKey="admin.gameProduct.gameWebsiteUrl"/>//官網地址
<display:column property="gamePlayUrl" maxLength="15" titleKey="admin.gameProduct.gamePlayUrl"/>//選服頁地址
<display:column property="gameHtmlName" maxLength="5" titleKey="admin.gameProduct.gameHtmlName"/>//html名稱
<display:column property="gameImgShowInProduct" maxLength="20" titleKey="admin.gameProduct.gameImgShowInProduct"/>//產品圖片
<display:column property="gameShortDesc" maxLength="5" titleKey="admin.gameProduct.gameShortDesc" escapeXml="true"/>//簡介
<display:column property="gameDetailDesc" maxLength="5" titleKey="admin.gameProduct.gameDetailDesc" escapeXml="true"/>//詳細
<display:column property="createTime" style="width:50px;" titleKey="admin.createTime" format="{0,date,yyyy-MM-dd}"/>//建立日期
<display:column style="width:60px;" titleKey="admin.common.operation" href='${path}/gameProduct/edit.action'>
<s:url id="editUrl" namespace="/gameProduct" action="editPage">
<s:param name="gameProduct.id"><s:property value="#attr.gameProduct.id"/></s:param>
</s:url>
<s:a href="%{editUrl}"><s:text name="admin.common.edit"/></s:a>
<a href="#" onClick="deleteGameProduct('<s:property value="#attr.gameProduct.id"/>','<s:property value="#attr.gameProduct.gameName"/>')"><s:text name="admin.common.delete"/></a>
</display:column>
</display:table>
<jsp:include page="/WEB-INF/web/common/pagination2.jsp" flush="true" />
</pg:pager>
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ page session="false" %>
<%@ taglib uri="/WEB-INF/pager-taglib.tld" prefix="pg" %>
<jsp:useBean id="currentPageNumber" type="java.lang.Integer" scope="request"/>
<jsp:useBean id="totalItems" type="java.lang.Integer" scope="request"/>
<jsp:useBean id="maxPageItems" type="java.lang.Integer" scope="request"/>
<br>
<span class="tabletitle">Result Page: </span>
<span class="content">
<%
int totalItemCount = totalItems.intValue();
int pageSise = maxPageItems.intValue();
if(totalItemCount < 1){
%>
沒有查詢到任何記錄.
<%
}else if(totalItemCount == 1){
%>
1 - <%= totalItemCount %> of <%= totalItemCount %> record.
<%
}else if(totalItemCount <= pageSise){
%>
1 - <%= totalItemCount %> of <%= totalItemCount %> records.
<%
}
%>
<pg:index export="total=itemCount">
<pg:page export="first,last">
<%= first %> - <%= last %> of <%= total %> records.
</pg:page>
<pg:first export="firstPageUrl=pageUrl" unless="current">
<a href="<%= firstPageUrl %>">第一頁</a>
</pg:first>
<pg:prev export="prevPageUrl=pageUrl">
<a href="<%= prevPageUrl %>">前一頁</a>
</pg:prev>
<pg:pages><%
if (pageNumber == currentPageNumber) {
%> <b><%= pageNumber %></b> <%
} else {
%> <a href="<%= pageUrl %>"><%= pageNumber %></a> <%
}
%></pg:pages>
<pg:next export="nextPageUrl=pageUrl">
<a href="<%= nextPageUrl %>">後一頁</a>
</pg:next>
<pg:last export="lastPageUrl=pageUrl" unless="current">
<a href="<%= lastPageUrl %>">末頁</a>
</pg:last>
</pg:index>
</span>
<br>
7,小結
這是一個簡單卻經典的內容管理系統,在後臺管理數據,對數據進行增刪改查,數據管理好以後,使用freemarker生成靜態的html頁面,同時靜態資源分域,加快了站點的顯示速度;
後臺使用泛型和抽象類,大大的減小了重複代碼的數量,引入Guice容器,減小了對象的實例的互相依賴,同時使得程序的結構更加的清晰,hibernate簡化了持久層代碼的編寫,tiles和數據展現控件的使用使得後端的數據展現比較美觀,簡單卻使用的站點發布系統,經過這個項目,我熟悉了struts2,guice,hibernate,tiles技術,惟一一個以爲比較不妥的地方就是雖然大大的減小了重複代碼,可是新增模塊的話須要增長不少的類,若是能夠定義一個公共的動態定位的類來頂替這些大量的只是簡單繼承的類我以爲後端的代碼會更加整潔。