1. Difference between stucts1 and struts2
struts1 : Servlet used as Controller , you can visit the Servlet directly, code like if---else if--else
struts2 : Filter used as Controller , you can only visit it passively, though setting the condition like "/*"
2. Imitate the core Filter of Struts2 (StrutsPrepareAndExecuterFilter.java)
version 1.0(like struts1)
package com.cdv.web.filter; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet Filter implementation class StrutsPreparedAndExecuterFilter */ public class StrutsPreparedAndExecuterFilter implements Filter { /** * Default constructor. */ public StrutsPreparedAndExecuterFilter() { // TODO Auto-generated constructor stub } /** * @see Filter#destroy() */ public void destroy() { // TODO Auto-generated method stub } /** * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain) */ public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) resp; String uri = request.getRequestURI(); boolean isDo = uri.endsWith(".do"); if(isDo){ //get the operation String op = uri.substring(uri.lastIndexOf("/"), uri.lastIndexOf(".")); if(op.equals("login")){ request.setAttribute("username", "zhangsan"); }else{ //to do something } }else{ // chain.doFilter(request, response); } } /** * @see Filter#init(FilterConfig) */ public void init(FilterConfig fConfig) throws ServletException { // TODO Auto-generated method stub } }
version 2.0
we need create the interface , for creating different instance aim at different params condition, and use the map instead of if--else
package com.cdv.web.action; import javax.servlet.http.HttpServletRequest; public interface Action { String execute(HttpServletRequest request); }
package com.cdv.web.action; import javax.servlet.http.HttpServletRequest; public class LoginAction implements Action { @Override public String execute(HttpServletRequest request) { String username = request.getParameter("username"); if(username.equals("zhangsan")){ return "ok"; } return null; } }
package com.cdv.web.filter; import java.io.IOException; import java.util.HashMap; import java.util.Map; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.cdv.web.action.Action; import com.cdv.web.action.LoginAction; /** * Servlet Filter implementation class StrutsPreparedAndExecuterFilter */ public class StrutsPreparedAndExecuterFilter2 implements Filter { private Map<String, Action> actionMap = new HashMap<String, Action>(); /** * Default constructor. */ public StrutsPreparedAndExecuterFilter2() { // TODO Auto-generated constructor stub } /** * @see Filter#destroy() */ public void destroy() { // TODO Auto-generated method stub } /** * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain) */ public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) resp; String uri = request.getRequestURI(); boolean isDo = uri.endsWith(".do"); if(isDo){ String op = uri.substring(uri.lastIndexOf("/"), uri.lastIndexOf(".")); Action instance = actionMap.get(op); String result = instance.execute(request); if(result.equals("ok")){ request.setAttribute("user", "zhangsan"); } }else{ chain.doFilter(request, response); } } /** * @see Filter#init(FilterConfig) */ public void init(FilterConfig fConfig) throws ServletException { actionMap.put("login", new LoginAction()); //actionMap.put("logout", new LogoutAction()); //... } }
version 3.0
put the value of instance into xml file , use dom4j to parse it, and use reflect technology to create instance dynamic.
struts.xml
<?xml version="1.0" encoding="UTF-8"?> <package name="mypackage"> <action name="loginAction" class="com.cdv.web.action.LoginAction"></action> <action name="logoutAction" class="com.cdv.web.action.LogoutAction"></action> </package>
package com.cdv.web.filter; import java.io.IOException; import java.util.HashMap; import java.util.Map; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.cdv.web.action.Action; import com.cdv.web.action.LoginAction; /** * Servlet Filter implementation class StrutsPreparedAndExecuterFilter */ public class StrutsPreparedAndExecuterFilter3 implements Filter { private Map<String, Action> actionMap = new HashMap<String, Action>(); /** * Default constructor. */ public StrutsPreparedAndExecuterFilter3() { // TODO Auto-generated constructor stub } /** * @see Filter#destroy() */ public void destroy() { // TODO Auto-generated method stub } /** * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain) */ public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) resp; String uri = request.getRequestURI(); boolean isDo = uri.endsWith(".do"); if(isDo){ String op = uri.substring(uri.lastIndexOf("/"), uri.lastIndexOf(".")); Action instance = actionMap.get(op); String result = instance.execute(request); if(result.equals("ok")){ request.setAttribute("user", "zhangsan"); } }else{ chain.doFilter(request, response); } } /** * @see Filter#init(FilterConfig) */ public void init(FilterConfig fConfig) throws ServletException { //進行dom4j解析,將struts.xml中的name 和 value取出, //並利用反射將class的值轉化為對象 /* Document document = Dom4jUtil.getDocument(); List<Node> list = document.selectNodes("//action"); for(Node node : list){ //遍歷action節點,取出值 String key = node.valueOf("@name"); String value = node.valueOf("@class"); actionMap.put(key, (Action) Class.forName(value).newInstance()); }*/ } }