下面小编就为大家带来一篇ajax提交到java后台之后处理数据的实现。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
环境:eclipse+struts
要实现的效果:点击按钮提交数据到后台之后回到前台显示出来数据
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<input type="text" id="userinput">
<input type="button" id="submit">
<div id="msg"></div>
</body>
<script type="text/javascript" src="anF1ZXJ5LTIuMS4wLmpz"></script>
<script type="text/javascript">
window.onload = function() {
document.getElementById("submit").onclick = test;
}
function test(){
var userinput = document.getElementById("userinput");
$.post("http://localhost:8080/TestSpring/TestAction",{username:userinput.value},
function(data, textStatus){
document.getElementById("msg").innerHTML = data;
});
}
</script>
</html>
struts.xml
<action name="TestAction" class="com.action.Test">
<result>index.jsp</result>
</action>
Test.java
package com.action;
import java.io.PrintWriter;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class Test extends ActionSupport {
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
HttpServletRequest request = org.apache.struts2.ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
out.write(request.getParameter("username"));
out.flush();
out.close();
return SUCCESS;
}
}
以上这篇ajax提交到java后台之后处理数据的实现就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程学习网。
沃梦达教程
本文标题为:ajax提交到java后台之后处理数据的实现


猜你喜欢
- 关于 html:如何从 css 表中删除边距和填充 2022-09-21
- 1 Vue - 简介 2023-10-08
- ajax实现输入提示效果 2023-02-14
- 深入浅析AjaxFileUpload实现单个文件的 Ajax 文件上传库 2022-12-15
- javascript 判断当前浏览器版本并判断ie版本 2023-08-08
- JS实现左侧菜单工具栏 2022-08-31
- 基于CORS实现WebApi Ajax 跨域请求解决方法 2023-02-14
- jsPlumb+vue创建字段映射关系 2023-10-08
- layui数据表格以及传数据方式 2022-12-13
- vue keep-alive 2023-10-08