- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试学习 jsp + servlet,如果我基本上做到了,一切都会很顺利(jsp 将数据发送到 servlet,然后 servlet 从 db 获取数据并设置属性并转发到新页面)但是当我尝试时出现了问题在不重新加载页面的情况下更新一些内容(使用 ajax 将数据发送到 servlet 并转发到 jsp 文件,我使用 jquery 加载特定内容),内容没有任何改变。
小服务程序
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
/* TODO output your page here. You may use following sample code. */
int id = Integer.parseInt(request.getParameter("id"));
Database db = new Database();
ProductTable productTable = new ProductTable(db);
Product product = productTable.findByID(id);
db.close();
request.setAttribute("productDetail", product);
request.getRequestDispatcher("/view/backend/selectProduct.jsp").forward(request, response);
} finally {
out.close();
}
}
JSP 模式
<!-- language: html -->
<div class="modal fade" id="modalEditProduct" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body">
<div class="tableSelectProduct">
<%@include file="selectProduct.jsp" %>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-primary" id="btnOK">Save</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
模态脚本
$(document).ready(function() {
$(".viewdetail").click(function() {
var id = $(this).attr('id');
$.ajax({
url: "/BookStore/SelectProduct",
data: {"id": id},
type: 'POST',
cache: false,
error: function() {
alert('error');
}
});
$(".tableSelectProduct").load("/view/backend/selectProduct.jsp");
$("#modalEditProduct").modal('show');
$("#btnOK").click(function() {
$("#modalEditProduct").modal('hide');
});
});
});
selectProduct.jsp(想通过不重新加载主页来更新此内容)
<!-- language: lang-html-->
<table width="100%" border="0" class="table" id="center" align="center">
<tr>
<td width="14%"></td>
<td width="63%">
<label for="thumbnail">Pic</label>
<a class="thumbnail">
<img class="resize" src="/BookStore/images/${productDetail.photo}" id="imgprofile"
data-src="/BookStore/asset/js/holder.js/130x100">
</a>
<td width="23%"><span id="titleNotice"> * ( required )</span></td>
</tr>
<tr>
<td width="14%"></td>
<td width="63%">
<label for="titleNotice">Title</label>
<input type="text" class="form-control" name="title"
onblur="checkEmpty(this, 'titleNotice')" value="${productDetail.title}"></td>
<td width="23%"><span id="titleNotice"> * ( required )</span></td>
</tr>
<tr>
<td></td>
<td>
<label for="titleNotice">ISBN</label>
<input type="text" class="form-control" id="form-control"
value="${productDetail.isbn}" onblur="checkEmpty(this, 'ISBNNotice')">
</td>
<td><span id="ISBNNotice"> * ( required )</span></td>
</tr>
<tr>
<td></td>
<td>
<label for="titleNotice">Catagory</label>
<input type="text" class="form-control" id="form-control" readonly="true"
value="${productDetail.catagory}" name="catagory" onblur="checkEmpty(this, 'ISBNNotice')">
</td>
<td>
<label for="titleNotice"></label>
<button type="button" class="btn btn-primary" id="addCatagory">
Change Catagory
</button>
</td>
</tr>
<tr>
<td></td>
<td>
<label for="titleNotice">Author</label>
<input type="text" class="form-control" id="form-control"
value="${productDetail.author}" name="author" onblur="checkEmpty(this, 'authorNotice')"></td>
<td><span id="authorNotice"> * ( required )</span></td>
</tr>
<tr>
<td height="130"></td>
<td>
<label for="titleNotice">Detail</label>
<textarea name="detail" rows="5" class="form-control" id="form-control"
onblur="checkEmpty(this, 'detailNotice')" >${productDetail.detail}
</textarea>
</td>
<td><span id="detailNotice"> * ( required )</span></td>
</tr>
<tr>
<td></td>
<td>
<label for="titleNotice">Price</label>
<input type="text" class="form-control" id="form-control"
value="${productDetail.price}" name="price" onblur="checkEmpty(this, 'priceNotice')">
</td>
<td><span id="priceNotice"> * ( required )</span></td>
</tr>
<tr>
<td></td>
<td>
<label for="titleNotice">Stock</label>
<input type="text" class="form-control" id="form-control"
value="${productDetail.quantity}" name="quantity"
onblur="checkEmpty(this, 'stockNotice')"></td>
<td><span id="stockNotice"> * ( required )</span></td>
</tr>
</table>
最佳答案
看起来您缺少成功属性。
$.ajax({
url: "/BookStore/SelectProduct",
data: {"id": id},
type: 'POST',
cache: false,
success: function(response) {
// Add in what to do when ajax call is successful
},
error: function(xhr, ajaxOptions, thrownError) {
alert('error');
}
关于java - 如何用servlet + ajax制作动态内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20416221/
我有一个关于 Java Servlet 的问题。 假设我在 servlet 网页“somePage”上。我想登录(使用另一个 servlet,“登录”servlet)。所以我点击“somePage”上
如何将变量数组从一个 servlet 传递到另一个 servlet? 最佳答案 如果您要将当前请求传递给另一个servlet,则只需将其设置为请求属性即可。 request.setAttribute(
什么可能导致此错误? Caused by: jakarta.servlet.UnavailableException: Servlet class org.restlet.ext.servle
我的maven依赖树是这样的 我想问我maven如何解决这个冲突,有两个servlet-api.jar?提前谢谢你。 最佳答案 如果您想从 Velocity 工具中删除 servlet-api,您可以
config ProcessReg ProcessReg text HelloWorld1 public class config implements Serv
您好,我有一个关于 servlet 调用另一个 servlet 的问题 我有一个名为 Relay 的主 servlet,它将负责控制其他 servlet 用户将点击并将转发到 Relay servle
在我的 REST API 项目中,我已将 /* 映射到 RESTServlet,并且需要在同一 WAR 中托管静态内容。我更愿意将 /static/* 映射到 WAS liberty 提供的默认 se
响应映射在 Servlet 中如何工作? 每个响应如何知道清除特定 HTML 或 Handlebars 上的输出? 最佳答案 有一个 ember-java带有 Jersey REST 服务 的 git
有一个 @WebServlet(urlPatterns = "/myServlet/") .如果用户转到 myapp/myServlet/other ,我仍然希望我的 servlet 能够捕获。也就是
我正在使用 Filter 在我的所有页面中插入反点击劫持 header - 这工作正常,除了 JBoss EAP 6.3 容器管理的登录页面,这是更重要的页面之一拥有它。 登录页面根本不调用过滤器,登
我正在尝试使用 RequestDispatcher 将数据从一个 servlet 传递到另一个 servlet。这是我的调度程序代码。 String address; address = "/Java
我刚刚开始使用 Servlet,并设法让一些 Servlet 充当单独的 URL,用于填充数据库以进行一些虚拟测试。某种形式: public class Populate_ServletName ex
我是否需要同时配置app.servlet.version 和 grails.servlet.version? 前者在application.properties中,后者在BuildConfig.gro
在Myeclipse中我创建了一个名为web1的Web项目,并添加了一个名为servlet1的servlet,web.xml如下: servlet1 servlet1
这个问题在这里已经有了答案: How to run a background task in a servlet based web application? (5 个回答) 6年前关闭。 是否可以在
在我的 ManagedBean 中,如果我将范围从 @RequestScoped 更改为 @ViewScoped,我将收到以下错误堆栈。我该如何解决这个问题?不过,当我运行应用程序时,我可以在页面中看
这个问题已经有答案了: How do I execute multiple servlets in sequence? (2 个回答) 已关闭 3 年前。 我已经构建了jdbc-Servlet的代码,
我一直在徒劳地尝试实现 tomcat 9 的 jakarta servlet,而不是以前的 javax.servlet 实现(因为我的理解是 jakarta 包是前进的方向)。问题是,当我将浏览器指向
我是 Spring 新手,正在尝试使用 Maven 部署和运行我的第一个 Spring Web 应用程序。有关更多详细信息,请参阅 here (我昨天发布的一个问题): 现在,我遇到的问题是:当我尝试
我正在尝试学习如何使用 JSP。我遇到了一些毫无意义的事情,至少在我看来是这样: 当我尝试运行时: response.getWriter().println(m.getDb().printAll())
我是一名优秀的程序员,十分优秀!