隨筆-54  評論-0  文章-2  trackbacks-0

          1.新建FormBean

          //

          生成getset方法

          2.新建ActionMapping

          // <action path="/login"type="com.it315.action.LoginAction" name = "loginForm" > <!—nameforward標簽的唯一

          <forward name="success" path="/Success.jsp"></forward>

          <forward name="errpr" path="/Error.jsp"></forward>

          </action>

          private String path;

          private String name;

          private String type;

          private Map<String,ActionForward> forward = new HashMap< String,ActionForward >();

          getset方法

          3.新建ActionForward

          private String name;

          private String path;

          getset方法

          4.2步驟中的ActionMapping中新建Map

          5.新建ServletActionServlet

          重寫init,在其中

          //讀取Struts-config.xml,離不開dom4j的內容,當然struts不是用dom4j

          String path = this.getServletConfig().getInitParameter(“config”);

          this.getServletContext().getRealPath(path);

          Document document = this.getDocument(path);

          String xpath = “/struts-config/form-beans”

          6.新建Action

          package com.itcast.struts.sys;

          import javax.servlet.http.HttpServletRequest;
          import javax.servlet.http.HttpServletResponse;

          public class Action {

          public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,
          HttpServletResponse response){
          return null;
          }


          }


          7.新建ActionForm

          package com.itcast.struts.sys;

          public abstract class ActionForm {

          }


          8.新建LoginAction

          package com.itcast.struts.action;

          import javax.servlet.http.HttpServletRequest;
          import javax.servlet.http.HttpServletResponse;

          import com.itcast.struts.form.LoginForm;
          import com.itcast.struts.sys.Action;
          import com.itcast.struts.sys.ActionForm;
          import com.itcast.struts.sys.ActionForward;
          import com.itcast.struts.sys.ActionMapping;

          public class LoginAction extends Action {

          @Override
          public ActionForward execute(ActionMapping mapping, ActionForm form,
          HttpServletRequest request, HttpServletResponse response) {

          System.out.println("form "+form);
          LoginForm loginForm=(LoginForm)form;
          System.out.println(loginForm.getUsername());

          if("sa".equals(loginForm.getUsername())){
          return mapping.findForward("success");
          }
          return mapping.findForward("error");

          }

          }


           9.新建LoginForm

          package com.itcast.struts.form;

          import com.itcast.struts.sys.ActionForm;

          public class LoginForm extends ActionForm{

          private String username;
          private String psw;

          public String getUsername() {
          return username;
          }
          public void setUsername(String username) {
          System.out.println("username "+username);
          this.username = username;
          }
          public String getPsw() {
          return psw;
          }
          public void setPsw(String psw) {
          System.out.println("psw "+psw);
          this.psw = psw;
          }

          }


          10.配置Struts-config.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>
          <servlet-name>action</servlet-name>
          <servlet-class>com.itcast.struts.sys.ActionServlet</servlet-class>
          <init-param>
          <param-name>config</param-name>
          <param-value>/WEB-INF/struts-config.xml</param-value>
          </init-param>
          <load-on-startup>0</load-on-startup>
          </servlet>
          10.配置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>
          <servlet-name>action</servlet-name>
          <servlet-class>com.itcast.struts.sys.ActionServlet</servlet-class>
          <init-param>
          <param-name>config</param-name>
          <param-value>/WEB-INF/struts-config.xml</param-value>
          </init-param>
          <load-on-startup>0</load-on-startup>
          </servlet>


          <servlet-mapping>
          <servlet-name>action</servlet-name>
          <url-pattern>*.do</url-pattern>
          </servlet-mapping>

          </web-app>


          <servlet-mapping>
          <servlet-name>action</servlet-name>
          <url-pattern>*.do</url-pattern>
          </servlet-mapping>
          </web-app>

          10.新建ActionServlet

          package com.itcast.struts.sys;


          import java.io.File;
          import java.io.IOException;
          import java.lang.reflect.InvocationTargetException;
          import java.lang.reflect.Method;
          import java.util.ArrayList;
          import java.util.Collection;
          import java.util.HashMap;
          import java.util.List;
          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.dom4j.Document;
          import org.dom4j.DocumentException;
          import org.dom4j.Element;
          import org.dom4j.Node;
          import org.dom4j.io.SAXReader;

          public class ActionServlet extends HttpServlet {

          /*
          * <form-bean name="loginForm" type="com.itcast.struts.form.LoginForm"></form-bean>
          * map<name,new FormBean()>
          */
          private Map<String,FormBean> FormBeanMap=new HashMap<String,FormBean>();

          /*
          * <action path="/login" name="loginForm"
          * type="com.itcast.struts.action.LoginAction" scope="request">
          * map<path,new ActionMapping()>
          */

          private Map<String,ActionMapping> actionMapingMap=new HashMap<String,ActionMapping>();

          private static final long serialVersionUID = 1L;

          /**
          * 服務器啟動讀取struts-config.xml文件
          */
          public void init() throws ServletException {
          String filepath=this.getServletConfig().getInitParameter("config");
          filepath=this.getServletContext().getRealPath(filepath);
          System.out.println(filepath);

          //獲取Document文檔
          Document document=this.getDocument(filepath);
          //////////////////////////////////////////////////////////////////////////////////////////////////
          /*
          * 加載form-bean
          * <form-bean name="loginForm" type="com.itcast.struts.form.LoginForm"></form-bean>
          */
          String xpath="/struts-config/form-beans/form-bean";
          List<Node> formBeanList=document.selectNodes(xpath);
          if(formBeanList!=null&&formBeanList.size()>0){
          for(int i=0;i<formBeanList.size();i++){
          Element formBeanElement=(Element) formBeanList.get(i);
          String name=formBeanElement.attributeValue("name");
          String type=formBeanElement.attributeValue("type");

          FormBean formBean=new FormBean();
          formBean.setName(name);
          formBean.setType(type);
          FormBeanMap.put(name, formBean);
          }
          }

          // //遍歷
          // Collection<FormBean> c=FormBeanMap.values();
          // List<FormBean> list=new ArrayList<FormBean>(c);
          // for(int i=0;i<list.size();i++){
          // System.out.println(list.get(i).getName()+" "+list.get(i).getType());
          // System.out.println(FormBeanMap.get(list.get(i).getName()).getType());
          // }
          //
          //////////////////////////////////////////////////////////////////////////////////////////////////
          //<action path="/login" name="loginForm" type="com.itcast.struts.action.LoginAction" scope="request">

          xpath="/struts-config/action-mappings/action";
          List<Node> actionMapingList=document.selectNodes(xpath);
          if(actionMapingList!=null&&actionMapingList.size()>0){
          for(int i=0;i<actionMapingList.size();i++){

          //<action>
          Element actionMapingElement=(Element) actionMapingList.get(i);

          String path=actionMapingElement.attributeValue("path");
          String name=actionMapingElement.attributeValue("name");
          String type=actionMapingElement.attributeValue("type");

          System.out.println(path +" "+name +" "+type);
          ActionMapping mapping=new ActionMapping();

          mapping.setName(name);
          mapping.setPath(path);
          mapping.setType(type);
          actionMapingMap.put(path, mapping);
          //////////////////////////////////////////////////////////////////////////////////////////////////
          /*
          * <action path="/login" name="loginForm" type="com.itcast.struts.action.LoginAction" scope="request">
          <!-- 配置轉發或重定向 默認值是轉發-->
          <!--
          name 屬性:表示forword標簽的唯一標識
          path 屬性:表示要轉發或重定向的路徑
          -->
          <forward name="success" path="/success.jsp"></forward>

          */
          List<Element> forwardList=actionMapingElement.elements("forward");
          if(forwardList!=null&&forwardList.size()>0){
          for(int k=0;k<forwardList.size();k++){
          Element forwardElement=forwardList.get(k);

          String forward_name=forwardElement.attributeValue("name");
          String forward_path=forwardElement.attributeValue("path");

          System.out.println(forward_name+" "+forward_path);

          ActionForward actionForward=new ActionForward();
          actionForward.setName(forward_name);
          actionForward.setPath(forward_path);
          mapping.addActionForward(actionForward);

          }
          }

          }
          }
          ///////////////////////////////////////////////////////////////////////////////////////////////////
          //遍歷private Map<String,ActionMapping> actionMapingMap=new HashMap<String,ActionMapping>();
          List<ActionMapping> list=new ArrayList(actionMapingMap.values());
          for(int i=0;i<list.size();i++){

          ActionMapping m=list.get(i);
          System.out.println("m.getName() "+m.getName());
          System.out.println("m.getPath() "+m.getPath());
          System.out.println("m.getType() "+m.getType());

          List<ActionForward> l=new ArrayList(m.getForward().values());

          for(int k=0;k<l.size();k++){
          ActionForward a=l.get(k);
          System.out.println("^^^ "+a.getName()+ " " +a.getPath());


          }

          }
          }

          public Document getDocument(String path){
          Document document=null;
          SAXReader read=new SAXReader();
          try {
          document=read.read(new File(path));
          } catch (DocumentException e) {
          e.printStackTrace();
          }
          return document;

          }

          protected void doGet(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {

          request.setCharacterEncoding("utf-8");
          response.setContentType("text/html;charset=utf-8");
          //////////////////////////////////////////////////////////////////////////////////////////////////
          //獲取請求路徑
          String servletPath=request.getServletPath();
          // /login.do ---->/login
          //System.out.println("servletPath "+servletPath);
          servletPath=servletPath.substring(0,servletPath.lastIndexOf("."));
          System.out.println("servletPath "+servletPath); ///login

          //////////////////////////////////////////////////////////////////////////////////////////////////
          //通過解析請求路徑獲取 ActionMapping
          //private Map<path,ActionMapping> actionMapingMap=new HashMap<String,ActionMapping>();
          ActionMapping mapping=actionMapingMap.get(servletPath);
          //System.out.println(maping.getPath());
          //System.out.println(maping.getName());
          //System.out.println(maping.getType());
          String formBeanName=mapping.getName();
          //通過name屬性查找FormBean
          //private Map<String,FormBean> FormBeanMap=new HashMap<String,FormBean>();

          FormBean formBean=FormBeanMap.get(formBeanName);
          System.out.println(formBean.getName()+" "+formBean.getType());
          //////////////////////////////////////////////////////////////////////////////////////////////////
          //login.jsp頁面的數據填充的ActionForm(LoginForm)中

          //獲取FormBean中type屬性的值
          String formBean_type=formBean.getType(); // type=com.itcast.struts.form.LoginForm
          //創建com.itcast.struts.form.LoginForm類的實例
          try {
          Object obj=Class.forName(formBean_type).newInstance();
          //轉化到ActionForm
          ActionForm actionForm=(ActionForm)obj;

          //獲取actionForm該對象的類表示
          Class clazz=actionForm.getClass();

          //獲取類中所有的方法
          Method[] method=clazz.getMethods();

          //存放set方法的集合
          List<Method> listMehtod=new ArrayList<Method>();

          //獲取類中的set方法
          if(method!=null&&method.length>0){
          for(int i=0;i<method.length;i++){
          Method m=method[i];
          System.out.println("^^^$$$$$$^^^^^^^m "+m.getName());
          //方法的名稱是以set
          if(m.getName().startsWith("set")){
          listMehtod.add(m);
          }
          }
          }


          //遍歷所有set方法
          if(listMehtod.size()>0){
          for(int i=0;i<listMehtod.size();i++){
          Method m=listMehtod.get(i);
          String methodname=m.getName();
          System.out.println("^^^^^^^^^^methodname "+methodname); //setUsername

          //獲取屬性
          String params=methodname.substring(3,4).toLowerCase()+methodname.substring(4);
          System.out.println("params "+params);

          //調用的是set方法 public void setUsername(String username)
          m.invoke(actionForm, request.getParameter(params));
          }
          }
          //////////////////////////////////////////////////////////////////////////////////////////////////
          //獲取type屬性
          String type=mapping.getType();
          System.out.println("type "+type);

          Action action=(Action)Class.forName(type).newInstance();

          ActionForward actionForward=action.execute(mapping, actionForm, request, response);


          System.out.println("actionForward.getPath() "+actionForward.getPath());

          //重定向
          //response.sendRedirect(location)

          //轉發
          request.getRequestDispatcher(actionForward.getPath()).forward(request, response);


          } catch (InstantiationException e) {
          e.printStackTrace();
          } catch (IllegalAccessException e) {
          e.printStackTrace();
          } catch (ClassNotFoundException e) {
          e.printStackTrace();
          } catch (IllegalArgumentException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
          } catch (InvocationTargetException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
          }
          }

          protected void doPost(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
          this.doGet(request, response);
          }
          }

          11.新建success.jsp和error.jsp 這簡單就不贅述!







          posted on 2010-01-15 02:57 d66380022 閱讀(1894) 評論(0)  編輯  收藏
          主站蜘蛛池模板: 如东县| 屏山县| 吴江市| 靖宇县| 满洲里市| 平顶山市| 班玛县| 龙井市| 延川县| 新和县| 客服| 正镶白旗| 华池县| 招远市| 沙湾县| 柳林县| 兴山县| 南阳市| 内黄县| 孟津县| 浙江省| 大余县| 溧阳市| 鹿泉市| 自治县| 菏泽市| 科尔| 五指山市| 徐水县| 名山县| 黑河市| 旺苍县| 河北省| 资兴市| 措勤县| 通河县| 二连浩特市| 岳普湖县| 双城市| 嘉祥县| 达州市|