struts框架的运行原理:

public class ActionConfig{
 private String path;
 private String type;
 private List<ActionForward> list = new ArrayList();
 get/set...
   }
   public class ActionMapping{
 private Map<key,value> map  = new hashmap();
 public void createActionMapping(List list){
  map.put(list.get(0).getName(),list.get(0));
 }
 public ActonForward findForward(String str){
  return map.get(str);
 }
   }
   public class ActionForward{
 private String name;
 
 private String path;private boolean redirect;
 get/set...
   }

   public class ActionServelt extends HttpServlet{
 public void init(){
 //struts框架在启动时需要解析配置文件(struts-config.xml)
 ServletConfig sc = this.getServletConfig();
 String xmlPath = sc.getInitParameter("config");   //WEB-INF/struts-config.xml
 ServletContext context = this.getServletContext();
 String realPath = context.getRealPath(xmlPath);
 this.parseXml(realPath);

 }
 public void doGet(HttpServletRequest reqeust,HttpServletResponse response)throws IOException,ServletException(
  this.doPost(reqeust,response);
 )
 public void doPost(HttpServletRequest reqeust,HttpServletResponse response)throws IOException,ServletException(
  //处理请求做请求的分发
 ServletContext sc = this.getServletContext();
 List<ActionConfig> config = sc.getAttribute("list");
 String uri = request.getRequestURI().toString(); //   /strutsDemo1/HelloWorldAction.do
 
 //通过用户请求的URI来绝对执行那个Action
 for(ActionConfig c :config){
   if(uri.equals(request.getContextPath()+c.getPath()+".do")){
  String className = c.getType();
  Action obj = Class.forName(className).newInstance();
                ActionForward af =  obj.excute(ActionMapping,ActionForm,HttpservletRequest,HttpservletResponse);
  if(af.getRedirect()){
  response.sendRedirect(af.getPath());
  }else{
  request.getReqe.....(af.getPath).forward(reqeust,reponse);
  }
   }

 }

  
 )
 //解析配置文件
 private void parseXml(String realPath){
 List<ActionConfig> list = new ArrayList<ActionConfig>();
  InputStarem is = new file......(realPath);
          .....
   while(){
  AcitonConfig ac = new ActionConfig();
  ac.setPath(path); //path=/HelloWorldAction
  ac.setType(type); //type= "com.puke.web.actions.HelloWorldAction"
  if(action.eq( "forward")){
   ....
   ActionForward af = new ActionForward();
   af.setName(name);//name=suibian
   af.setPath(path);// /index.jsp
   ac.getList().add(af);
  }
  list.add(ac);
 }
 ServletContext sc = this.getServletContext();
 sc.setAttribute("list",list);
 }

 }
 
   }

转载于:https://my.oschina.net/u/1273168/blog/177266

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值