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);
}
}
}