ssh-ajax登陆action返回字符串

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.4.4.min.js"></script>
</head>
<body>

    姓名:<input type="text" name="name" id="name"/>
    密码:<input type="password" name="password" id="password"> 
    <input type="button" id="login"  value="登陆">
   <script>  
    
        $("#login").click(function(){  
        var name=$("#name").val();
        var password=$("#password").val();

        var message={
                'name':name,
                'password':password
                }
            var url = "isLogin.action";  
            $.ajax({  
                type:'post',  
                url:url,  
                dataType: 'json', 
                data: message,
                success:function(data){  
                       var obj = $.parseJSON(data);  //使用
                     var state_value = obj.result; 
                       alert(state_value);
                }  
            })  
              
        })  
    </script>  

</body>
</html>
<?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>
      <package name="default" extends="struts-default" namespace="/">
          <action name="findAll" class="LoginAction" method="findAll">
              <result name="input">/input.jsp</result>
              <result name="success">/result.jsp</result>
          </action>
      </package>
      <package name="ajax" extends="json-default" namespace="/">
      <action name="isLogin" class="LoginAction" method="isLogin">
      
      <result type="json">
          <param name="root">result</param>
      </result>
          </action>
      </package>
  </struts>
package com.gu.action;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.json.JSONString;

import com.gu.model.Usermanager;
import com.gu.service.LoginService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;


public class LoginAction extends ActionSupport {
    public static Logger log=Logger.getLogger(LoginAction.class);
    
    private LoginService loginService;
    public void setLoginService(LoginService loginService) {
        this.loginService = loginService;
    }
    
    private String name;
    private String password;
    private String result;
    

    
    public String getResult() {
        return result;
    }


    public void setResult(String result) {
        this.result = result;
    }


    public String getName() {
        return name;
    }


    public void setName(String name) {
        this.name = name;
    }


    public String getPassword() {
        return password;
    }


    public void setPassword(String password) {
        this.password = password;
    }


    
    public String isLogin() throws Exception {
        
         result=loginService.isLogin(name, password);
    
            log.warn(result);
        //list转成json对象
        /*     JSONArray jsonArray=null;
         * jsonArray=JSONArray.fromObject(result);
            HttpServletResponse response=(HttpServletResponse) ActionContext.getContext().get(ServletActionContext.HTTP_RESPONSE);
            response.setCharacterEncoding("UTF-8");
            response.getWriter().print(jsonArray);*/
            HttpServletResponse response=(HttpServletResponse) ActionContext.getContext().get(ServletActionContext.HTTP_RESPONSE);
            response.setCharacterEncoding("UTF-8");
            
        
            String str="{\"result\":\""+result+"\"}";
            JSONObject jso=JSONObject.fromObject(str);
            result=jso.toString();
            return super.execute();
    }
    
    
    public String findAll(){
        List<Usermanager> list=loginService.findAll();
        ActionContext.getContext().put("list", list);
        //log.warn(list.get(0).getUserName()+","+list.get(0).getUserPassword());
        return SUCCESS;
    }
    
}

 

posted @ 2017-10-09 15:15  mmmmgxt  阅读(878)  评论(0编辑  收藏  举报