Struts2+Mybatis+Freemarker+Tiles架构灵活的开发框架(一)
本文主要介绍如何架构一个可扩展的系统开发框架。
其中Struts2主要负责控制,Mybatis主要负责数据库操作,FreeMarker负责前台输出,tiles实现前台的分块。
第一,先导入Struts2的包
struts2-core-2.3.7.jar
xwork-core-2.3.7.jar
ognl-3.0.5.jar
javassist-3.11.0.GA.jar
commons-logging-1.1.1.jar
commons-lang3-3.1.jar
commons-io-2.0.1.jar
commons-fileupload-1.2.2.jar
freemarker-2.3.19.jar
第二,配置web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"><!-- Struts2配置 --><filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping></web-app>
第三,配置strus.xml
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"><struts><package name="basic" extends="struts-default"><action name="index" method="index"><result name="success">index.html</result></action></package></struts>
第四,建立一个Action,XXX为自己的名字,lib可以改成项目名称
package com.XXX.lib.action;import com.opensymphony.xwork2.ActionSupport;public class IndexAction extends ActionSupport{private static final long serialVersionUID = 1L;public String index(){return SUCCESS;}}
第四,建立一个HTML。在WebContent下面
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Hello World</title></head><body>Hello World</body></html>
第五,测试一下
输入http://localhost:8080/XXX_S_LIB/index.action出现Hello World,OK啦
特别说明:不明白请在下面跟帖,有错误的地方麻烦指正