您好,登錄后才能下訂單哦!
前端jsp 代碼:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("input").focus(function(){
$("input").css("background-color","#FFFFCC");
});
$("input").blur(function(){
var username= $.trim($("#username").val()); //$.trim 去掉頭尾空格,.val()獲取username值
$.post("checkusername.action",{username:username},function(date) { //date 獲取返回對象
if(date.result=="ok") {
$("#span1").html("用戶名可以使用");
}else{
$("#span1").html("用戶名不可以使用");
}
return false;},
'json');
});
});
</script>
</head>
<body>
username:<input type="text" name="username" size="20" id="username">
<span id="span1"></span>
</body>
</html>
web.xml:添加struts支持
<display-name>Struts2</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
struts.xml: //配置action對應的類
<package name="struts2_login" extends="json-default">
<action name="checkusername"
class="action.CheckUsername" method="CheckPerson">
<result type="json"></result>
</action>
</package>
后端代碼:
public class CheckUsername extends ActionSupport{
private String username;
private String result;
...省略set(),get();
public String CheckPerson() throws Exception{ //進行驗證邏輯處理,我這只是檢測用戶名為aa的,就返回ok;
if(username.equals("aa")){
this.result="ok";
}
else{
this.result="no";
}
System.out.println(result);
return SUCCESS;
}
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。