mvcå¢å¼º
éè¿XML对èªå®ä¹mvcæ¡æ¶è¿è¡å¢å¼º
ä»»å¡ï¼ç¨mvcå®æå åä¹é¤
åå¤ï¼
导4个jarå
æ¡ä¾
æªmvcå¢å¼º
åå¤ï¼
- DispatcherServletä¸å¤®æ§å¶å¨
package wxm_mvc; import java.io.IOException;import java.lang.reflect.InvocationTargetException;import java.util.HashMap;import java.util.Map; import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse; import org.apache.commons.beanutils.BeanUtils; import com.web.AddCalAction;import com.web.DelCalAction;import com.web.DiviCalAction;import com.web.MulCalAction;import com.wxm.framework.ActionModel;import com.wxm.framework.ConfigModel;import com.wxm.framework.ConfigModelFactory;import com.wxm.framework.ForwardModel;import com.wxm.framework.ModelDrivern; public class DispatcherServlet extends HttpServlet { private static final long serialVersionUID = 1971683367441467463L; private Map<String, Action> actionMap=new HashMap<>(); public void init() { //http://localhost:8080/wxm/cal_add.action actionMap.put("/cal_add", new AddCalAction()); actionMap.put("/cal_del", new DelCalAction()); actionMap.put("/cal_mul", new MulCalAction()); actionMap.put("/cal_divi", new DiviCalAction()); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub init(); String url=req.getRequestURI(); url=url.substring(url.lastIndexOf("/"), url.lastIndexOf(".")); Action action = actionMap.get(url); action.execute(req, resp);// }}
- åæ§å¶å¨Action
package wxm_mvc; import java.io.IOException; import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse; public interface Action { String execute(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException;}
- å建å个å®ç°è¯·æ±çç±»
- å AddCalAction
-
package com.web; import java.io.IOException; import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse; import com.entity.Cal; import wxm_mvc.Action; public class AddCalAction implements Action{ public String execute(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException { String num1=req.getParameter("num1"); String num2=req.getParameter("num2"); Cal cal=new Cal(num1, num2); if(cal.getNum1()!=null&&cal.getNum2()!=null) { req.setAttribute("rs", Integer.valueOf(cal.getNum1())+Integer.parseInt(num2)); } // req.getRequestDispatcher("/rs.jsp").forward(req, resp); return "rs"; };}
- å DelCalAction
-
package com.web; import java.io.IOException; import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse; import com.entity.Cal; import wxm_mvc.Action; public class DelCalAction implements Action{ public String execute(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException { String num1=req.getParameter("num1"); String num2=req.getParameter("num2"); System.out.println(num1+num2); Cal cal=new Cal(num1, num2); if(cal.getNum1()!=null&&cal.getNum2()!=null) { req.setAttribute("rs", Integer.valueOf(cal.getNum1())-Integer.parseInt(num2)); } // req.getRequestDispatcher("/rs.jsp").forward(req, resp); return "rs"; };}
- ä¹ MulCalAction
-
package com.web; import java.io.IOException; import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse; import com.entity.Cal; import wxm_mvc.Action; public class MulCalAction implements Action{ public String execute(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException { String num1=req.getParameter("num1"); String num2=req.getParameter("num2"); System.out.println(num1+num2); Cal cal=new Cal(num1, num2); if(cal.getNum1()!=null&&cal.getNum2()!=null) { req.setAttribute("rs", Integer.valueOf(cal.getNum1())*Integer.parseInt(num2)); } // req.getRequestDispatcher("/rs.jsp").forward(req, resp); return "rs"; };}
- é¤
-
package com.web; import java.io.IOException; import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse; import com.entity.Cal; import wxm_mvc.Action; public class DiviCalAction implements Action{ public String execute(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException { String num1=req.getParameter("num1"); String num2=req.getParameter("num2"); System.out.println(num1+num2); Cal cal=new Cal(num1, num2); if(cal.getNum1()!=null&&cal.getNum2()!=null) { req.setAttribute("rs", Integer.valueOf(cal.getNum1())/Integer.parseInt(num2)); } // req.getRequestDispatcher("/rs.jsp").forward(req, resp); return "rs"; };}
- å建å®ä½ç±»ï¼åé¢ä¸ä¼åï¼
- Cal
-
package com.entity; public class Cal {private String num1;private String num2;public String getNum1() { return num1;}public void setNum1(String num1) { this.num1 = num1;}public String getNum2() { return num2;}public void setNum2(String num2) { this.num2 = num2;}public Cal(String num1, String num2) { super(); this.num1 = num1; this.num2 = num2;}public Cal() { super();}}
- é ç½®å®å ¨ç®å½web-infä¸çweb.xmlï¼åé¢ä¹ä¸ä¼åï¼
-
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>wxm_mvc</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>wxm_mvc.DispatcherServlet</servlet-class> <init-param> <param-name>xmlPath</param-name> <param-value>/mvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping></web-app>
- jspå±ç¤ºé¡µé¢index.jsp
-
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Insert title here</title><script type="text/javascript"> function doSub(v){if(v==1){ calForm.action="${pageContext.request.contextPath}/cal_add.action";}else if(v==2){ calForm.action="${pageContext.request.contextPath}/cal_del.action";}else if(v==3){ calForm.action="${pageContext.request.contextPath}/cal_mul.action";}else if(v==4){ calForm.action="${pageContext.request.contextPath}/cal_divi.action";}calForm.submit();} </script> </head><body><form id="calForm" action="" method="post">num1:<input type="text" name="num1"><br>num2:<input type="text" name="num2"><br><button onclick="doSub(1)" >+</button><button onclick="doSub(2)" >-</button><button onclick="doSub(3)" >*</button><button onclick="doSub(4)" >/</button></form></body></html>
- rs.jspç»æ页é¢
-
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Insert title here</title></head><body>ç»æ:${rs}</body></html>
- mvc.xml(srcç®å½ä¸)
-
<?xml version="1.0" encoding="UTF-8"?><config><action path="/cal_add" type="com.web.AddCalAction"> <forward name="rs" path="/rs.jsp" redirect="false" /> </action> <action path="/cal_del" type="com.web.DelCalAction"> <forward name="rs" path="/rs.jsp" redirect="false" /> <action path="/cal_mul" type="com.web.DelCalAction"> <forward name="rs" path="/rs.jsp" redirect="false" /> <action path="/cal_divi" type="com.web.DelCalAction"> <forward name="rs" path="/rs.jsp" redirect="false" /> </action></config>
- è¿è¡å°æå¡å¨å
- ææï¼
mvcå¢å¼º
æ¡ä¾1 å°Actionçä¿¡æ¯é ç½®å°xml(åå°å®ä¾å)
+ **解å³äºå¨æ¡æ¶ä»£ç ä¸å»æ¹å¨ï¼ä»¥ä¾¿äºå®æ客æ·éæ±ï¼è¿ä¸ªæ¯ä¸åçç
DispatcherServletä¸å¤®æ§å¶å¨: ä½ç¨ï¼æ¥å请æ±ï¼éè¿è¯·æ±å¯»æ¾è¯·æ±ç对åºçåæ§å¶å¨**
å·¥å ·ç±»ï¼ï¼ç´æ¥ç¨ï¼
- ActionModelï¼ç¨æ¥æè¿°actionæ ç¾
package com.wxm.framework; import java.io.Serializable;import java.util.HashMap;import java.util.Map; /** * ç¨æ¥æè¿°actionæ ç¾î· * @author Administrator * */public class ActionModel implements Serializable{ private static final long serialVersionUID = 6145949994701469663L; private Map<String, ForwardModel> forwardModels = new HashMap<String, ForwardModel>(); private String path; private String type; public String getPath() { return path; } public void setPath(String path) { this.path = path; } public String getType() { return type; } public void setType(String type) { this.type = type; } public void put(ForwardModel forwardModel){ forwardModels.put(forwardModel.getName(), forwardModel); } public ForwardModel get(String name){ return forwardModels.get(name); } }
- ConfigModelï¼ç¨æ¥æè¿°configæ ç¾
package com.wxm.framework; import java.io.Serializable;import java.util.HashMap;import java.util.Map; /** * ç¨æ¥æè¿°configæ ç¾ * @author Administrator * */public class ConfigModel implements Serializable{ private static final long serialVersionUID = -2334963138078250952L; private Map<String, ActionModel> actionModels = new HashMap<String, ActionModel>(); public void put(ActionModel actionModel){ actionModels.put(actionModel.getPath(), actionModel); } public ActionModel get(String name){ return actionModels.get(name); }}
- ConfigModelFactoryå·¥å模å¼å建config建模对象
package com.wxm.framework; import java.io.InputStream;import java.util.List; import org.dom4j.Document;import org.dom4j.Element;import org.dom4j.io.SAXReader; public class ConfigModelFactory { private ConfigModelFactory() { } private static ConfigModel configModel = null; public static ConfigModel newInstance() throws Exception { return newInstance("mvc.xml"); } /** * å·¥å模å¼å建config建模对象 * * @param path * @return * @throws Exception */ public static ConfigModel newInstance(String path) throws Exception { if (null != configModel) { return configModel; } ConfigModel configModel = new ConfigModel(); InputStream is = ConfigModelFactory.class.getResourceAsStream(path); SAXReader saxReader = new SAXReader(); Document doc = saxReader.read(is); List<Element> actionEleList = doc.selectNodes("/config/action"); ActionModel actionModel = null; ForwardModel forwardModel = null; for (Element actionEle : actionEleList) { actionModel = new ActionModel(); actionModel.setPath(actionEle.attributeValue("path")); actionModel.setType(actionEle.attributeValue("type")); List<Element> forwordEleList = actionEle.selectNodes("forward"); for (Element forwordEle : forwordEleList) { forwardModel = new ForwardModel(); forwardModel.setName(forwordEle.attributeValue("name")); forwardModel.setPath(forwordEle.attributeValue("path")); forwardModel.setRedirect(forwordEle.attributeValue("redirect")); actionModel.put(forwardModel); } configModel.put(actionModel); } return configModel; } public static void main(String[] args) { try { ConfigModel configModel = ConfigModelFactory.newInstance(); ActionModel actionModel = configModel.get("/loginAction"); ForwardModel forwardModel = actionModel.get("failed"); System.out.println(actionModel.getType()); System.out.println(forwardModel.getPath()); } catch (Exception e) { e.printStackTrace(); } }}
- ForwardModelç¨æ¥æè¿°forwardæ ç¾
package com.wxm.framework; import java.io.Serializable; /** * ç¨æ¥æè¿°forwardæ ç¾ * @author Administrator * */public class ForwardModel implements Serializable { private static final long serialVersionUID = -8587690587750366756L; private String name; private String path; private String redirect; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPath() { return path; } public void setPath(String path) { this.path = path; } public String getRedirect() { return redirect; } public void setRedirect(String redirect) { this.redirect = redirect; }
- ä¸å¤®æ§å¶å¨DispatcherServlet
-
package wxm_mvc; import java.io.IOException;import java.lang.reflect.InvocationTargetException;import java.util.HashMap;import java.util.Map; import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse; import org.apache.commons.beanutils.BeanUtils; import com.web.AddCalAction;import com.web.DelCalAction;import com.web.DiviCalAction;import com.web.MulCalAction;import com.wxm.framework.ActionModel;import com.wxm.framework.ConfigModel;import com.wxm.framework.ConfigModelFactory;import com.wxm.framework.ForwardModel;import com.wxm.framework.ModelDrivern; public class DispatcherServlet extends HttpServlet{ private static final long serialVersionUID = 2304962805570259027L;// private Map<String, Action> actionMap=new HashMap<String, Action>(); private ConfigModel configModel=null; public void init() { //http://localhost:8080/wxm/cal_add.action// actionMap.put("/cal_add", new AddCalAction());// actionMap.put("/cal_del", new DelCalAction());// actionMap.put("/cal_mul", new MulCalAction());// actionMap.put("/cal_divi", new DiviCalAction()); //1.actionçå¨æé ç½®ï¼å»ºæ¨¡ï¼ try { configModel=ConfigModelFactory.newInstance(); //æ¿ä»£äºmap } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub doPost(req, resp);}@Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { init(); String url=req.getRequestURI();url=url.substring(url.lastIndexOf("/"),url.lastIndexOf(".")); ActionModel actionModel=configModel.get(url); try { Action action= (Action) Class.forName(actionModel.getType()).newInstance(); action.execute(req, resp); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }}
- æ ¸å¿ä»£ç
- åé¢çå®ä½ç±»ä¸å
- Add/Del/Mul/DiviCalAction ä¹ä¸å
-
index.jsp års.jspä¹ä¸å
ææ
æ¡ä¾2 .éè¿ç»æç æ§å¶é¡µé¢ç跳转
-å°±æ¯ç®åï¼ req.getRequestDispatcher("/rs.jsp").forward(req, resp)/éå®å;ï¼
- ä¸å¤®æ§å¶å¨ï¼DispatcherServlet ï¼
- DispatcherAction methodName:add/minus/mul/div CalAction extends DispatcherAction æä¾ä¸ç»ä¸executeæ¹æ³çåæ°ãè¿åå¼ç¸åçæ¹æ³ï¼åªææ¹æ³åä¸ä¸æ ·
-
package wxm_mvc; import java.io.IOException;import java.lang.reflect.InvocationTargetException;import java.util.HashMap;import java.util.Map; import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse; import org.apache.commons.beanutils.BeanUtils; import com.web.AddCalAction;import com.web.DelCalAction;import com.web.DiviCalAction;import com.web.MulCalAction;import com.wxm.framework.ActionModel;import com.wxm.framework.ConfigModel;import com.wxm.framework.ConfigModelFactory;import com.wxm.framework.ForwardModel;import com.wxm.framework.ModelDrivern; public class DispatcherServlet extends HttpServlet{ private static final long serialVersionUID = 2304962805570259027L;// private Map<String, Action> actionMap=new HashMap<String, Action>(); private ConfigModel configModel=null; public void init() { //http://localhost:8080/wxm/cal_add.action// actionMap.put("/cal_add", new AddCalAction());// actionMap.put("/cal_del", new DelCalAction());// actionMap.put("/cal_mul", new MulCalAction());// actionMap.put("/cal_divi", new DiviCalAction()); //1.actionçå¨æé ç½®ï¼å»ºæ¨¡ï¼ try { configModel=ConfigModelFactory.newInstance(); //æ¿ä»£äºmap } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub doPost(req, resp);}@Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { init(); String url=req.getRequestURI();url=url.substring(url.lastIndexOf("/"),url.lastIndexOf(".")); ActionModel actionModel=configModel.get(url); try { Action action= (Action) Class.forName(actionModel.getType()).newInstance(); String code=action.execute(req, resp); ForwardModel forwardModel = actionModel.get(code); if(forwardModel!=null) { String jspPath = forwardModel.getPath(); if("false".equals(forwardModel.getRedirect())) {// å转åçå¤ç req.getRequestDispatcher(jspPath).forward(req, resp); }else { resp.sendRedirect(req.getContextPath()+jspPath); } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }}
- æ ¸å¿ä»£ç
-
åæ§å¶å¨Action/mvc.xml/AddAction/del/mul/diviä¸å
-CalActionä¸ç转ååéå®å注é
ä¾å¦
ç»æ
- æ¡ä¾3 å°ä¸ç»ç¸å ³çæä½æ¾å°ä¸ä¸ªActionä¸(åå°è°ç¨æ¹æ³)
-
DispatcherServlet ä¸å¤®æ§å¶å¨
æ ¸å¿ä»£ç
init(); String url=req.getRequestURI(); url=url.substring(url.lastIndexOf("/"), url.lastIndexOf(".")); ActionModel actionModel = configModel.get(url); try { Action action =(Action) Class.forName(actionModel.getType()).newInstance(); String code=action.execute(req, resp); ForwardModel forwardModel = actionModel.get(code); if(forwardModel!=null) { String jspPath = forwardModel.getPath(); if("false".equals(forwardModel.getRedirect())) {// å转åçå¤ç req.getRequestDispatcher(jspPath).forward(req, resp); }else { resp.sendRedirect(req.getContextPath()+jspPath); } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }
- åæ§å¶å¨ï¼Actionï¼----->å¢å¼ºççåæ§å¶å¨ï¼ActionSupport ï¼
package com.wxm.framework; import java.io.IOException;import java.lang.reflect.Method; import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse; import wxm_mvc.Action; public class ActionSupport implements Action{ public String execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // ä¼ åå°çä»ä¹æ¹æ³ï¼å®ï¼åå°ï¼å°±è°ç¨ä»ä¹æ¹æ³---å¨ææ¹æ³è°ç¨ï¼åå°ç¥è¯ï¼ String methodName=req.getParameter("methodName"); System.out.println(methodName); String code=null; //è·åå½ååæ§å¶å¨ç±»çå®ä¾// this.å¨è¿éæ¯æCalActionå®çä¸ä¸ªç±»å®ä¾ try { Method m = this.getClass().getDeclaredMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);//è·å类对象 m.setAccessible(true);//设置æé //å¨æè°ç¨addæ¹æ³ //å®ä¹æè¿åå¼ï¼æ¯è¿åç code=(String) m.invoke(this, req,resp); } catch (Exception e) { e.printStackTrace(); } return code; } }
- mvc.xml
- æ ¸å¿ä»£ç
-
<action path="/cal" type="com.web.CalAction"> <forward name="rs" path="/rs.jsp" redirect="false" /> </action>
- å åä¹é¤ï¼
package com.web; import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse; import com.entity.Cal;import com.wxm.framework.ActionSupport;import com.wxm.framework.ModelDrivern;public class CalAction extends ActionSupport implements ModelDrivern<Cal>{ private Cal cal=new Cal(); private String add(HttpServletRequest req,HttpServletResponse resp) { String num1=req.getParameter("num1"); String num2=req.getParameter("num2"); Cal cal=new Cal(num1, num2); if(cal.getNum1()!=null&&cal.getNum2()!=null) { req.setAttribute("rs", Integer.valueOf(cal.getNum1())+Integer.parseInt(num2)); }req.getRequestDispatcher("/rs.jsp").forward(req, resp); return "rs"; } private String del(HttpServletRequest req,HttpServletResponse resp) {String num1=req.getParameter("num1"); String num2=req.getParameter("num2");Cal cal=new Cal(num1, num2); if(cal.getNum1()!=null&&cal.getNum2()!=null) { req.setAttribute("rs", Integer.valueOf(cal.getNum1())+Integer.parseInt(num2));}req.getRequestDispatcher("/rs.jsp").forward(req, resp); return "rs"; } private String mul(HttpServletRequest req,HttpServletResponse resp) { String num1=req.getParameter("num1"); String num2=req.getParameter("num2"); Cal cal=new Cal(num1, num2); if(cal.getNum1()!=null&&cal.getNum2()!=null) { req.setAttribute("rs", Integer.valueOf(cal.getNum1())*Integer.parseInt(num2)); } req.getRequestDispatcher("/rs.jsp").forward(req, resp); return "rs"; } private String divi(HttpServletRequest req,HttpServletResponse resp) { String num1=req.getParameter("num1"); String num2=req.getParameter("num2");Cal cal=new Cal(num1, num2); if(cal.getNum1()!=null&&cal.getNum2()!=null) { req.setAttribute("rs", Integer.valueOf(cal.getNum1())/Integer.parseInt(num2)); } req.getRequestDispatcher("/rs.jsp").forward(req, resp); return "rs"; } public Cal getModel() { // TODO Auto-generated method stub return cal; } }
- jsp页é¢ï¼index.jsp
-
function doSub(v){ if(v==1){ calForm.action="${pageContext.request.contextPath}/cal.action?methodName=add"; }else if(v==2){ calForm.action="${pageContext.request.contextPath}/cal.action?methodName=del"; }else if(v==3){ calForm.action="${pageContext.request.contextPath}/cal.action?methodName=mul"; }else if(v==4){ calForm.action="${pageContext.request.contextPath}/cal.action?methodName=divi"; } calForm.submit(); }
- ææ
æ¡ä¾4.å©ç¨ModelDriveræ¥å£å¯¹Java对象è¿è¡èµå¼(åå°è¯»åæ¹æ³)
- DispatcherServlet
- æ ¸å¿ä»£ç
try { init(); String url=req.getRequestURI(); url=url.substring(url.lastIndexOf("/"),url.lastIndexOf(".")); ActionModel actionModel=configModel.get(url); if(actionModel==null) { throw new RuntimeException("ä½ æ²¡æé
ç½®actionæ ç¾ï¼æ¾ä¸å°å¯¹åºçèªæ§å¶å¨æ¥å¤çæµè§å¨åéåºæ¥çè¯·æ± "); } // Action action=(Action) Class.forName("com.web.AddCalAction").newInstance();// Action action=(Action) AddCalAction(); // actionå°±æ¯com.web.AddCalAction Action action=(Action) Class.forName(actionModel.getType()).newInstance(); if(action instanceof ModelDrivern) { ModelDrivern modelDrivern=(ModelDrivern) action;// æ¤æ¶çmodelææå±æ§å¼æ¯null Object model = modelDrivern.getModel();// ç»modelèµå¼ï¼é£ä¹æå³çè°ç¨add/delçcalä¸å为空 BeanUtils.populate(model, req.getParameterMap());// å¯ä»¥å°req.getParameterMap()çå¼éè¿åå°çæ¹å¼å°å
¶å¡è¿modelå®ä¾ // åç// Map<String, String[]> paMap = req.getParameterMap();// Set<Entry<String, String[]>> entrySet = paMap.entrySet();// Class<? extends Object> cls = model.getClass();// for (Entry<String, String[]> entry : entrySet) {// Field field = cls.getDeclaredField(entry.getKey());// field.setAccessible(true);// field.set(model, entry.getValue());// }// // } String code=action.execute(req, resp); ForwardModel forwardModel=actionModel.get(code); if(forwardModel!=null) { String jspPath=forwardModel.getPath(); if("false".equals(forwardModel.getRedirect())) {// å转åçå¤ç req.getRequestDispatcher(jspPath).forward(req, resp); }else {// 注æï¼é»è®¤ä¼ç¼ºå¤±é¡¹ç®å resp.sendRedirect(req.getContextPath()+jspPath); } } } } 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(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); }
- ModelDriveræ¥å£å¯¹Java对象è¿è¡èµå¼(CalAction )
-
- ModelDrivern
-
package com.wxm.framework;public interface ModelDrivern<T> { T getModel();}
- å¢å æ¹æ¥
-
package com.web; import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse; import com.entity.Cal;import com.wxm.framework.ActionSupport;import com.wxm.framework.ModelDrivern;public class CalAction extends ActionSupport implements ModelDrivern<Cal>{ private Cal cal=new Cal(); private String add(HttpServletRequest req,HttpServletResponse resp) {// String num1=req.getParameter("num1");// String num2=req.getParameter("num2");// Cal cal=new Cal(num1, num2); if(cal.getNum1()!=null&&cal.getNum2()!=null) { req.setAttribute("rs", Integer.valueOf(cal.getNum1())+Integer.parseInt(cal.getNum2())); }// req.getRequestDispatcher("/rs.jsp").forward(req, resp); return "rs"; } private String del(HttpServletRequest req,HttpServletResponse resp) {// String num1=req.getParameter("num1");// String num2=req.getParameter("num2");// Cal cal=new Cal(num1, num2); if(cal.getNum1()!=null&&cal.getNum2()!=null) { req.setAttribute("rs", Integer.valueOf(cal.getNum1())+Integer.parseInt(cal.getNum2())); }// req.getRequestDispatcher("/rs.jsp").forward(req, resp); return "rs"; } private String mul(HttpServletRequest req,HttpServletResponse resp) {// String num1=req.getParameter("num1");// String num2=req.getParameter("num2");// Cal cal=new Cal(num1, num2); if(cal.getNum1()!=null&&cal.getNum2()!=null) { req.setAttribute("rs", Integer.valueOf(cal.getNum1())*Integer.parseInt(cal.getNum2())); }// req.getRequestDispatcher("/rs.jsp").forward(req, resp); return "rs"; } private String divi(HttpServletRequest req,HttpServletResponse resp) {// String num1=req.getParameter("num1");// String num2=req.getParameter("num2");// Cal cal=new Cal(num1, num2); if(cal.getNum1()!=null&&cal.getNum2()!=null) { req.setAttribute("rs", Integer.valueOf(cal.getNum1())/Integer.parseInt(cal.getNum2())); }// req.getRequestDispatcher("/rs.jsp").forward(req, resp); return "rs"; } public Cal getModel() { // TODO Auto-generated method stub return cal; } }
- mvc.xml
-
<action path="/cal" type="com.web.CalAction"> <forward name="rs" path="/rs.jsp" redirect="false" /> </action>
- index.jsp
-
function doSub(v){ if(v==1){ calForm.action="${pageContext.request.contextPath}/cal.action?methodName=add"; }else if(v==2){ calForm.action="${pageContext.request.contextPath}/cal.action?methodName=del"; }else if(v==3){ calForm.action="${pageContext.request.contextPath}/cal.action?methodName=mul"; }else if(v==4){ calForm.action="${pageContext.request.contextPath}/cal.action?methodName=divi"; } calForm.submit(); }
- ææ è¿é注æç解
常è§å¼å¸¸
å¦æ¥nullæéå¼å¸¸/ä½ æ²¡æé ç½®actionæ ç¾ï¼æ¾ä¸å°å¯¹åºçèªæ§å¶å¨æ¥å¤çæµè§å¨åéåºæ¥ç请æ±
解å³æ¹æ³ mvc.xmlé ç½®ççè·¯å¾åºäºé®é¢
å¦æ£æ¥ä¸é¢ä¸ä¸ªå°æ¹ æ¯å¦æ£ç¡®
å¦ä¸ç§æ åµä¼æ¯
æ¡ä¾5 使å¾æ¡æ¶çé ç½®æ件å¯å
-
å¨ä¸æ¹å¨frameworkæ件éé¢ç代ç åæä¸ï¼ä¿®æ¹mvc.xmlæ件ï¼
æ°å»ºä¸ä¸ªä¸srcå级ä¸çwxm.xmlï¼
- web.xmlæ ¸å¿ä»£ç
<init-param> <param-name>xmlPath</param-name> <param-value>/wxm.xml</param-value></init-param>
- ä¸å¤®æ§å¶å¨æ ¸å¿ä»£ç
-
try { String xmlPath=this.getInitParameter("xmlPath"); System.out.println(xmlPath); if(xmlPath==null||"".equals(xmlPath)) { configModel=ConfigModelFactory.newInstance(); }else { configModel=ConfigModelFactory.newInstance(xmlPath); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }
- ææ
- ååè¦å¯¹åºå¥½ï¼ è¿éæå 示è举个é误çä¾å