Struts 1.3.8 学习笔记(六)
第六个版本,我们将User的信息放到Session中,这里的关键是在Action中取得Session对象,然后将对象保存到Session中,最后在jsp页面中取得该对象。
?
更新后的代码
LoginAction.java
package com.coderdream.action;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import org.apache.struts.action.Action;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionForward;import org.apache.struts.action.ActionMapping;import com.hp.gddc.ad.db.UserDao;import com.hp.gddc.ad.form.LoginForm;import com.hp.gddc.ad.vo.UserView;public class LoginAction extends Action {/** * 处理客户端请求 */@Overridepublic ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,HttpServletResponse response) throws Exception {// 得到客户的的提交数据LoginForm lf = (LoginForm) form;UserDao userDao = new UserDao();String userName = lf.getUserName();String password = lf.getPassword();UserView userView = new UserView();userView.setUserName(userName);userView.setPassword(password);int result = userDao.queryUser(userName, password);// 业务调用if (1 <= result) {HttpSession session = request.getSession();session.setAttribute("userView", userView);// 用户名密码验证成功,跳转到成功页面return mapping.findForward("success");} else {// 用户名密码错误,跳转到失败页面return mapping.findForward("failing");}}}
?
success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%><%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%><%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html:html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>登录成功</title></head><body>用户登录成功!<br> 对象保存在Session中:<logic:present name="userView" scope="session"><bean:write name="userView" property="userName" /></logic:present></body></html:html>
?