表单提交并转发后,再次调用控制器

时间:2019-06-04 12:02:23

标签: java jsp tomcat java-ee controller

我对此问题感到困惑:JAVA EE(框架:tomcat)控制器具有方法String execute(request,response),该方法管理house.jsp JSP页面,这是第一次显示表单,之后是表单完成后,它管理返回的数据;此后,execute方法返回“ /adminAccueil.jsp”,路由器中的转发应显示正确的jsp页面;确实可以,但是我看到控制器又被调用了一次,这是一个意外的时间,并且由于请求对象包含不正确的数据,程序崩溃了。

更简单地说,这是一些代码: adminCreerModifierBien.jsp(一种形式):

<form id="modif" action="intro?action=action5" method="post" accept-charset="UTF-8" novalidate
      class="needs-validation">
    <input type="hidden" id="id" name="id" value="${bien.getId()}"/>
    <div class="row">
        <div class="col">
            <label for="description">Description :</label>
            <input type="text" id="description" name="description"
                   class="form-control" value="${bien.getDescription()}" required/>
            <div class="valid-feedback">
                Ca a l'air bien!
            </div>
            <div class="invalid-feedback">
                La description est nécessaire
            </div>
        </div>
    </div>
    <div class="row">
    ...

请注意,jsp页面中有一些javascript:

    $(function () {
        'use strict';
        window.addEventListener('load', function () {
// Fetch all the forms we want to apply custom Bootstrap validation styles to
            var forms = document.getElementsByClassName('needs-validation');
// Loop over them and prevent submission
            var validation = Array.prototype.filter.call(forms, function (form) {
                form.addEventListener('submit', function (event) {
                    if (form.checkValidity() === false) {
                        event.preventDefault();
                        event.stopPropagation();
                    }
                    form.classList.add('was-validated');
                }, false);
            });
        }, false);
    });

它用于表单验证。

控制器管理两种类型的调用:直接(GET)和表单提交(POST)之后:

@Override
    public String execute(HttpServletRequest request, HttpServletResponse response)
    {
        try {
            request.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException e) {
            System.out.println("erreur de lecture des paramètres");
        }

        if (request.getParameter("description") != null) {
            //si retour de formulaire

控制器中的无错误情况以此结尾:

try {
                    daoBien.mettre_a_jour(bien_miseajour);
                } catch (ImmobException e) {
                    request.setAttribute("message", "problème de mise à jour du bien");
                    request.setAttribute("bien", bien_miseajour);
                    try {
                        mettreVillesChauffagesOptionsDansRequest(request);
                    } catch (ImmobException ex) {
                        request.setAttribute("message","erreur lors d'accès à la base de données");
                        return "/admin/erreur/erreur.jsp";
                    }
                    return "/admin/biens/adminCreerModifierBien.jsp";

                }
                //ok
                try {
                    request.setAttribute("tousBiens", daoBien.recupereBiensAvecRequete(
                            "order by DATECREATION desc"));
                } catch (ImmobException e) {
                    request.setAttribute("message","erreur lors de la récupération des biens");
                    return "/admin/erreur/erreur.jsp";
                }
                return "/admin/adminAccueil.jsp";

adminAccueil.jsp页面正确显示,但是控制器又被调用一次...

这是路由器(主控制器,它将被叫分派给控制器:

    @Override
    public void init() throws ServletException
    {

        correspondance.put("action1", new AccueilBiens());
        correspondance.put("action2", new Acheter());
        correspondance.put("action3", new DetailsBien());
        correspondance.put("action4", new AdminAccueil());
        correspondance.put("action5", new AdminCreerModifierBien());
        correspondance.put("action6", new AdminSupprimerBien());
        correspondance.put("action7", new AdminListeOptions());
        correspondance.put("action8", new AdminCreerModifierOption());
        correspondance.put("action9", new AdminSupprimerOption());

//        logger = Logger.getLogger("immobiSite");


    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        process(request, response, getServletContext());

    }

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


    }

    public void process(HttpServletRequest request, HttpServletResponse response, ServletContext servletContext) throws ServletException, IOException
    {
//        logger.info("test");
        String action = null;
        ICommand commandeRetour = null;
        try {
            action = request.getParameter("action");

            if (action == null || action.equals(""))
                action = "action1";

            commandeRetour = correspondance.get(action);

            request.setAttribute("retour_origine", "intro?action=" + request.getParameter("page_origine"));

        } catch (Exception e) {
            System.out.println("erreur de routage");
        }

        if (request.getSession().getAttribute("isAdmin") == null)
            request.getSession().setAttribute("isAdmin", false);


        servletContext.getRequestDispatcher(commandeRetour.execute(request, response)).forward(request, response);
    }

有人可以帮我吗?该行为是来自jsp页面,还是来自路由器中的某些错误概念?

谢谢。

编辑: 添加的呼叫是GET。 该行为发生在每个页面上:路由器被调用两次。是tomcat相关的问题吗?

0 个答案:

没有答案