这篇文章给大家分享的是有关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="jquery-2.1.0.js"></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后台之后处理数据的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。