struts初步学
目的:
一个servlet process方法处理所有的 .do
-----------------------------------------------------------------------------
主要的不同之处是只用到一个servlet 来截获我们的所有的.do 请求
----------------------------------------------------------------------------
然后 我们拿到url 参数
Exmple:
通过request.getRequestURI 来拿到url参数
/struts/addUser.do
------------------------------------------------------------------------
但是我们需要的是 /addUser 这个东西来创建 addUserAction
对于action需要的是一个execute方法
--------------------------------------------------------------------
相当于我们将所有的servlet修改成为action
通过种种方法我们进入action,并拿到跳转信息
在servlet(中央控制器中跳转)
-------------------ok----------------------------------------
如下是actionServlet
-----------------------actionServlet-----------------------------
public class ActionServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { this.process(request,response); } public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { this.process(request,response); } protected void process (HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{ String url = request.getRequestURI();//从View层我拿到了url请求地址 String path = url.substring(url.lastIndexOf("/"), url.lastIndexOf("."));//将请求地址截获修剪 // load properties Properties prop = new Properties(); InputStream ips = this.getClass().getClassLoader().getResourceAsStream("actionconfig.properties"); prop.load(ips); String actionName = prop.getProperty(path);//拿到我们的actionName之后我们可以生成对象了 System.out.println(actionName); Action action = null; try {action = (Action) Class.forName(actionName).newInstance(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } String actionForward = action.execute(request, response); response.sendRedirect(actionForward); }
-----------------------END-----------------------------