ajax,json获取action,html获取session中的值,显示登录名

来源:互联网 发布:火锅烧烤网络营销策划 编辑:程序博客网 时间:2024/06/05 21:08

问题描述:session中保存着UserInfo对象,成功登录后,在html中显示“欢迎xxx”
解决方法:通过ajax,json获取UserInfo数据,再显示

1.js

<script type="text/javascript" src="js/jquery-1.8.3.js"></script><script type="text/javascript">    $(function() {        $.ajax({            type : "get",            url : "login!getLoginName.action",            dataType : "text",            success : function(result) {                document.getElementsByTagName('b')[0].innerHTML=result;            },            error : function() {                alert("請求失敗");            }        });    });</script>

2.页面

<html><head><title>管理页面</title></head><body>     <table>            <tr>                        <td width="74%" height="38" class="admin_txt">管理员:<b></b>您好,感谢登陆使用!</td>                    </tr>                </table></body></html>

3.实体:UserInfo

public class UserInfo {    private int UserInfoId;    private String userInfoName;    private String UserInfoPsw;    //省略get,set    }

4.LoginAction中:

    public void getLoginName() {        System.out.println("getLoginUser");        HttpServletResponse response = ServletActionContext.getResponse();        response.setContentType("text/plain;charset=UTF-8");        PrintWriter out;        try {            String userName = ((UserInfo) ActionContext.getContext()                    .getSession().get("user")).getUserInfoName();            System.out.println(userName);            out = response.getWriter();            out.print(userName);            out.flush();            out.close();        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }
0 0