struts2 ajax 交互

今天就将下struts2与Ajax交互实现的一个小例子:实现动态检验用户是否在数据库中存在


第一个是注册页面:register.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ page import="java.io.*"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title><%=basePath%>用户注册</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css"> --> <script type="text/javascript" src="JS/Ajax.js" ></script> <script type="text/javascript"><!-- function checkUsersName(value){ if(value!=""){ Ajax("get","check?usersName="+value,deal,null); } } function deal(){ if(Text=="1"){ document.getElementById('check').removeChild(document.getElementById('check').childNodes[0]); document.getElementById('check').appendChild(document.createTextNode("不行")); }else{ document.getElementById('check').removeChild(document.getElementById('check').childNodes[0]); document.getElementById('check').appendChild(document.createTextNode("行")); } } // --> </script> </head> <body> <form action="register" method="post" id="form1"> <table> <tr> <td>用户名:</td> <td><input type="text" name="usersName" οnblur="checkUsersName(this.value)"/><div id="check">111</div></td> </tr> <tr> <td><input type="submit" value="注册"/></td><td><input type="reset" value="重置"/></td> </tr> </table> </form> </body> </html>

第二个是action:RegisterAction


package com.article.action; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; import com.article.bean.users; import com.article.service.RegisterService; import com.article.utils.md5; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; public class RegisterAction extends ActionSupport{ private RegisterService rs; private List<File> usersImage; private List<String> usersImageFileName; private List<String> usersImageContentType; private String usersName; private String usersPass; private String usersEmail; private String usersQuestion; private String usersAnswer; private String imageFile; private String exit; private String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } //处理注册用户信息 public String execute() { //得到工程保存图片的路径 System.out.println(usersImage); String root = ServletActionContext.getRequest().getRealPath("/images"); //循环上传的文件 for(int i = 0 ; i < usersImage.size() ; i ++){ try{ InputStream is = new FileInputStream(usersImage.get(i)); //得到图片保存的位置(根据root来得到图片保存的路径在tomcat下的该工程里) File destFile = new File(root,this.getUsersImageFileName().get(i)); //把图片写入到上面设置的路径里 OutputStream os = new FileOutputStream(destFile); byte[] buffer = new byte[400]; int length = 0 ; while((length = is.read(buffer))>0){ os.write(buffer, 0, length); } is.close(); os.close(); }catch(Exception e) { e.printStackTrace(); return "error"; } } users user=new users(); user.setUsersName(usersName); user.setUsersPass(md5.md5s(usersPass)); user.setUsersQuestion(usersQuestion); user.setUsersAnswer(usersAnswer); user.setUsersEmail(usersEmail); user.setUsersRight(0); user.setUsersImage(root+"//"+this.getUsersImageFileName().get(0)); user.setUsersStatus(0); rs.addUser(user); message="<mce:script type="text/javascript"><!-- alert('账号注册成功,正在审核中!');window.location.href='login.jsp' // --></mce:script>"; return SUCCESS; } //是否存在该用户 //注意以下就是Ajax交互的重要代码 public String checkUserName() { HttpServletResponse response=ServletActionContext.getResponse(); response.setContentType("text/html"); response.setCharacterEncoding("utf-8"); PrintWriter out = null; try { out=response.getWriter(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } String name=""; try { //页面传递usersName过来,这里涉及到中文转码 name=java.net.URLDecoder.decode(usersName,"UTF-8"); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } //使用Ajax,存在返回1,否则返回0 if(rs.checkUserExit(name)!=null) { exit="1"; out.print(exit); out.flush(); out.close(); System.out.println(exit); return null; } return null;//一定是返回null } public RegisterService getRs() { return rs; } public void setRs(RegisterService rs) { this.rs = rs; } public List<File> getUsersImage() { return usersImage; } public void setUsersImage(List<File> usersImage) { this.usersImage = usersImage; } public List<String> getUsersImageFileName() { return usersImageFileName; } public void setUsersImageFileName(List<String> usersImageFileName) { this.usersImageFileName = usersImageFileName; } public List<String> getUsersImageContentType() { return usersImageContentType; } public void setUsersImageContentType(List<String> usersImageContentType) { this.usersImageContentType = usersImageContentType; } public String getUsersName() { return usersName; } public void setUsersName(String usersName) { this.usersName = usersName; } public String getUsersPass() { return usersPass; } public void setUsersPass(String usersPass) { this.usersPass = usersPass; } public String getUsersEmail() { return usersEmail; } public void setUsersEmail(String usersEmail) { this.usersEmail = usersEmail; } public String getUsersQuestion() { return usersQuestion; } public void setUsersQuestion(String usersQuestion) { this.usersQuestion = usersQuestion; } public String getUsersAnswer() { return usersAnswer; } public void setUsersAnswer(String usersAnswer) { this.usersAnswer = usersAnswer; } public String getExit() { return exit; } public void setExit(String exit) { this.exit = exit; } public String getImageFile() { return imageFile; } public void setImageFile(String imageFile) { this.imageFile = imageFile; } }

贴下一些必要代码.


Ajax.js文件的代码

function Ajax(method,url,statechange,data){ var ajax = createXMLHttpRequest(); ajax.open(method,url,true); ajax.onreadystatechange = function(){ if(ajax.readyState==4&&ajax.status==200){ statechange:Text = ajax.responseText; statechange(); } }; ajax.send(data); } function createXMLHttpRequest(){ var xmlhttp; if(window.XMLHttpRequest){//Firfox IE7���� xmlhttp = new XMLHttpRequest(); }else if(window.ActiveXObject){ xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } return xmlhttp; }

struts.xml


<action name="check" class="com.article.action.RegisterAction" method="checkUserName"> <result>register.jsp</result> </action>

上面的<result></result>里面可以不写,如果报错,就写个已存在的页面,不会跳转的,注册页面与action交互,action里execute的返回值必须为空!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值