0
回答
Struts2+jquery+ajax用户验证
华为云实践训练营,热门技术免费实践!>>>   

这是struts.xml配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
 <constant name="struts.enable.DynamicMethodInvocation" value="false" />
 <constant name="struts.devMode" value="false" />
 <include file="example.xml" />

 <package name="default" namespace="/" extends="json-default">
  <action name="Login" class="ly.struts.action.LoginAction"
   method="login">
   <result name="success" type="redirect">index.jsp</result>
   <result name="input" type="dispatcher">login.jsp</result>
  </action>
 </package>
</struts>

这是后台action配置

 

package ly.struts.action;

 

 

import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {
 private static final long serialVersionUID = 1L;
 private String userId;
 private String message;

 
 public String login() {
  ActionContext content=ActionContext.getContext();
  HttpServletRequest request=ServletActionContext.getRequest();
  String names=request.getParameter("userId");
  System.out.println("username is "+ names);
//  HttpServletResponse response=ServletActionContext.getResponse();
//  HttpSession session=request.getSession();
  if("abc".equalsIgnoreCase(getUserId())){
   message="用户名已存在!";
   return SUCCESS;
  }else{
   message="用户名可以使用!";
   return INPUT;
  }
 }

 public String getUserId() {
  return userId;
 }

 public void setUserId(String userId) {
  this.userId = userId;
 }

 public String getMessage() {
  return message;
 }

 public void setMessage(String message) {
  this.message = message;
 }
 
}

这是前台页面代码

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
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>userInfo</title>
  <script type="text/javascript" src="http://ajax.googleapis. com/ajax/libs/jquery/1.8/jquery.min.js"></script>
  <script type="text/javascript" src="<%=basePath%>/js/jquery-1.8.3.min.js"></script>
  <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="userInfo">
 </head>
 <script type="text/javascript">
 function check_username(){
  $.ajax({
    type:'get',
    url:'Login.action',
    data:'userId='+userId,
    success:function(){
    alert("+++++++++"+data);
    return $("#msg").val();
  }
  });

}
</script>
 <body>
  <form action="Login.action" method="get">
   UserInfo
   <br />
   <div>
    username:
    <input type="text" id="userId" name="username" maxlength="20" />
    <font color="red" >*</font><span id="msg"></span>
   </div>
   <br />
   password:
   <input type="password" name="password" id="password" />
   <hr />
   <input type="submit" value="login" onclick="return check_username();" />
  </form>
 </body>
</html>

各位哥哥姐姐  谢了!

 

举报
no_smoking
发帖于5年前 0回/3K+阅
顶部