Struts 2 + Jquery + Ajax 구현 사용자 이름 새로 고침 없 음
15252 단어 struts2
페이지 코드
<body>
<s:form action="registAction" method="post" namespace="/user">
<table>
<tr>
<td>UserName</td>
<td id="t1"><input type="text" name="userName" id="userName" /></td>
<td><input type="button" id="test" value="Test" /><div id="tip1"></div></td>
</tr>
<tr>
<td>Password</td>
<td id="t2" colspan="2"><input type="password" name="userPwd1" id="userPwd1" /></td>
</tr>
<tr>
<td>Password AG</td>
<td id="t3" colspan="2"><input type="password" name="userPwd2" id="userPwd2" /></td>
</tr>
<tr>
<td colspan="3">
<input type="submit" value="submit" />
<input type="reset" value="reset" />
</td>
</tr>
</table>
</s:form>
</body>
javaScript 코드:
$(document).ready(function(){
var inputUserNameObj = $("#userName");
var inputUserPwd1Obj = $("#userPwd1");
var inputUserPwd2Obj = $("#userPwd2");
$("#userName").blur(function(){
var text = inputUserNameObj.val();
$.post("testAction.action?userName="+text,null,function(response){
if(response==" "){
document.getElementById("tip1").innerHTML="<font color='red'>"+response+"</font>";
}else{
document.getElementById("tip1").innerHTML="<font color='blue'>"+response+"</font>";
}
});
});
});
Action 코드:
public class TestAction extends ActionSupport{
HttpServletRequest hsr;
String result;
public HttpServletRequest getHsr() {
return hsr;
}
public void setHsr(HttpServletRequest hsr) {
this.hsr = hsr;
}
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
public String test() throws IOException{
Map<String, String> map = new HashMap<String, String>();
ActionContext ac = ActionContext.getContext();
HttpServletRequest req = (HttpServletRequest)ac.get(ServletActionContext.HTTP_REQUEST);
HttpServletResponse res = (HttpServletResponse)ac.get(ServletActionContext.HTTP_RESPONSE);
String userName = req.getParameter("userName");
map.put("userName", userName);
res.setContentType("text/html;charset=UTF-8");
PrintWriter w = res.getWriter();
if("jack".equals(userName)){
// map json
JSONObject jo = JSONObject.fromObject(map);
System.out.println(" .....");
// json toString result
this.result = jo.toString();
w.println(" ");
}else{
w.println(" ");
}
w.close();
return "success";
}
}
Struts 설정:
<package name="ajax" extends="json-default" >
<!-- -->
<action name="testAction" class="com.hisoft.jqt.action.TestAction" method="test">
<!-- json sjon-default -->
<result type="json" name="success">
<!-- root -->
<!-- result action result -->
<param name="root">result</param>
</result>
</action>
</package>
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
apache struts2 취약점 검증이번에는 보안 캠프의 과제였던 apache struts2의 취약성에 대해 실제로 손을 움직여 실행해 보고 싶습니다. 환경 VirtualBox에서 브리지 어댑터 사용 호스트:macOS 10.12 게스트:ubuntu 1...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.