简体   繁体   English

我想在单个jsp页面中使用java变量

[英]I want to use java variable in single jsp page

<%!
    public void runJavaMethod(int id)
    {
    %>
    <%
        try{
            String icd = request.getParameter("icd");
            String inm = request.getParameter("inm");
            String istk = request.getParameter("istock");
            String sstk = request.getParameter("sstock");
            String upr = request.getParameter("uprice");
            String spr = request.getParameter("sprice");

            r = s.executeQuery("select * from itemsyncdata");
            while(r.next())
            {
                s.executeUpdate("update itemsyncdata set itemcode='"+icd+"',itemname='"+inm+"',instock='"+istk+"',storestock='"+sstk+"',unitprice='"+upr+"',storeprice='"+spr+"' where id='"+a+"'");
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

    %>
    <%!} %>

And I am Calling Function from html like 我从html像

<input type="submit" id="btnSync" value="Sync" class="button" name="Sync" onclick="<%runJavaMethod(r.getInt(1));%>"/>

So We want runJavaMethod Parameter. 因此,我们需要runJavaMethod参数。

onclick="<%runJavaMethod(r.getInt(1));%>"/>

HTML/Javascript Plays on client side and JSP/Java plays on server side. HTML / Javascript在客户端播放,而JSP / Java在服务器端播放。

Simply you can't. 简直你做不到。 You might misunderstand that JSP and HTML/JavaScript existed on same document. 您可能会误解JSP和HTML/JavaScript存在于同一文档中。 Yes but JSP part compiles on server side itself comes to client. 是的,但是JSP部分是在服务器端本身进行编译的。 What you can do is you have to make a server request. 您可以做的是必须发出服务器请求。 Most probably look at Ajax requests. 最有可能查看Ajax请求。

You are trying to mix up two languages ie, Java and Javascript/html together ie, onclick is a Javascript event and you can't call runJavaMethod from Javascript. 您正在尝试将 Java和Javascript / html这两种语言混合在一起,即onclick是Javascript事件,并且无法从Javascript调用runJavaMethod

In simple words, you can't directly call a Java method (present inside the scriptlet) using Javascript because all of your JSP code produces (becomes) html when it is loaded from the server . 简而言之,您无法使用Javascript直接调用Java方法(在scriptlet内部),因为从服务器加载时,所有JSP代码都会生成(成为)html

So, if you have to fix the issue, upon onclick , you need to call an URL which hits a servlet/controller method on the server to do the job (ie, executes the business logic). 因此,如果必须解决问题,请在onclick调用一个在服务器上命中servlet / controller方法的URL来完成该工作(即执行业务逻辑)。

One more important point is that Scriptlets are legacy code and I strongly suggest not use them to generate or manage the html content, rather use JSTL so that there will be a clear separation between the concerns (ie, business logic and user interface requirements) . 更为重要的一点是Scriptlet是遗留代码,我强烈建议不要使用Scriptlet来生成或管理html内容,而应使用JSTL,以便在关注点(即业务逻辑和用户界面要求)之间明确的区分

Also, I strongly suggest you read the JSP best practices from here and follow them. 另外,我强烈建议您从此处阅读JSP最佳实践并遵循它们。

You are mixing to two different things. 您正在混合两种不同的事物。 JSP is server side code and it's rendered response is the HTML that is send back to the browser.Javascript is pure client side in your case. JSP是服务器端代码,其呈现的响应是发送回浏览器的HTML。在您的情况下,Javascript是纯客户端。

If you really want invoke a server side processing than create a simple Java script function with Ajax call and get response back which u can use it. 如果您真的想调用服务器端处理,而不是通过Ajax调用创建一个简单的Java脚本函数并获得响应,那么您可以使用它。

I suggest send all the logic of JSP in backend class not a good practice to put in the jsp. 我建议在后端类中发送JSP的所有逻辑,而不是放在jsp中的好习惯。 JSP is ideally for UI design. JSP是UI设计的理想选择。

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

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