1.2自定义框架总结_使用xml判断对应的Action

1.2版本中分别加入了ActionManager,ActionMapping,ActionMappingManager

首先在ActionServlet中的Init方法中,我们调用ActionMappingManager

中的Init方法解析了xml文件,预先加载了Map中的值

然后在doPost方法中,通过fileName的值,拿到了Map中对应的对象

ActionMapping,通过ActionMapping对象中的actionName属性,

在ActionServlet中我们通过该值创建了对应的Action对象,随后调用

该对象的execute方法,整个流程结束.

ActionManager

public class ActionManager {
	public Action createAction(String actionName){
		try {
			return (Action) Class.forName(actionName).newInstance();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}

 

ActionMapping

实际上就是一个有2个属性的对象,属性分别为name,actionName

分别对应xml文件中的name,class的2个值.

ActionMappingManager

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class ActionMappingManager {
	private Map<String,ActionMapping> actionMappings=new HashMap();
	public void init(String fileName){
		//解析器的创建
		SAXReader reader=new SAXReader();
		InputStream is=this.getClass().getResourceAsStream("/"+fileName);
		try {
			Document doc=reader.read(is);
			//获得根节点
			Element root=doc.getRootElement();
			Element actions=root.element("actions");
			List<Element> list=actions.elements("action");
			for(Element el:list){
				ActionMapping actionMapping=new ActionMapping();
				actionMapping.setName(el.attributeValue("name"));
				actionMapping.setActionName(el.attributeValue("class"));
				actionMappings.put(actionMapping.getName(), actionMapping);
			}
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public Map<String, ActionMapping> getActionMappings() {
		return actionMappings;
	}
	public void setActionMappings(Map<String, ActionMapping> actionMappings) {
		this.actionMappings = actionMappings;
	}
}

 

ActionServlet

package com.sun.framework;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sun.action.LoginAction;

import com.sun.framework.ActionMapping;

public class ActionServlet extends HttpServlet {

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	Map<String, ActionMapping> actionMappings;
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=gbk");
		String path=request.getRequestURI();
		String actionName=path.substring(path.lastIndexOf("/")+1,path.lastIndexOf("."));
		//通过actionName从map拿到对象
		ActionMapping actionMapping=actionMappings.get(actionName);
		//从对象中获得ActionName属性,并且通过create方法获得Action对象
		ActionManager actionManager=new ActionManager();
		Action action=actionManager.createAction(actionMapping.getActionName());
		//执行对应Action对象中的execute方法
		String resultView=action.execute(request, response);
		request.getRequestDispatcher(resultView).forward(request, response);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init(ServletConfig config) throws ServletException {
		//拿到Servlet初始化参数的值
		String fileName=config.getInitParameter("config");
		ActionMappingManager actionMappingManager=new ActionMappingManager();
		//初始化map
		actionMappingManager.init(fileName);
		//拿到map
		actionMappings=actionMappingManager.getActionMappings();
		
	}

}

 

Struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<mystruts>
	<actions>
		<action name="login" class="com.sun.action.LoginAction" />
		<action name="register" class="com.sun.action.RegisterAction" />
	</actions>
</mystruts>

 Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>ActionServlet</servlet-name>
    <servlet-class>com.sun.framework.ActionServlet</servlet-class>
    <init-param>
    	<param-name>config</param-name>
 		<param-value>StrutsMvc.xml</param-value>
    </init-param>
    <load-on-startup>0</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>ActionServlet</servlet-name>
    <url-pattern>*.cc</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 

<init-param>为初始化参数

<load-on-startup>为设定该Servlet为第几个加载,即程序启动时加载.

你可能感兴趣的:(action)