【IT168 技术文档】AJF参考struts,webwork实现,
共5个类
AjfAction.java
AjfServlet.java
AjfView.java
AjfConfig.java
AjfUtil.java
主要类2个
AjfServlet.java
AjfAction.java
AjfServlet 相当于 webwork的com.opensymphony.webwork.dispatcher.ServletDispatcher
AjfAction 相当于 webwork,struts中的action
子类继承AjfAction,实现service方法
AjfView获取request中的attribute
a.特点:
1.废弃formbean,与webwork类似
2.http request 封装,动态注入,与webwork类似
3.零配置文件(从简易性上考虑,没有使用配置文件,以后会加入配置文件)
直接提交action类名
如/com.zjuhg2k.mvc.demo.HelloAction.ajf
AjfServlet解析该请求,提取action类名,实例化action类
action的service方法直接返回视图的url 如 /mvc/hello.jsp
b.需要改进加强的地方
1.配置文件
2.异常处理
3.前后台验证支持
c.web.xml配置
< servlet> < servlet-name>ajf< /servlet-name> < servlet-class>com.zjuhg2k.mvc.AjfServlet< /servlet-class> < /servlet> < servlet-mapping> < servlet-name>ajf< /servlet-name> < url-pattern>*.ajf< /url-pattern> < /servlet-mapping>
d.一个简单的例子
1.action类 HelloAction.java
package com.zjuhg2k.mvc.action; import com.zjuhg2k.mvc.*; import com.hoson.*; public class HelloAction extends AjfAction{ public String service(){ java.sql.Timestamp nowTime = StringUtil.getNowTime(); add("time",nowTime);//add data to model return "/mvc/hello.jsp";//return jsp view } }
2.view /mvc/hello.jsp
< %@page contentType="text/html;charset=GBK"%> < %@page import="com.zjuhg2k.mvc.*"%> hello,< %=AjfView.get(request,"name")%> < br>time now is < %=AjfView.get(request,"time")%>
3.访问路径
/com.zjuhg2k.mvc.demo.HelloAction.ajf?name=tiger
e.代码
1.AjfServlet.java
package com.zjuhg2k.mvc; /* * 祝福zjuhg2k的所有XDJM * * AJF agile java framework * * version 0.1 20060617 * * 杜 刚 * dugang * giscat * * qq 53732908 mail giscat@163.com * 浙江东阳三单 1982.10.13 * 简单务实自由快乐 * 头号国产偶像 毛泽东,周恩来,邓小平 * 头号NBA偶像 tim duncan * 头号足球偶像 罗那尔多 */ import java.util.regex.*; import java.io.*; import java.util.*; import java.text.*; import java.net.*; import java.sql.*; import javax.servlet.http.*; import javax.servlet.*; import com.hoson.*; //----------mvc control //--------ajf agile java framework------ public class AjfServlet extends HttpServlet { //private static String ext = ".ajf"; //-------------- public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //response.sendRedirect(request.getContextPath()); //System.out.println("servlet"); String action = null; action=getAction(request); AjfAction ajf = null; try{ ajf = (AjfAction)(Class.forName(action).newInstance()); ajf.httpWrap(request,response); ajf.execute(); }catch(Exception e){ //System.out.println(e); try{ request.setAttribute(AjfConfig.get_ajf_error_msg_key(),e); JspUtil.forward(request,response,AjfConfig.get_ajf_error_page_url()); }catch(Exception e2){throw new IOException(e2+"");} } } //---------------------- public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } //-------------------- public static String getAction(HttpServletRequest req){ String s = null; s = req.getServletPath(); int pos1 = 0; int pos2 = 0; pos1=s.lastIndexOf("/"); pos2=s.lastIndexOf("."); s=s.substring(pos1+1,pos2-pos1); return s; } //----------- }
2.AjfAction.java
package com.zjuhg2k.mvc; import java.util.regex.*; import java.io.*; import java.util.*; import java.text.*; import java.net.*; import java.sql.*; import javax.servlet.http.*; import javax.servlet.*; import com.hoson.*; //----------mvc control //--------ajf agile java framework------ //action extends AjfAction //over write validate and service method /* * 祝福zjuhg2k的所有XDJM * * AJF agile java framework * * version 0.1 20060617 * * * dugang * giscat * qq 53732908 mail giscat@163.com * 浙江东阳三单 1982.10.13 * 简单务实自由快乐 * 头号国产偶像 毛泽东,周恩来,邓小平 * 头号NBA偶像 tim duncan * 头号足球偶像 罗那尔多 */ public class AjfAction { private HttpServletRequest req = null; private ServletResponse res = null; private HttpSession session = null; private String action = null; private String op = null; //Map model = new HashMap();//model ,data container Properties httpProp = null;//request prop Map httpMap = null;//request map //List keyList = new ArrayList(); public void httpWrap(HttpServletRequest req, ServletResponse res)throws Exception{ this.req = req; this.res = res; this.session = req.getSession(); this.httpProp = JspUtil.getReqProp(req); this.op=req.getParameter("op"); } //-------------- public void execute() throws Exception{ String url = null; try{ url = validate(); if(!StringUtil.isempty(url)) { forward(url); } url = service(); //System.out.println(url); if(!StringUtil.isempty(url)) { forward(url); } }catch(Exception e){ add(AjfConfig.get_ajf_error_msg_key(),e); forward(AjfConfig.get_ajf_error_page_url()); } } //------------ //--------logic/biz/service method here----- public String service() throws Exception{ //return view url return null; } //---------- //--------server side validate here--- public String validate() throws Exception{ //return view url, // /common/validate_error.jsp return null; } //------------------ public void forward(String url) throws Exception{ prop2model(); //JspUtil.forward(req,res,url); req.getRequestDispatcher(url).forward(req,res); } //------------ //-----put http Prop into model map //------ private void prop2model(){ Enumeration e = null; String key = null; String v = null; for(e = httpProp.propertyNames(); e.hasMoreElements();) { key = (String)e.nextElement(); v= httpProp.getProperty(key); add(key,v); } } //----------put data into model--- public void add(String key,Object obj){ //keyList.add(key); //model.put(key,obj); req.setAttribute(key,obj); } //---------------- }
3.AjfView.java
package com.zjuhg2k.mvc; import java.util.regex.*; import java.io.*; import java.util.*; import java.text.*; import java.net.*; import java.sql.*; import javax.servlet.http.*; import javax.servlet.*; public class AjfView{ // ----------- public static String get(HttpServletRequest req,String key){ Object v = null; v=req.getAttribute(key); if(v==null){return "";} return v+""; } //------------- }
4. Ajfconfig.java
package com.zjuhg2k.mvc; public class AjfConfig{ private AjfConfig(){} private static String ajf_error_page_url = "/common/ajf_error.jsp"; private static String ajf_error_msg_key = "ajf_error_msg"; public static String get_ajf_error_page_url(){ return ajf_error_page_url; } public static String get_ajf_error_msg_key(){ return ajf_error_msg_key; } }