从属性文件ajax mvc获取处理程序

时间:2015-05-27 23:07:58

标签: javascript ajax jsp servlets model-view-controller

我正在尝试验证电子邮件输入,如果我的数据库中有任何类似的电子邮件与ajax.I使用没有任何框架的mvc模式。你会看到我的控制器是一个servlet,它读取一个名称并使用属性进行验证文件,然后为每个事件调用一个特定的处理程序。它显示一个错误,因为由于ajax xmlHttp.open中的url参数,事件始终为null

PropertiesFile

300

控制器

    SIGNUP = events.SignUpEventHandler
    AJAX= events.EventAjaxMail

JSP& AJAX& JAVASCRIPT

package controller;
import events.EventHandlerBase;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.ResourceBundle;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


public class Controller extends HttpServlet {

     protected HashMap events= new HashMap();

     public void init() throws ServletException{

      // get the event values and save them into events
      ResourceBundle bundle = ResourceBundle.getBundle("Event");
      Enumeration e = bundle.getKeys();

        while(e.hasMoreElements()) {
           String key = (String) e.nextElement();
           String value = bundle.getString(key);
           try {
                EventHandlerBase event =
                (EventHandlerBase) Class.forName(value).newInstance();
                events.put(key, event);
                System.out.println(this + "init event"+key+ "Handler: "+ event.getClass().getName());

                } catch(Exception exc) {
                    System.out.println("NO HANDLER FOUND!");
                    exc.printStackTrace();
            }
        }


    }

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


    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {      


        //get session if the server sent one to the browser
        HttpSession session = request.getSession(false);

        //case that no session found, start new
        if (session == null) {
            session = request.getSession();
        }

        String event = validateEvent(request);

        EventHandlerBase handler = getEventHandler(event);

        try {
            handler.process(session, request, response);
        } catch (Exception e) {
            e.printStackTrace();
            //handler = getEventHandler("UNKNOWN_EVENT");
        }


        handler.forward(request, response);
    }

    protected String validateEvent(HttpServletRequest request) {
        String event = request.getParameter("event");
        System.out.println("\n\nevent= "+event);

        if (event == null || !events.containsKey(event)) {
        //event = Events.UNKNOWN.toString();
            System.out.println("UNKNOWN_EVENT EGINE");
            event="UNKNOWN_EVENT";

        }

        return event;
     }

    protected EventHandlerBase getEventHandler(String e) {
            EventHandlerBase h;
            try {
            h = (EventHandlerBase) events.get(e);
            } catch(Exception exc) {
            h = (EventHandlerBase)events.get("UNKNOWN_EVENT");
            }
            //System.out.println("to h sto getEventHandler: "+ h.getClass().getName());

            return h;
    }



  }

在该url中我想要一个name =“event”和value =“AJAX”,以便控制器(controller_servl)可以处理该事件

更新

我已经编辑了如下所示的网址并且有效。这是正确的解决方案吗?

0 个答案:

没有答案