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 { /** * 处理客户端请求 */ @Override public 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>
相关推荐
Kafka 2020-09-18
Wepe0 2020-10-30
杜倩 2020-10-29
windle 2020-10-29
minerd 2020-10-28
mengzuchao 2020-10-22
Junzizhiai 2020-10-10
bxqybxqy 2020-09-30
风之沙城 2020-09-24
kingszelda 2020-09-22
大唐帝国前营 2020-08-18
yixu0 2020-08-17
TangCuYu 2020-08-15
xiaoboliu00 2020-08-15
songshijiazuaa 2020-08-15
xclxcl 2020-08-03
zmzmmf 2020-08-03
newfarhui 2020-08-03
likesyour 2020-08-01