1.前台的form表单建立,注意action、enctype的内容,
2.通过添加一个隐藏的iframe标签使form的target指向iframe来达到不跳转页面的效果,同时需要在js里获取iframe里的内容(即后台利用GSON传回来的返回值)。
代码部分:
<form id="form1" action="../PublishPostingsServlet" enctype="multipart/form-data" method="POST" target="iframe_userInterface">
<!-- 正文区域--多行文本框 -->
<textarea name="ptext" id="ptext" cols="30" rows="10"></textarea>
<!-- 图片和标签选择区域 -->
<ul id="ptext_ul">
<li>
<a id="photo" href="javaScript:;" onclick="showPic();">
<em class="iconfont"></em>图片
</a>
</li>
<li id="lable">
<a href="javaScript:;" onclick="showLable();">
<em class="iconfont"></em>标签
</a>
</li>
</ul>
<div id="tupianqu">
<span class="ziti">本地上传</span>
<input id="tupian_btn" name="tupian_btn" type="file" accept="image/gif,image/jpeg,image/jpg,image/png" onchange="selectFile();" />
</div>
<button id="ptext_btn" type="submit">发布</button>
</form>
<iframe id="iframe_userInterface" name="iframe_userInterface" style="display: none;"></iframe>
3.js里获取文本代码如下:
$("#iframe_userInterface").load(function(){
var text = $(this).contents().find("body").text();//获取iframe里的内容
console.log(text);//打印iframe页面的内容
}
})
可以利用text来进行验证
后台要接收form表单传过去的数据,并且利用GSON将返回值传回到iframe里,
代码:
@WebServlet("/PublishPostingsServlet")
@MultipartConfig // 标识Servlet支持文件上传
public class PublishPostingsServlet extends HttpServlet {
private static final long serialVersionUID = 1L;// 主要用于版本控制
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 设置数据的编码方式为utf-8
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
// 返回的是项目在tomcat中的实际发布运行的根路径
String savePath = request.getServletContext().getRealPath("/picture");
Part part = request.getPart("tupian_btn");
String header = part.getHeader("content-disposition");// 获取请求头--form-data; name="tupian_btn"; filename=""
String fileName = getFileName(header);// 获取文件名
System.out.println("文件名:" + fileName);
part.write(savePath + File.separator + fileName);// 获取文件类型
/*判断登录状态*/
String id = null;
int result = 0;// 返回给前端的结果
HttpSession session = request.getSession();
id = (String) session.getAttribute("Account");
System.out.println("session里的id:" + id);
if (id == null) {
result = 4;// 当id为空的时候,登录失效,返回4
}
String ptext = request.getParameter("ptext");// 获取前台页面传递的参数
String label = Tools.getTable(ptext);
String ptime = Tools.getTime();
while (result == 0) {
result = PostingsService.publishPostings(id, ptext, ptime, label, fileName);// result接收数据在处理的结果1或2或3
}
Gson gson = new Gson();
String postinfsInfo = gson.toJson(result);// 定义postingsInfo存放GSON要传回的数据
response.getWriter().write(postinfsInfo);// 返回数据
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);// 调用doGet
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端