使用dojo的Java方法调用
Java method call using dojo
我有一个java类,它包含一个方法,其中有一些业务逻辑。我需要在单击按钮时直接从JSP调用这个java方法(除了通过servelet或struts操作或scrippets)。我知道这可以使用DWR(根据其行为)来完成,但有没有可能使用dojo框架直接调用java方法?
我关心dojo而不是使用DWR,因为在我的应用程序中我已经在使用dojo框架了。我的应用程序也包含struts2,但没有调用struts操作,使用dojo(ajax调用)本身,我需要像在DWR中那样直接调用那个特定的java方法。
有使用dojo经验的人请指导我。
注意:如果您需要我的应用程序的代码部分,请告诉我,但我认为以上描述可能足以理解我的动机。
谢谢
zulox,
您需要做的是使用web应用程序设置模型-视图-控制器体系结构。这样,您就可以在前端单击后调用后端代码中的方法。
使用MVC,视图有效地提供了应用程序的用户界面元素。它将把模型中的数据呈现为适合用户界面的形式。
控制器接收用户输入并对模型对象和视图进行调用以执行适当的操作。
该模型管理应用程序的基本行为和数据。
您的JSP将如下所示:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form action="add" method="post">
Value 1:<input type="text" name="val1" id="val1"/><br>
Value 2:<input type="text" name="val2" id="val2"/><br>
<input type="submit" value="Submit"/><br>
</form>
<%String sum="";
sum = (String)request.getAttribute("val3"); %>
<input type="text" value="<%=sum%>" />
</body>
</html>
然后后端中的java文件将如下所示:
package controller;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class add extends HttpServlet {
String val3;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String val1=request.getParameter("val1");
String val2=request.getParameter("val2");
if(val1 != null && val2 != null)
val3=""+(Integer.parseInt(val1)+Integer.parseInt(val2));
else
val3="";
request.setAttribute("val3",val3);
request.getRequestDispatcher("index.jsp").forward(request, response);
try {
} finally {
out.close();
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
您还需要一个web服务器/servlet容器,它可以承载后端文件并为JSP提供服务。Apache Tomcat是一个很好的入门教程。这里有一些非常好的Tomcat教程,可以帮助您开始设置一切,以便轻松地为页面提供服务。
如果你有任何问题,请告诉我!
相关文章:
- 如何在webView,Android中从@JavascriptInterface方法调用Javascript
- javascript从子方法调用父方法
- 我应该返回一个类似console.log()的方法调用吗
- NodeJS-从同一文件中的另一个方法调用一个方法
- 使用restangular save方法调用put与post时的控件
- 从实例方法调用实例方法
- Node.js-异步方法调用问题
- Jquery事件绑定获胜'当作为方法调用时不起作用,但当直接在控制台中调用时会起作用
- 向模板实例变量传递调用方法调用的结果时出现异常
- jQuery 方法调用了两次
- 不能从 Backbone.View 的方法调用 Backbone.collection 的方法,其中包含 require
- 组合 onLayout 的方法调用
- lodash/下划线 - 使用绑定与方法调用和方法保持未绑定
- 从公共方法调用模块的私有函数
- 如何对 jqGrid 的 loadComplete 和 onSelectRow 进行基因预和后方法调用
- 完全限定的 JavaScript 方法调用
- 为什么我在 Angular JS 中收到两个方法调用
- 如何在从排序方法调用参数时将其传递给回调
- 使用两种不同的方法调用方法
- 如何在 javascript 中更改方法调用的全局范围