标签:sign tac writer str 1.3 使用 框架 name ext.get
-----------------------------------------------------------------------------------------jsp--------------------------------------------------------------------------------
<script type="text/javascript" src="/struts2.0/js/jquery-1.11.3.min.js"></script>
<script type="text/javascript">
function checkUsername(){
//获取文本框的值
var usernameValue = $("#username").val();
//向服务器发送请求
$.post("/struts2.0/checkUsername",{"username":usernameValue},function(data){
//var jsonObject = eval("("+data+")");
var html="";
if(data.flag){
html="<font color=‘green‘>"+data.message+"</font>";
}else{
html="<font color=‘red‘>"+data.message+"</font>";
}
$("#username_msg").html(html);
},"json");
}
</script>
</head>
<body>
username:<input type="text" name="username" id="username" onblur="checkUsername()"><span id="username_msg"></span><br>
password:<input type="password" name="password"><br>
</body>
</html>
-----------------------------------------------------------------------------------------action--------------------------------------------------------------------------------
public class LoginAction {
public void checkUsername(){
//1.接受数据
String username = ServletActionContext.getRequest().getParameter("username");
Result result = new Result();
//2.判断username是否可用
if("tom".equalsIgnoreCase(username)){
result.setFlag(false);
result.setMessage("用户名已被占用");
}else{
result.setFlag(true);
result.setMessage("用户名可以使用");
}
//将result转换成json
String json = JSONObject.toJSONString(result);
try {
//通过response响应到浏览器
ServletActionContext.getResponse().setCharacterEncoding("utf-8");
ServletActionContext.getResponse().getWriter().write(json);
Strtus2框架使用HttpServletResponse响应数据
标签:sign tac writer str 1.3 使用 框架 name ext.get
原文地址:http://www.cnblogs.com/wwwzzz/p/7841127.html