版权声明:转载请注明出处 https://blog.csdn.net/chushoufengli/article/details/81088303
仅适合小项目,简单的同时带来了好多弊端。
loginAction.java 的部分代码:
public String login(){
StringBuffer sb = null;
if(userService.isExitUser(username, password)){
//ActionContext.getContext().getSession().put("user", user);
//return "success";
User user = userService.getUser(username, password);
sb = new StringBuffer();
sb.append("{\"user\":[");
sb.append('{')
.append("\"id\":\"" + user.getId() + "\"")
.append(",")
.append("\"userName\":\"" + user.getUserName() + "\"")
.append(",")
.append("\"password\":\"" + user.getPassword() + "\"")
.append(",")
.append("\"realName\":\"" + user.getRealName() + "\"")
.append(",")
.append("\"score\":\"" + user.getScore() + "\"")
.append(",")
.append("\"First\":\"" + user.getFirst() + "\"")
.append(",")
.append("\"Second\":\"" + user.getSecond() + "\"")
.append(",")
.append("\"Third\":\"" + user.getThird() + "\"")
.append('}');
sb.append("]}");
}
String str = new String(sb);
ActionContext.getContext().getSession().put("user",str);
System.out.println("登录");
return "success";
}
.xml 的部分代码:
<!--登录-->
<action name="loginAction" class="loginAction" method="login">
<result name="success">/resultJSONLogin.jsp</result>
<!--
<result name="error">/error.jsp</result>
<result name="failure">/</result>
-->
</action>
resultJSONLogin.jsp 的代码:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><%=pageContext.getSession().getAttribute("user")%>
客户端收到的就是resultJSONLogin.jsp显示出来的信息,即JSON