现在的位置: 首页 > 综合 > 正文

struts2+jquery($.ajax)

2013年10月24日 ⁄ 综合 ⁄ 共 2258字 ⁄ 字号 评论关闭

jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
 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>用户登录</title>

  <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="This is my page">
  <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->
  <script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
  <script type="text/javascript">
$(function(){
 $("#btn").click(function(){
 var u = $("#username").val();
 var p = $("#password").val();
 $.ajax({
 url:'login_ajax.action',
 data:{username:u,password:p},
 success:function(str){
  if(str == "ok"){
   alert("ok");
  }else{
   alert("no");
  }
 }
 });
 });
});
</script>
 </head>

 <body>
  <s:form action="login" id="login">
   <s:textfield name="username" label="用户名" id="username" />
   <s:password name="password" label="密码" id="password" />
   <s:submit value="提交" />
  </s:form>
  ajax测试<input type="button" value="ajax" id="btn" />
 </body>
</html>

_________________________________________________________________

action

package com.test.action;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

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

public class LoginAction extends ActionSupport{
 private String username;
 private String password;

 public String getUsername() {
  return username;
 }

 public void setUsername(String username) {
  this.username = username;
 }

 public String getPassword() {
  return password;
 }

 public void setPassword(String password) {
  this.password = password;
 }
 
 
   public void ajax()throws Exception{
    HttpServletResponse response = ServletActionContext.getResponse();
    PrintWriter out = response.getWriter();
    if(getUsername().equals("admin") && getPassword().equals("admin")) {
   out.write("ok");
  }else{
   out.write("no");
  }
   
   }
}

_____________________________________________________________________-

 

抱歉!评论已关闭.