简体   繁体   English

如何通过单击按钮运行在jsp中声明的函数

[英]How to run a function declared in jsp from a button click

i'm trying to run a function that i declared in jsp (<%! %>) when i click on a button. 我试图运行我在按钮上单击时在jsp(<%!%>)中声明的函数。 i don't want to load a new page, i'd like to stay on the same page and just execute something. 我不想加载新页面,我想停留在同一页面上并执行一些操作。

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" import="hitchhike.*"%>
<!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>Loading Route</title>
</head>
<body>
<%! 
    String login;
    String adresseOrigin;
    String adresseDestination;
    Adresse origin;
    Adresse destination;
    Itineraire itineraire;
%>
<%
    login=request.getParameter("login");
    adresseOrigin=request.getParameter("inputOrigin");
    adresseDestination=request.getParameter("inputDestination");
    origin=new Adresse(adresseOrigin);
    destination=new Adresse(adresseDestination);
    origin.generateAdresse();
    destination.generateAdresse();
    itineraire=new Itineraire(origin, destination);
    itineraire.calculItineraire();
%>
<%=origin.getAdresse() %>
</br>
<%=destination.getAdresse() %>
</br>
<%=itineraire.getTemps() %>
    <button type="submit" class="btn btn-primary" onclick="valider()">Validate</button>
</body>
</html>
<%!
void valider(){
    RouteDAO routeDAO=new RouteDAO();
    Route route=new Route(login, 
            origin.getAdresse(), 
            ""+origin.getcoordX(), 
            ""+origin.getCoordY(), 
            destination.getAdresse(),
            ""+destination.getcoordX(),
            ""+destination.getCoordY(),
            itineraire.getTempsSec()
            );
    routeDAO.addRoute(route);
}
    %>

this is my code, the function i'd like to run is the one at the end ( valider() ). 这是我的代码,我想运行的功能是最后一个(valider())。 but when i click on the button i have the error "valider is not defined" i'd rather not add jquery. 但是,当我单击按钮时,出现错误“未定义验证器”,我宁愿不添加jquery。

thanks for helping me 谢谢你帮我

The onclick event can only be tied to a JS/ Jquery function and the code in <%! ... %> onclick事件只能绑定到JS / Jquery函数和<%! ... %>的代码<%! ... %> <%! ... %> gets compiled into java (backend code). <%! ... %>被编译成Java(后端代码)。

You will have to move the function into another JSP/ backend java code make an AJAX call as part of the onclick event function. 您必须将函数移到另一个JSP /后端Java代码中,作为onclick事件函数的一部分进行AJAX调用。

Create a new JSP, say validator.jsp at the same directory level. 创建一个新的JSP,在同一目录级别说一下validator.jsp。 validator.jsp would have: validateator.jsp将具有:

<%!
    RouteDAO routeDAO=new RouteDAO();
    Route route=new Route(login, 
            origin.getAdresse(), 
            ""+origin.getcoordX(), 
            ""+origin.getCoordY(), 
            destination.getAdresse(),
            ""+destination.getcoordX(),
            ""+destination.getCoordY(),
            itineraire.getTempsSec()
            );
    routeDAO.addRoute(route);
%>

add a JS, JQuery function in the actual JSP like this: 在实际的JSP中添加一个JS,JQuery函数,如下所示:

function validator() {
    $.ajax({
        url: 'validator.jsp',  // Change the relative path if needed 
        type: "GET", // This is optional and defaults to GET as well
        error : function(){ 
            console.log('Error in the AJAX call'); 
        },
        success: function(msg){      
                console.log(msg);
        }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM