index.jsp
pageEncoding="utf-8"%>
%>
%>
%>
Struts结合ajax示例src="jquery/jquery-1.2.6.js">
action="/reg.do?method=check">
οnblur="checkUsername()"/>
id="name_msg"
style="color:red">
/>
确定
function checkUsername(){
var url="/test/reg.do?method=check";
$.ajaxSetup({cache:false});
$.getJSON(url,
{"username":$("#username").val()},
function(json)
{
if(json.result=='success')
{
$("#name_msg").text("用户名可用");
}
else if(json.result=='failure')
{
$("#name_msg").text("用户名已被占用");
}
});
}
RegAction.java
public class RegAction extends DispatchAction {
public ActionForward check(ActionMapping mapping,ActionForm
form,HttpServletRequest request,HttpServletResponse response)throws
Exception{
PrintWriter out =
response.getWriter();
String username =
request.getParameter("username");
if(username!=null
&& "rain".equals(username)){
out.print("{/"result/":/"failure/"}");
}else{
out.print("{/"result/":/"success/"}");
}
return null;
}
}
struts-config.xml
type="org.apache.struts.action.DynaActionForm">
name="username" type="java.lang.String" />
name="password" type="java.lang.String" />
name="regForm" parameter="method"
scope="request">